-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_objectcache.py
52 lines (41 loc) · 1.35 KB
/
test_objectcache.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
46
47
48
49
50
51
52
"""Test jigsawwm.objectcache module"""
from threading import Thread
from jigsawwm.objectcache import ObjectCache, ChangeDetector
class Foo:
"""A dummy class for testing"""
def test_object_cache_concurrency():
"""Test the ObjectCache class"""
cache = ObjectCache()
cache._create = Foo
result = [None for i in range(100)]
def concurrent_get(i):
result[i] = cache.get("foo")
return cache.get(i)
thread = [Thread(target=concurrent_get, args=(i,)) for i in range(len(result))]
for t in thread:
t.start()
for t in thread:
t.join()
id1 = id(result[0])
for r in result[1:]:
assert id1 == id(r)
def test_change_detector():
"""Test the ChangeDetector class"""
# first round
detector = ChangeDetector()
detector.current_keys = lambda: {1, 2, 3}
changed, new_keys, removed_keys = detector.detect_changes()
assert changed
assert new_keys == {1, 2, 3}
assert removed_keys == set()
# second round: no change
changed, new_keys, removed_keys = detector.detect_changes()
assert not changed
assert new_keys == set()
assert removed_keys == set()
# second round: removed
detector.current_keys = lambda: {2}
changed, new_keys, removed_keys = detector.detect_changes()
assert changed
assert new_keys == set()
assert removed_keys == {1, 3}