| 54 | @pytest.mark.parametrize("lock_type", [AsyncFileLock, AsyncSoftFileLock]) |
| 55 | @pytest.mark.asyncio |
| 56 | async def test_non_blocking(lock_type: type[BaseAsyncFileLock], tmp_path: Path) -> None: |
| 57 | lock_path = tmp_path / "a" |
| 58 | lock_1, lock_2 = lock_type(str(lock_path)), lock_type(str(lock_path)) |
| 59 | lock_3 = lock_type(str(lock_path), blocking=False) |
| 60 | lock_4 = lock_type(str(lock_path), timeout=0) |
| 61 | lock_5 = lock_type(str(lock_path), blocking=False, timeout=-1) |
| 62 | |
| 63 | await lock_1.acquire() |
| 64 | assert lock_1.is_locked |
| 65 | assert not lock_2.is_locked |
| 66 | assert not lock_3.is_locked |
| 67 | assert not lock_4.is_locked |
| 68 | assert not lock_5.is_locked |
| 69 | |
| 70 | with pytest.raises(Timeout, match=r"The file lock '.*' could not be acquired."): |
| 71 | await lock_2.acquire(blocking=False) |
| 72 | assert not lock_2.is_locked |
| 73 | assert lock_1.is_locked |
| 74 | |
| 75 | with pytest.raises(Timeout, match=r"The file lock '.*' could not be acquired."): |
| 76 | await lock_3.acquire() |
| 77 | assert not lock_3.is_locked |
| 78 | assert lock_1.is_locked |
| 79 | |
| 80 | with pytest.raises(Timeout, match=r"The file lock '.*' could not be acquired."): |
| 81 | async with lock_3: |
| 82 | pass # pragma: no cover # __aenter__ raises Timeout, so the body never runs |
| 83 | assert not lock_3.is_locked |
| 84 | assert lock_1.is_locked |
| 85 | |
| 86 | with pytest.raises(Timeout, match=r"The file lock '.*' could not be acquired."): |
| 87 | await lock_4.acquire() |
| 88 | assert not lock_4.is_locked |
| 89 | assert lock_1.is_locked |
| 90 | |
| 91 | with pytest.raises(Timeout, match=r"The file lock '.*' could not be acquired."): |
| 92 | async with lock_4: |
| 93 | pass # pragma: no cover # __aenter__ raises Timeout, so the body never runs |
| 94 | assert not lock_4.is_locked |
| 95 | assert lock_1.is_locked |
| 96 | |
| 97 | # blocking precedence over timeout |
| 98 | with pytest.raises(Timeout, match=r"The file lock '.*' could not be acquired."): |
| 99 | await lock_5.acquire() |
| 100 | assert not lock_5.is_locked |
| 101 | assert lock_1.is_locked |
| 102 | |
| 103 | with pytest.raises(Timeout, match=r"The file lock '.*' could not be acquired."): |
| 104 | async with lock_5: |
| 105 | pass # pragma: no cover # __aenter__ raises Timeout, so the body never runs |
| 106 | assert not lock_5.is_locked |
| 107 | assert lock_1.is_locked |
| 108 | |
| 109 | await lock_1.release() |
| 110 | assert not lock_1.is_locked |
| 111 | assert not lock_2.is_locked |
| 112 | assert not lock_3.is_locked |
| 113 | assert not lock_4.is_locked |