From 4ba91f2f4ce47d8e12e0fb1aca05132a91db1bc5 Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Thu, 27 Feb 2020 10:58:51 +0100 Subject: [PATCH] tests: check operation modes for `Object` Verify that `Object` can have multiple readers but as long as at least one reader is active, no one can write to it. Also checks that after `Object` has left the context it is not writable anymore. --- test/test_objectstore.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/test_objectstore.py b/test/test_objectstore.py index 003400c2..9a3eef40 100644 --- a/test/test_objectstore.py +++ b/test/test_objectstore.py @@ -142,6 +142,39 @@ class TestObjectStore(unittest.TestCase): # should have changed self.assertNotEqual(tree.treesum, x_hash) + def test_object_mode(self): + object_store = objectstore.ObjectStore(self.store) + with object_store.new() as tree: + # check that trying to write to a tree that is + # currently being read from fails + with tree.read() as _: + with self.assertRaises(ValueError): + tree.write() + + # check multiple readers are ok + with tree.read() as _: + # calculating the treesum also is reading, + # so this is 3 nested readers + _ = tree.treesum + + # writing should still fail + with self.assertRaises(ValueError): + tree.write() + + # Now that all readers are gone, writing should + # work + tree.write() + + # and back to reading, one last time + with tree.read() as _: + with self.assertRaises(ValueError): + tree.write() + + # tree has exited the context, it should NOT be + # writable anymore + with self.assertRaises(ValueError): + tree.write() + def test_snapshot(self): object_store = objectstore.ObjectStore(self.store) with object_store.new() as tree: