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>
This commit is contained in:
Kevin Morris 2021-11-22 20:06:50 -08:00
parent 155aa47a1a
commit 4b0cb0721d
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
3 changed files with 95 additions and 24 deletions

26
test/test_filelock.py Normal file
View file

@ -0,0 +1,26 @@
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()