def test_pid_file_lock_context_manager(tmpdir): pid_file = Path(tmpdir) / "test.pid" assert not store.have_lock("%s" % pid_file) with daemon.pid_file_lock(pid_file): assert store.have_lock("%s" % pid_file)
def acquire(n): assert not store.have_lock(path) if debug: print(f"{n}: Trying lock\n") store.aquire_lock(path, blocking=True) assert store.have_lock(path) # We check to see if the other threads are actually waiting. if not saw_someone_wait: _wait_for_waiting_lock() saw_someone_wait.append(1) if debug: print(f"{n}: Got lock\n") acquired.append(1) # This part is guarded by the lock, so we should never have more than one entry in here, # even if multiple threads try to append at the same time assert len(acquired) == 1 acquired.pop() store.release_lock(path) assert not store.have_lock(path) if debug: print(f"{n}: Released lock\n")
def test_non_blocking_decorated_locking_without_previous_lock(locked_file, path_type, t1): assert t1.store != store path = path_type(locked_file) with store.try_locked(path) as result: assert result is True assert store.have_lock(path) is True assert store.have_lock(path) is False
def test_release_lock(locked_file, path_type): path = path_type(locked_file) assert store.have_lock(path) is False store.aquire_lock(path) assert store.have_lock(path) is True store.release_lock(path) assert store.have_lock(path) is False
def test_non_blocking_locking_without_previous_lock(locked_file, path_type, t1): assert t1.store != store path = path_type(locked_file) # Try to lock first assert store.try_aquire_lock(path) is True assert store.have_lock(path) is True store.release_lock(path) assert store.have_lock(path) is False
def test_acquire_lock(tmpdir): locked_file = tmpdir.join("locked_file") locked_file.write("") path = "%s" % locked_file assert store.have_lock(path) is False store.aquire_lock(path) assert store.have_lock(path) is True
def test_acquire_lock(tmp_path): locked_file = tmp_path / "locked_file" locked_file.write_text(u"", encoding="utf-8") path = str(locked_file) assert store.have_lock(path) is False store.aquire_lock(path) assert store.have_lock(path) is True
def test_locked(locked_file, path_type): path = path_type(locked_file) assert store.have_lock(path) is False with store.locked(path): assert store.have_lock(path) is True assert store.have_lock(path) is False
def acquire(_): try: store.aquire_lock(path, blocking=False) acquired.append(1) assert store.have_lock(path) store.release_lock(path) assert not store.have_lock(path) except IOError: assert not store.have_lock(path)
def test_try_locked(locked_file, path_type): path = path_type(locked_file) assert store.have_lock(path) is False with store.try_locked(path) as result: assert result is True assert store.have_lock(path) is True assert store.have_lock(path) is False
def test_cleanup_locked_pid_file(tmpdir): pid_file = Path(tmpdir) / "test.pid" assert not store.have_lock("%s" % pid_file) daemon.lock_with_pid_file(pid_file) assert store.have_lock("%s" % pid_file) daemon._cleanup_locked_pid_file(pid_file) assert not store.have_lock("%s" % pid_file)
def test_release_all_locks_already_closed(locked_file, path_type): path = path_type(locked_file) assert store.have_lock(path) is False store.aquire_lock(path) assert store.have_lock(path) is True os.close(store._locks._get_lock(str(path))) store.release_all_locks() assert store.have_lock(path) is False
def test_release_lock_already_closed(locked_file, path_type): path = path_type(locked_file) assert store.have_lock(path) is False store.aquire_lock(path) assert store.have_lock(path) is True os.close(store._acquired_locks[str(path)]) store.release_lock(path) assert store.have_lock(path) is False
def test_release_lock(tmp_path, path_type): locked_file = tmp_path / "locked_file" locked_file.write_text(u"", encoding="utf-8") path = path_type(locked_file) assert store.have_lock(path) is False store.aquire_lock(path) assert store.have_lock(path) is True store.release_lock(path) assert store.have_lock(path) is False
def test_release_all_locks_already_closed(locked_file, path_type): path = path_type(locked_file) assert store.have_lock(path) is False store.aquire_lock(path) assert store.have_lock(path) is True os.close(store._locks._get_lock(str(path))) # pylint:disable=no-value-for-parameter store.release_all_locks() assert store.have_lock(path) is False
def test_pid_file_lock_context_manager_exception(tmpdir): pid_file = Path(tmpdir) / "test.pid" assert not store.have_lock("%s" % pid_file) try: with daemon.pid_file_lock(pid_file): assert store.have_lock("%s" % pid_file) raise MKGeneralException("bla") except MKGeneralException: pass assert not store.have_lock("%s" % pid_file)
def test_non_blocking_decorated_locking_while_already_locked(locked_file, path_type, t1): assert t1.store != store path = path_type(locked_file) # Take lock with t1.store t1.lock() # And now try to get the lock (which should not be possible) with store.try_locked(path) as result: assert result is False assert store.have_lock(path) is False assert store.have_lock(path) is False
def test_locked(tmp_path, path_type): locked_file = tmp_path / "locked_file" locked_file.write_text(u"", encoding="utf-8") path = path_type(locked_file) assert store.have_lock(path) is False with store.locked(path): assert store.have_lock(path) is True assert store.have_lock(path) is False
def test_release_lock_already_closed(locked_file, path_type): path = path_type(locked_file) assert store.have_lock(path) is False store.aquire_lock(path) assert store.have_lock(path) is True fd = store._locks._get_lock(str(path)) assert isinstance(fd, int) os.close(fd) store.release_lock(path) assert store.have_lock(path) is False
def test_release_all_locks_already_closed(tmp_path): locked_file = tmp_path / "locked_file" locked_file.write_text(u"", encoding="utf-8") path = str(locked_file) assert store.have_lock(path) is False store.aquire_lock(path) assert store.have_lock(path) is True os.close(store._acquired_locks[path]) store.release_all_locks() assert store.have_lock(path) is False
def test_release_all_locks_already_closed(tmpdir): locked_file = tmpdir.join("locked_file") locked_file.write("") path = "%s" % locked_file assert store.have_lock(path) is False store.aquire_lock(path) assert store.have_lock(path) is True os.close(store._acquired_locks[path]) store.release_all_locks() assert store.have_lock(path) is False
def test_try_locked_fails(locked_file, path_type, monkeypatch): path = path_type(locked_file) def _is_already_locked(path, blocking): raise IOError(errno.EAGAIN, "%s is already locked" % path) monkeypatch.setattr(store._locks, "aquire_lock", _is_already_locked) assert store.have_lock(path) is False with store.try_locked(path) as result: assert result is False assert store.have_lock(path) is False assert store.have_lock(path) is False
def test_load_data_from_file_locking(tmp_path): locked_file = tmp_path / "locked_file" locked_file.write_text(u"[1, 2]", encoding="utf-8") data = store.load_data_from_file(str(locked_file), lock=True) assert data == [1, 2] assert store.have_lock(str(locked_file)) is True
def test_load_data_from_file_locking(tmp_path, path_type): locked_file = tmp_path / "locked_file" locked_file.write_text("[1, 2]", encoding="utf-8") data = store.load_object_from_file(path_type(locked_file), default=None, lock=True) assert data == [1, 2] assert store.have_lock(path_type(locked_file)) is True
def test_load_data_from_file_locking(tmpdir): locked_file = tmpdir.join("locked_file") locked_file.write("[1, 2]") data = store.load_data_from_file("%s" % locked_file, lock=True) assert data == [1, 2] assert store.have_lock("%s" % locked_file) is True
def test_non_blocking_decorated_locking_while_already_locked( locked_file, path_type, t1): assert t1.store != store path = path_type(locked_file) # Take lock with t1.store t1.do = "lock" for _dummy in range(20): if t1.store.have_lock(path): break time.sleep(0.01) assert t1.store.have_lock(path) is True # And now try to get the lock (which should not be possible) with store.try_locked(path) as result: assert result is False assert store.have_lock(path) is False assert store.have_lock(path) is False
def test_lock_with_pid_file(tmpdir): pid_file = Path(tmpdir) / "test.pid" daemon.lock_with_pid_file(pid_file) assert store.have_lock("%s" % pid_file) with pid_file.open() as f: assert int(f.read()) == os.getpid()
def test_non_blocking_locking_while_already_locked(locked_file, path_type, t1): assert t1.store != store path = path_type(locked_file) # Now take lock with t1.store t1.lock() # And now try to get the lock (which should not be possible) assert store.try_aquire_lock(path) is False assert store.have_lock(path) is False
def _cleanup_locked_pid_file(path: Path) -> None: """Cleanup the lock + file acquired by the function above""" if not store.have_lock(str(path)): return store.release_lock(str(path)) try: path.unlink() except OSError: pass
def test_release_all_locks(tmp_path): locked_file1 = tmp_path / "locked_file1" locked_file1.write_text(u"", encoding="utf-8") locked_file2 = tmp_path / "locked_file2" locked_file2.write_text(u"", encoding="utf-8") path1 = str(locked_file1) path2 = str(locked_file2) assert store.have_lock(path1) is False store.aquire_lock(path1) assert store.have_lock(path1) is True assert store.have_lock(path2) is False store.aquire_lock(path2) assert store.have_lock(path2) is True store.release_all_locks() assert store.have_lock(path1) is False assert store.have_lock(path2) is False