| 75 | @pytest.mark.parametrize("lock_type", [AsyncFileLock, AsyncSoftFileLock]) |
| 76 | @pytest.mark.asyncio |
| 77 | async def test_non_blocking(lock_type: type[BaseAsyncFileLock], tmp_path: Path) -> None: |
| 78 | # raises Timeout error when the lock cannot be acquired |
| 79 | lock_path = tmp_path / "a" |
| 80 | lock_1, lock_2 = lock_type(str(lock_path)), lock_type(str(lock_path)) |
| 81 | lock_3 = lock_type(str(lock_path), blocking=False) |
| 82 | lock_4 = lock_type(str(lock_path), timeout=0) |
| 83 | lock_5 = lock_type(str(lock_path), blocking=False, timeout=-1) |
| 84 | |
| 85 | # acquire lock 1 |
| 86 | await lock_1.acquire() |
| 87 | assert lock_1.is_locked |
| 88 | assert not lock_2.is_locked |
| 89 | assert not lock_3.is_locked |
| 90 | assert not lock_4.is_locked |
| 91 | assert not lock_5.is_locked |
| 92 | |
| 93 | # try to acquire lock 2 |
| 94 | with pytest.raises(Timeout, match="The file lock '.*' could not be acquired."): |
| 95 | await lock_2.acquire(blocking=False) |
| 96 | assert not lock_2.is_locked |
| 97 | assert lock_1.is_locked |
| 98 | |
| 99 | # try to acquire pre-parametrized `blocking=False` lock 3 with `acquire` |
| 100 | with pytest.raises(Timeout, match="The file lock '.*' could not be acquired."): |
| 101 | await lock_3.acquire() |
| 102 | assert not lock_3.is_locked |
| 103 | assert lock_1.is_locked |
| 104 | |
| 105 | # try to acquire pre-parametrized `blocking=False` lock 3 with context manager |
| 106 | with pytest.raises(Timeout, match="The file lock '.*' could not be acquired."): |
| 107 | async with lock_3: |
| 108 | pass |
| 109 | assert not lock_3.is_locked |
| 110 | assert lock_1.is_locked |
| 111 | |
| 112 | # try to acquire pre-parametrized `timeout=0` lock 4 with `acquire` |
| 113 | with pytest.raises(Timeout, match="The file lock '.*' could not be acquired."): |
| 114 | await lock_4.acquire() |
| 115 | assert not lock_4.is_locked |
| 116 | assert lock_1.is_locked |
| 117 | |
| 118 | # try to acquire pre-parametrized `timeout=0` lock 4 with context manager |
| 119 | with pytest.raises(Timeout, match="The file lock '.*' could not be acquired."): |
| 120 | async with lock_4: |
| 121 | pass |
| 122 | assert not lock_4.is_locked |
| 123 | assert lock_1.is_locked |
| 124 | |
| 125 | # blocking precedence over timeout |
| 126 | # try to acquire pre-parametrized `timeout=-1,blocking=False` lock 5 with `acquire` |
| 127 | with pytest.raises(Timeout, match="The file lock '.*' could not be acquired."): |
| 128 | await lock_5.acquire() |
| 129 | assert not lock_5.is_locked |
| 130 | assert lock_1.is_locked |
| 131 | |
| 132 | # try to acquire pre-parametrized `timeout=-1,blocking=False` lock 5 with context manager |
| 133 | with pytest.raises(Timeout, match="The file lock '.*' could not be acquired."): |
| 134 | async with lock_5: |