Skip to content

Commit

Permalink
Merge branch 'vara-dev' into f-HotFunctionDetection
Browse files Browse the repository at this point in the history
  • Loading branch information
vulder authored Apr 27, 2024
2 parents 1b2b0bd + 62cbd3a commit e99024c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
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

0 comments on commit e99024c

Please sign in to comment.