Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds lock to FM provider #882

Merged
merged 7 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions tests/utils/test_filesystem_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Test the filesystem utils of VaRA-TS."""
import errno
import os
import unittest
import uuid
from fcntl import flock, LOCK_EX, LOCK_NB, LOCK_UN

from varats.utils.filesystem_util import lock_file


class TestFileLock(unittest.TestCase):
"""Tests whether the lock context manager works correctly."""

def test_file_locking(self):
"""Test that the file is locked when in a context manager."""
tmp_lock_file = "/tmp/lock-test.lock"

with lock_file(tmp_lock_file):
# File should automatically be created
self.assertTrue(os.path.exists(tmp_lock_file))

f = os.open(tmp_lock_file, os.O_RDONLY)

with self.assertRaises(OSError) as context:
# A non-blocking attempt to lock the file again should fail immediately
flock(f, LOCK_EX | LOCK_NB)
os.close(f)
self.assertEqual(context.exception.errno, errno.EWOULDBLOCK)

# Attempting to lock the file and immediately unlocking should now work
f = os.open(tmp_lock_file, os.O_RDONLY)
flock(f, LOCK_EX | LOCK_NB)
flock(f, LOCK_UN)
os.close(f)

def test_lock_file_new_folder(self):
"""Test that the lock context manager works correctly when the lock file
is in a new folder."""
tmp_lock_file = f"/tmp/{uuid.uuid4()}"

while os.path.isdir(tmp_lock_file):
tmp_lock_file = f"/tmp/{uuid.uuid4()}"

tmp_lock_file += "/lock-test.lock"

with lock_file(tmp_lock_file):
# File should automatically be created
self.assertTrue(os.path.exists(tmp_lock_file))
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from benchbuild.source.base import target_prefix

from varats.provider.provider import Provider
from varats.utils.filesystem_util import lock_file


class FeatureModelNotFound(FileNotFoundError):
Expand Down Expand Up @@ -90,6 +91,9 @@ def _get_feature_model_repository_path() -> Path:
refspec="origin/HEAD",
limit=1,
)
fm_source.fetch()

lock_path = Path(target_prefix()) / "fm_provider.lock"
with lock_file(lock_path):
fm_source.fetch()

return Path(Path(target_prefix()) / fm_source.local)
3 changes: 3 additions & 0 deletions varats-core/varats/utils/filesystem_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def __init__(self, folder: tp.Union[Path, str]) -> None:
@contextmanager
def lock_file(lock_path: Path,
lock_mode: int = fcntl.LOCK_EX) -> tp.Generator[None, None, None]:
# Create directories until lock file if required
os.makedirs(os.path.dirname(lock_path), exist_ok=True)

open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC
lock_fd = os.open(lock_path, open_mode)
try:
Expand Down
Loading