Forking what is left of ZeroNet and hopefully adding an AT Proto Frontend/Proxy
1import time
2
3from util import Cached
4
5
6class CachedObject:
7 def __init__(self):
8 self.num_called_add = 0
9 self.num_called_multiply = 0
10 self.num_called_none = 0
11
12 @Cached(timeout=1)
13 def calcAdd(self, a, b):
14 self.num_called_add += 1
15 return a + b
16
17 @Cached(timeout=1)
18 def calcMultiply(self, a, b):
19 self.num_called_multiply += 1
20 return a * b
21
22 @Cached(timeout=1)
23 def none(self):
24 self.num_called_none += 1
25 return None
26
27
28class TestCached:
29 def testNoneValue(self):
30 cached_object = CachedObject()
31 assert cached_object.none() is None
32 assert cached_object.none() is None
33 assert cached_object.num_called_none == 1
34 time.sleep(2)
35 assert cached_object.none() is None
36 assert cached_object.num_called_none == 2
37
38 def testCall(self):
39 cached_object = CachedObject()
40
41 assert cached_object.calcAdd(1, 2) == 3
42 assert cached_object.calcAdd(1, 2) == 3
43 assert cached_object.calcMultiply(1, 2) == 2
44 assert cached_object.calcMultiply(1, 2) == 2
45 assert cached_object.num_called_add == 1
46 assert cached_object.num_called_multiply == 1
47
48 assert cached_object.calcAdd(2, 3) == 5
49 assert cached_object.calcAdd(2, 3) == 5
50 assert cached_object.num_called_add == 2
51
52 assert cached_object.calcAdd(1, 2) == 3
53 assert cached_object.calcMultiply(2, 3) == 6
54 assert cached_object.num_called_add == 2
55 assert cached_object.num_called_multiply == 2
56
57 time.sleep(2)
58 assert cached_object.calcAdd(1, 2) == 3
59 assert cached_object.num_called_add == 3