aurweb/test/test_filelock.py
Kevin Morris 4b0cb0721d
fix(conftest): use synchronization locks for setup_database
We were running into data race issues where the `fn.is_file()`
check would occur twice before writing the file in the `else`
clause. For this reason, a new aurweb.lock.Lock class has been
added which doubles as a thread and process lock. We can use
this elsewhere in the future, but we are also able to use it
to solve this kind of data race issue.

That being said, we still need the lock file state to tell us
when the first caller acquired the lock.

Signed-off-by: Kevin Morris <kevr@0cost.org>
2021-11-28 19:55:11 -08:00

26 lines
668 B
Python

import py
from _pytest.logging import LogCaptureFixture
from aurweb.testing.filelock import FileLock
def test_filelock(tmpdir: py.path.local):
cb_path = None
def setup(path: str):
nonlocal cb_path
cb_path = str(path)
flock = FileLock(tmpdir, "test")
assert not flock.lock(on_create=setup)
assert cb_path == str(tmpdir / "test")
assert flock.lock()
def test_filelock_default(caplog: LogCaptureFixture, tmpdir: py.path.local):
# Test default_on_create here.
flock = FileLock(tmpdir, "test")
assert not flock.lock()
assert caplog.messages[0] == f"Filelock at {flock.path} acquired."
assert flock.lock()