3 # © Copyright 2021-2022, Scott Gasch
5 """shared_dict unittest."""
9 import parallelize as p
12 from collect.shared_dict import SharedDict
15 class SharedDictTest(unittest.TestCase):
16 @p.parallelize(method=p.Method.PROCESS)
17 def doit(self, n: int, dict_name: str):
18 d = SharedDict(dict_name)
20 msg = f'Hello from shard {n}'
22 self.assertTrue(n in d)
23 self.assertEqual(msg, d[n])
28 def test_basic_operations(self):
29 dict_name = 'test_shared_dict'
30 d = SharedDict(dict_name, 4096)
32 self.assertEqual(dict_name, d.get_name())
35 f = self.doit(n, d.get_name())
37 smart_future.wait_all(results)
39 self.assertTrue(f.wrapped_future.done())
41 self.assertEqual(d[k], f'Hello from shard {k}')
46 @p.parallelize(method=p.Method.PROCESS)
47 def add_one(self, name: str):
51 with SharedDict.MPLOCK:
56 def test_locking_works(self):
57 dict_name = 'test_shared_dict_lock'
58 d = SharedDict(dict_name, 4096)
63 f = self.add_one(d.get_name())
65 smart_future.wait_all(results)
66 self.assertEqual(10000, d["sum"])
72 if __name__ == '__main__':