Skip to content

Commit 071eba2

Browse files
committed
add tests on ttl
1 parent db62398 commit 071eba2

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import memoization
22
from memoization import cached
33
import unittest
4+
import time
45

56

67
class TestedFunctions:
@@ -18,6 +19,10 @@ def add(self, a, b):
1819
def subtract(self, a, b):
1920
return a - b
2021

22+
@cached(ttl=0.01)
23+
def multiply(self, a, b):
24+
return a * b
25+
2126
@cached()
2227
def function_with_side_effects(self, a):
2328
self.number += 1
@@ -211,6 +216,20 @@ def test_size_without_restriction(self):
211216
memoization.clear()
212217
self.assertEqual(memoization.size(), 0)
213218

219+
def test_ttl(self):
220+
"""
221+
Test memoization.cached with ttl
222+
"""
223+
for _ in range(3):
224+
self.f.multiply(2, 3)
225+
cache_unit = memoization._cache.get(self._wrapped_func_id(self.f.multiply))[self._make_cache_key(2, 3)]
226+
self.assertEqual(cache_unit['result'], 6)
227+
self.assertEqual(cache_unit['access_count'], 2)
228+
time.sleep(0.02)
229+
self.f.multiply(2, 3)
230+
cache_unit = memoization._cache.get(self._wrapped_func_id(self.f.multiply))[self._make_cache_key(2, 3)]
231+
self.assertEqual(cache_unit['access_count'], 0)
232+
214233

215234
if __name__ == '__main__':
216235
unittest.main()

0 commit comments

Comments
 (0)