|
| 1 | +import time |
| 2 | + |
1 | 3 | import pytest |
2 | 4 | from mybatis import Cache, CacheKey |
3 | 5 |
|
4 | 6 | def test_basic(): |
5 | | - cache = Cache(memory_limit=555) # 50MB |
| 7 | + cache = Cache(memory_limit=555, max_live_ms=10*1000) # 50MB, 10sec |
6 | 8 | cache.put(CacheKey("a", [1, 'a', None]), [{"a1": 1}, {"a2": 2}]) |
7 | 9 | cache.put(CacheKey("b", [2, 'b', None]), "2") |
8 | 10 | cache.put(CacheKey("c", [3, 'c', None]), "3") |
@@ -42,3 +44,18 @@ def test_basic(): |
42 | 44 |
|
43 | 45 | assert l[2][0] == '{"sql": "d", "param_list": [4, "d", null]}' |
44 | 46 | assert l[2][1] == None |
| 47 | + |
| 48 | +def test_timeout(): |
| 49 | + cache = Cache(memory_limit=555, max_live_ms=1 * 1000) # 50MB, 10sec |
| 50 | + cache.put(CacheKey("a", [1, 'a', None]), [{"a1": 1}, {"a2": 2}]) |
| 51 | + cache.put(CacheKey("b", [2, 'b', None]), "2") |
| 52 | + cache.put(CacheKey("c", [3, 'c', None]), "3") |
| 53 | + cache.put(CacheKey("d", [4, 'd', None]), None) |
| 54 | + |
| 55 | + time.sleep(2) |
| 56 | + assert cache.get(CacheKey("a", [1, 'a', None])) == None |
| 57 | + assert cache.get(CacheKey("b", [2, 'b', None])) == None |
| 58 | + assert cache.get(CacheKey("c", [3, 'c', None])) == None |
| 59 | + assert cache.get(CacheKey("d", [4, 'd', None])) == None |
| 60 | + |
| 61 | + assert cache.memory_used == 0 |
0 commit comments