-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
45 lines (28 loc) · 822 Bytes
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import time
import unittest
from util import CappedCache
class TestCappedCache(unittest.TestCase):
def test_max_size(self):
cc = CappedCache(max_size=5)
cc['a'] = 'x'
cc['b'] = 'x'
cc['c'] = 'x'
cc['d'] = 'x'
cc['e'] = 'x'
self.assertEqual(len(cc), 5)
cc['e'] = 'x'
self.assertEqual(len(cc), 5)
self.assertTrue('e' in cc)
cc['f'] = 'x'
self.assertEqual(len(cc), 5)
self.assertTrue('f' in cc)
def test_timeout(self):
cc = CappedCache()
cc.set('a', 'x', timeout=1)
self.assertTrue('a' in cc)
self.assertEqual(cc['a'], 'x')
time.sleep(2)
self.assertFalse('a' in cc)
self.assertIsNone(cc['a'])
if __name__ == '__main__':
unittest.main()