Example #1
0
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()
Example #2
0
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()
Example #3
0
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)
Example #4
0
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)
Example #5
0
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()
Example #6
0
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()
Example #7
0
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
Example #8
0
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()
Example #9
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()
Example #10
0
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()
Example #11
0
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()
Example #12
0
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()
Example #13
0
 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()
Example #14
0
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()
Example #15
0
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()
Example #16
0
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
Example #17
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()
Example #18
0
 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()
Example #19
0
def test_filelock_supports_with_statement(tmp_path):
    filename = tmp_path / "lock"
    with FileLock(filename):
        pass
Example #20
0
def test_filelock_supports_with_statement(tmpdir):
    filename = os.path.join(str(tmpdir), 'lock')
    with FileLock(filename):
        pass
Example #21
0
def acquire_lock_and_idle(filename):
    lock = FileLock(filename, timeout=0.01, poll=0.01)
    lock.acquire()
    while True:
        time.sleep(1.)
Example #22
0
def acquire_lock_and_idle(filename):
    lock = FileLock(filename, timeout=0.01, poll=0.01)
    lock.acquire()
    while True:
        time.sleep(1.)
Example #23
0
 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()
Example #24
0
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()