def test_released_filelock_can_be_reacquired(tmpdir): filename = os.path.join(str(tmpdir), 'lock') lock = FileLock(filename, timeout=0.01, poll=0.01) lock.acquire() lock.release() lock.acquire() lock.release()
def test_released_filelock_can_be_reacquired(tmp_path): filename = tmp_path / "lock" lock = FileLock(filename, timeout=0.01, poll=0.01) lock.acquire() lock.release() lock.acquire() lock.release()
def acquire_lock(filename): lock = FileLock(filename, timeout=0.01, poll=0.01) try: lock.acquire() except TimeoutError: sys.exit(0) else: sys.exit(1)
def test_filelock_gets_released_on_lock_deletion(tmp_path): filename = tmp_path / "lock" lock = FileLock(filename, timeout=0.01, poll=0.01) lock.acquire() del lock lock = FileLock(filename, timeout=0.01, poll=0.01) lock.acquire() lock.release()
def test_filelock_gets_released_on_lock_deletion(tmpdir): filename = os.path.join(str(tmpdir), 'lock') lock = FileLock(filename, timeout=0.01, poll=0.01) lock.acquire() del lock lock = FileLock(filename, timeout=0.01, poll=0.01) lock.acquire() lock.release()
def test_can_acquire_filelock_at_most_once(tmpdir): filename = os.path.join(str(tmpdir), 'lock') lock = FileLock(filename, timeout=0.01, poll=0.01) lock.acquire() p = multiprocessing.Process(target=acquire_lock, args=(filename,)) p.start() p.join() lock.release() assert p.exitcode == 0
def test_can_release_filelock_multiple_times(tmp_path): filename = tmp_path / "lock" lock = FileLock(filename, timeout=0.01, poll=0.01) lock.release() lock.acquire() lock.release() lock.release()
def test_can_release_filelock_multiple_times(tmpdir): filename = os.path.join(str(tmpdir), 'lock') lock = FileLock(filename, timeout=0.01, poll=0.01) lock.release() lock.acquire() lock.release() lock.release()
def test_process_termination_releases_lock(tmpdir): filename = os.path.join(str(tmpdir), "lock") p = multiprocessing.Process(target=acquire_lock_and_idle, args=(filename,)) p.start() while p.is_alive() and not os.path.exists(filename): time.sleep(0.2) assert p.is_alive() lock = FileLock(filename, timeout=0.01, poll=0.01) with pytest.raises(TimeoutError): lock.acquire() p.terminate() p.join() lock = FileLock(filename, timeout=0.01, poll=0.01) lock.acquire() lock.release()
def __init__(self, filename): self.filename = filename self._lock = FileLock(self.filename + '.lock') self._index = None self._updates = {} self._deletes = set() self._removed_files = set()
def test_process_termination_releases_lock(tmpdir): filename = os.path.join(str(tmpdir), 'lock') p = multiprocessing.Process(target=acquire_lock_and_idle, args=(filename,)) p.start() while p.is_alive() and not os.path.exists(filename): time.sleep(0.2) assert p.is_alive() lock = FileLock(filename, timeout=0.01, poll=0.01) with pytest.raises(TimeoutError): lock.acquire() p.terminate() p.join() lock = FileLock(filename, timeout=0.01, poll=0.01) lock.acquire() lock.release()
def test_can_acquire_filelock_at_most_once(tmpdir): filename = os.path.join(str(tmpdir), 'lock') lock = FileLock(filename, timeout=0.01, poll=0.01) lock.acquire() p = multiprocessing.Process(target=acquire_lock, args=(filename, )) p.start() p.join() lock.release() assert p.exitcode == 0
def _remove_legacy_files(self): """Remove files from now invalid locations in the cache. This will not remove any files if a legacy file exists and is up to date. Once legacy files are removed, a legacy file will be written to avoid a costly ``os.listdir`` after calling this. """ lock_filename = 'legacy.lock' with FileLock(os.path.join(self.cache_dir, lock_filename)): if self._check_legacy_file(): return for f in os.listdir(self.cache_dir): if f == lock_filename: continue path = os.path.join(self.cache_dir, f) if os.path.isdir(path): shutil.rmtree(path) else: os.remove(path) self._write_legacy_file()
def __init__(self, cache_dir): super(WriteableCacheIndex, self).__init__(cache_dir) self._lock = FileLock(self.index_path + '.lock') self._updates = {} self._deletes = set() self._removed_files = set()
def test_filelock_supports_with_statement(tmp_path): filename = tmp_path / "lock" with FileLock(filename): pass
def test_filelock_supports_with_statement(tmpdir): filename = os.path.join(str(tmpdir), 'lock') with FileLock(filename): pass
def acquire_lock_and_idle(filename): lock = FileLock(filename, timeout=0.01, poll=0.01) lock.acquire() while True: time.sleep(1.)
def __init__(self, cache_dir): super().__init__(cache_dir) self._lock = FileLock(self.index_path + ".lock") self._updates = {} self._deletes = set() self._removed_files = set()
def test_can_acquire_filelock_at_most_once(tmpdir): filename = os.path.join(str(tmpdir), 'lock') lock = FileLock(filename, timeout=0.01, poll=0.01) lock.acquire() with pytest.raises(TimeoutError): lock.acquire() lock2 = FileLock(filename, timeout=0.01, poll=0.01) with pytest.raises(TimeoutError): lock2.acquire() lock.release()