Exemplo n.º 1
0
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)
Exemplo n.º 2
0
    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")
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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
Exemplo n.º 5
0
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
Exemplo n.º 6
0
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
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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
Exemplo n.º 9
0
 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)
Exemplo n.º 10
0
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
Exemplo n.º 11
0
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)
Exemplo n.º 12
0
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
Exemplo n.º 13
0
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
Exemplo n.º 14
0
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
Exemplo n.º 15
0
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
Exemplo n.º 16
0
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)
Exemplo n.º 17
0
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
Exemplo n.º 18
0
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
Exemplo n.º 19
0
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
Exemplo n.º 20
0
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
Exemplo n.º 21
0
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
Exemplo n.º 22
0
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
Exemplo n.º 23
0
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
Exemplo n.º 24
0
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
Exemplo n.º 25
0
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
Exemplo n.º 26
0
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
Exemplo n.º 27
0
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()
Exemplo n.º 28
0
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
Exemplo n.º 29
0
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
Exemplo n.º 30
0
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