Skip to content

Commit 266a973

Browse files
Added test against memory leaks with cache decorator (#555)
* Implemented caching wrapper for spectral helper * Added test for caching decorator * Added test for memory leaks with cache decorator
1 parent e351091 commit 266a973

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

pySDC/tests/test_helpers/test_spectral_helper.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,39 @@ def increment(self, x):
575575
assert dummy.num_calls == len(unique_vals)
576576

577577

578+
@pytest.mark.base
579+
def test_cache_memory_leaks():
580+
from pySDC.helpers.spectral_helper import cache
581+
582+
track = [0, 0]
583+
584+
class KeepTrack:
585+
586+
def __init__(self):
587+
track[0] += 1
588+
track[1] = 0
589+
590+
@cache
591+
def method(self, a, b, c=1, d=2):
592+
track[1] += 1
593+
return f"{a},{b},c={c},d={d}"
594+
595+
def __del__(self):
596+
track[0] -= 1
597+
598+
def function():
599+
obj = KeepTrack()
600+
for _ in range(10):
601+
obj.method(1, 2, d=2)
602+
assert track[0] == 1
603+
assert track[1] == 1
604+
605+
for _ in range(3):
606+
function()
607+
608+
assert track[0] == 0, "possible memory leak with the @cache"
609+
610+
578611
if __name__ == '__main__':
579612
str_to_bool = lambda me: False if me == 'False' else True
580613
str_to_tuple = lambda arg: tuple(int(me) for me in arg.split(','))

0 commit comments

Comments
 (0)