diff options
| author | Scott <[email protected]> | 2022-01-30 21:29:34 -0800 |
|---|---|---|
| committer | Scott <[email protected]> | 2022-01-30 21:29:34 -0800 |
| commit | 865825894beeedd47d26dd092d40bfee582f5475 (patch) | |
| tree | a439e1a8ba4a013c57779f9399ed298fdbaa4000 /tests | |
| parent | 5e09a33068fcdf6d43f12477dd943e108e11ae06 (diff) | |
Change locking boundaries for shared dict. Add a unit test.
Make smart_futures re-raise exceptions that happened in futures.
Mess with file_utils.
Diffstat (limited to 'tests')
| -rwxr-xr-x | tests/shared_dict_test.py | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/tests/shared_dict_test.py b/tests/shared_dict_test.py new file mode 100755 index 0000000..c8294c5 --- /dev/null +++ b/tests/shared_dict_test.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 + +import unittest + +from collect.shared_dict import SharedDict +import parallelize as p +import smart_future +import unittest_utils + + +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() |
