From b038a217b5713c695dd90144f60d87278b3bee9b Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Sat, 21 Dec 2024 02:19:58 +0100 Subject: [PATCH] Update test_xdr.py --- .../MDAnalysisTests/coordinates/test_xdr.py | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/testsuite/MDAnalysisTests/coordinates/test_xdr.py b/testsuite/MDAnalysisTests/coordinates/test_xdr.py index 2d0d471b96..5d8de657c4 100644 --- a/testsuite/MDAnalysisTests/coordinates/test_xdr.py +++ b/testsuite/MDAnalysisTests/coordinates/test_xdr.py @@ -27,6 +27,7 @@ import os import shutil import subprocess +import time from pathlib import Path import numpy as np @@ -938,8 +939,35 @@ def test_persistent_offsets_readonly(self, tmpdir): shutil.rmtree(tmpdir) def test_offset_lock_created(self): - # File is created, but without .lock ending - assert os.path.exists(XDR.offsets_filename(self.filename)) + lock_file_path = XDR.offsets_filename(self.filename, ending='lock') + lock = FileLock(lock_file_path) + + # Start a count to release lock after 5 seconds + def release_lock_after_delay(): + time.sleep(5) + lock.release() + + # Acquire the lock + lock.acquire() + try: + # Start count + release_lock_after_delay() + + # Start timing to ensure the reader waits for the lock + start_time = time.time() + + with pytest.warns(UserWarning, match="Waiting for lock file"): + self._reader(self.filename) + + elapsed_time = time.time() - start_time + + # Ensure the function waited for the lock + assert elapsed_time >= 5, "read_offsets did not wait for the lock to be released." + + finally: + # Ensure the lock is released to avoid hanging + if lock.is_locked: + lock.release() class TestXTCReader_offsets(_GromacsReader_offsets):