blob: 230bdb989c4c5e74c6d31cecebee3ea468b277c8 (
plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
#!/usr/bin/env python3
# © Copyright 2021-2022, Scott Gasch
"""shared_dict unittest."""
import unittest
import parallelize as p
import smart_future
import unittest_utils
from collect.shared_dict import SharedDict
class SharedDictTest(unittest.TestCase):
@p.parallelize(method=p.Method.PROCESS)
def doit(self, n: int, dict_name: str):
d = SharedDict(dict_name)
try:
msg = f'Hello from shard {n}'
d[n] = msg
self.assertTrue(n in d)
self.assertEqual(msg, d[n])
return n
finally:
d.close()
def test_basic_operations(self):
dict_name = 'test_shared_dict'
d = SharedDict(dict_name, 4096)
try:
self.assertEqual(dict_name, d.get_name())
results = []
for n in range(100):
f = self.doit(n, d.get_name())
results.append(f)
smart_future.wait_all(results)
for f in results:
self.assertTrue(f.wrapped_future.done())
for k in d:
self.assertEqual(d[k], f'Hello from shard {k}')
finally:
d.close()
d.cleanup()
@p.parallelize(method=p.Method.PROCESS)
def add_one(self, name: str):
d = SharedDict(name)
try:
for x in range(1000):
with SharedDict.MPLOCK:
d["sum"] += 1
finally:
d.close()
def test_locking_works(self):
dict_name = 'test_shared_dict_lock'
d = SharedDict(dict_name, 4096)
try:
d["sum"] = 0
results = []
for n in range(10):
f = self.add_one(d.get_name())
results.append(f)
smart_future.wait_all(results)
self.assertEqual(10000, d["sum"])
finally:
d.close()
d.cleanup()
if __name__ == '__main__':
unittest.main()
|