From 77e7f71ee3124714f6db27af0fa1f0bd43100ca5 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Wed, 7 Sep 2022 12:03:50 +0100 Subject: [PATCH] tests: journal_lock: Add, and use, `_release_lock()` to fix TestJournalLock.test_release_lock Because the `with ...` was leaving the file locked it then couldn't be `os.unlink()`'d. --- tests/journal_lock.py/test_journal_lock.py | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/journal_lock.py/test_journal_lock.py b/tests/journal_lock.py/test_journal_lock.py index eb100269..6d93d54a 100644 --- a/tests/journal_lock.py/test_journal_lock.py +++ b/tests/journal_lock.py/test_journal_lock.py @@ -104,6 +104,40 @@ def _obtain_lock(prefix: str, filehandle) -> bool: return False return True + + +def _release_lock(prefix: str, filehandle) -> bool: + """ + Release the JournalLock. + + :param prefix: str - what to prefix output with. + :param filehandle: File handle already open on the lockfile. + :return: bool - True if we released the lock. + """ + if sys.platform == 'win32': + print(f'{prefix}: On win32') + import msvcrt + try: + print(f'{prefix}: Trying msvcrt.locking() ...') + filehandle.seek(0) + msvcrt.locking(filehandle.fileno(), msvcrt.LK_UNLCK, 4096) + + except Exception as e: + print(f'{prefix}: Unable to unlock file: {e!r}') + return False + + else: + import fcntl + + print(f'{prefix}: Not win32, using fcntl') + try: + fcntl.flock(filehandle, fcntl.LOCK_UN) + + except Exception as e: + print(f'{prefix}: Unable to unlock file: {e!r}') + return False + + return True ########################################################################### @@ -316,6 +350,7 @@ class TestJournalLock: # And finally check it actually IS unlocked. with open(mock_journaldir.getbasetemp() / 'edmc-journal-lock.txt', mode='w+') as lf: assert _obtain_lock('release-lock', lf) + assert _release_lock('release-lock', lf) # Cleanup, to avoid side-effect on other tests os.unlink(str(jlock.journal_dir_lockfile_name))