def test_acquire_lock(self, lock_file: unittest.mock.Mock) -> None: (_, path) = tempfile.mkstemp() lockfile_file_descriptor = None with acquire_lock(path, blocking=False) as file_descriptor: lockfile_file_descriptor = file_descriptor with acquire_lock(path, blocking=True): pass lock_file.assert_has_calls( [ call(lockfile_file_descriptor, fcntl.LOCK_EX | fcntl.LOCK_NB), call(lockfile_file_descriptor, fcntl.LOCK_UN), call(lockfile_file_descriptor, fcntl.LOCK_EX), call(lockfile_file_descriptor, fcntl.LOCK_UN), ] ) def fail_on_exclusive(_, lock_kind): if lock_kind == fcntl.LOCK_EX | fcntl.LOCK_NB: raise OSError() return None lock_file.side_effect = fail_on_exclusive with self.assertRaises(OSError): with acquire_lock(path, blocking=False): pass
def test_try_lock(self, lock_file: unittest.mock.Mock) -> None: (_, path) = tempfile.mkstemp() lockfile_file_descriptor = None with try_lock(path) as file_descriptor: lockfile_file_descriptor = file_descriptor lock_file.assert_has_calls([ call(lockfile_file_descriptor, fcntl.LOCK_EX | fcntl.LOCK_NB), call(lockfile_file_descriptor, fcntl.LOCK_UN) ]) def fail_on_exclusive(_, lock_kind): if lock_kind == fcntl.LOCK_EX | fcntl.LOCK_NB: raise OSError() return None lock_file.side_effect = fail_on_exclusive with self.assertRaises(OSError): with try_lock(path): pass
def test_try_lock(self, lock_file: unittest.mock.Mock) -> None: (_, path) = tempfile.mkstemp() lockfile_file_descriptor = None # pyre-ignore: T29602753 with try_lock(path) as file_descriptor: lockfile_file_descriptor = file_descriptor lock_file.assert_has_calls([ call(lockfile_file_descriptor, fcntl.LOCK_EX | fcntl.LOCK_NB), call(lockfile_file_descriptor, fcntl.LOCK_UN), ]) def fail_on_exclusive(_, lock_kind): if lock_kind == fcntl.LOCK_EX | fcntl.LOCK_NB: raise OSError() return None lock_file.side_effect = fail_on_exclusive with self.assertRaises(OSError): with try_lock(path): pass