| 45 | |
| 46 | @pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock]) |
| 47 | def test_non_blocking(lock_type: type[BaseFileLock], tmp_path: Path) -> None: |
| 48 | path = tmp_path / "a" |
| 49 | holder = lock_type(path) |
| 50 | contender = lock_type(path) |
| 51 | peers = (lock_type(path, blocking=False), lock_type(path, timeout=0), lock_type(path, blocking=False, timeout=-1)) |
| 52 | message = r"The file lock '.*' could not be acquired." |
| 53 | |
| 54 | with holder: |
| 55 | assert holder.is_locked |
| 56 | assert not contender.is_locked |
| 57 | assert not any(peer.is_locked for peer in peers) |
| 58 | with pytest.raises(Timeout, match=message): |
| 59 | contender.acquire(blocking=False) |
| 60 | assert (contender.is_locked, holder.is_locked) == (False, True) |
| 61 | |
| 62 | for peer in peers: |
| 63 | with pytest.raises(Timeout, match=message): |
| 64 | peer.acquire() |
| 65 | assert (peer.is_locked, holder.is_locked) == (False, True) |
| 66 | with pytest.raises(Timeout, match=message), peer: |
| 67 | pass # pragma: no cover # entering the context raises Timeout |
| 68 | assert (peer.is_locked, holder.is_locked) == (False, True) |
| 69 | |
| 70 | assert not any(lock.is_locked for lock in (holder, contender, *peers)) |
| 71 | |
| 72 | |
| 73 | @pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock]) |