def testcontendedpyandrust(self): with self.ui.configoverride({("devel", "lockmode"): "python_and_rust"}): acquired, released = (0, 0) def acquire(): nonlocal acquired acquired += 1 def release(): nonlocal released released += 1 with lock.rustlock(self.vfs, "foo"): with self.assertRaises(error.LockHeld): lock.trylock( self.ui, self.vfs, "foo", 0, acquirefn=acquire, releasefn=release, ) # Rust still locked self.assertLocked("foo") # Make sure we released python lock and didn't call any callbacks. self.assertLegacyLock("foo", False) self.assertEqual(acquired, 0) self.assertEqual(released, 0)
def testsubdirlock(self): self.vfs.mkdir("some_dir") l = lock.rustlock( self.vfs, "some_dir/foo", ) self.assertLocked("some_dir/foo") l.release() self.assertNotLocked("some_dir/foo")
def testfork(self): l = lock.rustlock(self.vfs, "foo") pid = os.fork() if pid == 0: l.release() self.assertLocked("foo") os._exit(0) os.waitpid(pid, 0) self.assertLocked("foo") l.release() self.assertNotLocked("foo")
def testcallbacks(self): acquired, prereleased, postreleased = (0, 0, 0) def acquire(): nonlocal acquired acquired += 1 def release(): nonlocal prereleased prereleased += 1 def postrelease(): nonlocal postreleased postreleased += 1 l = lock.rustlock( self.vfs, "foo", acquirefn=acquire, releasefn=release, ) l.postrelease.append(postrelease) self.assertLocked("foo") self.assertEqual(acquired, 1) self.assertEqual(prereleased, 0) self.assertEqual(postreleased, 0) # recursive lock call - don't call callbacks again l.lock() l.release() self.assertLocked("foo") self.assertEqual(acquired, 1) self.assertEqual(prereleased, 0) self.assertEqual(postreleased, 0) l.release() self.assertNotLocked("foo") self.assertEqual(acquired, 1) self.assertEqual(prereleased, 1) self.assertEqual(postreleased, 1)
def testpermissionerror(self): os.chmod(self.vfs.base, 0) with self.assertRaises(error.LockUnavailable): lock.rustlock(self.vfs, "foo")
def assertNotLocked(self, name): try: lock.rustlock(self.vfs, name, timeout=0).release() except Exception as err: self.assertTrue(False, str(err))
def assertLocked(self, name): with self.assertRaises(error.LockHeld): lock.rustlock(self.vfs, name, timeout=0)