Skip to content

Commit

Permalink
Merge pull request #229 from Griffin-Sullivan/0.4.0-merging
Browse files Browse the repository at this point in the history
Fix 0.4.0 merge conflicts
  • Loading branch information
JacobCallahan committed Aug 29, 2023
2 parents 773942f + c7aed5e commit c67b246
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
38 changes: 38 additions & 0 deletions broker/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,44 @@ def simple_retry(cmd, cmd_args=None, cmd_kwargs=None, max_timeout=60, _cur_timeo
simple_retry(cmd, cmd_args, cmd_kwargs, max_timeout, new_wait)


class FileLock:
"""Basic file locking class that acquires and releases locks.
Recommended usage is the context manager which will handle everything for you.
with FileLock("basic_file.txt"):
Path("basic_file.txt").write_text("some text")
If a lock is already in place, FileLock will wait up to <timeout> seconds
"""

def __init__(self, file_name, timeout=10):
self.lock = Path(f"{file_name}.lock")
self.timeout = timeout

def wait_file(self):
"""Wait for an existing lock to be released, if applicable."""
timeout_after = time.time() + self.timeout
while self.lock.exists():
if time.time() <= timeout_after:
time.sleep(1)
else:
raise exceptions.BrokerError(
f"Timeout while waiting for lock release: {self.lock.absolute()}"
)
self.lock.touch()

def return_file(self):
"""Release our lock."""
self.lock.unlink()

def __enter__(self): # noqa: D105
self.wait_file()

def __exit__(self, *tb_info): # noqa: D105
self.return_file()


class Result:
"""Dummy result class for presenting results in dot access."""

Expand Down
5 changes: 3 additions & 2 deletions catalog-info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ metadata:
- aap
- docker
- podman
namespace: quality-community
annotations:
github.com/project-slug: SatelliteQE/broker
spec:
type: library
owner: satelliteqe
lifecycle: beta
owner: group:redhat/satelliteqe
lifecycle: preproduction
17 changes: 17 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ def test_emitter(tmp_file):
assert written == {"test": "value", "another": 5, "thing": 13}


def test_lock_file_created(tmp_file):
lock_file = helpers.FileLock(tmp_file)
with lock_file:
assert isinstance(lock_file.lock, Path)
assert lock_file.lock.exists()
assert not lock_file.lock.exists()


def test_lock_timeout(tmp_file):
tmp_lock = Path(f"{tmp_file}.lock")
tmp_lock.touch()
with pytest.raises(exceptions.BrokerError) as exc:
with helpers.FileLock(tmp_file, timeout=1):
pass
assert str(exc.value).startswith("Timeout while waiting for lock release: ")


def test_find_origin_simple():
origin = helpers.find_origin()
assert len(origin) == 2
Expand Down

0 comments on commit c67b246

Please sign in to comment.