MCPcopy Create free account
hub / github.com/tox-dev/filelock / test_non_blocking

Function test_non_blocking

tests/test_filelock.py:319–384  ·  view source on GitHub ↗
(lock_type: type[BaseFileLock], tmp_path: Path)

Source from the content-addressed store, hash-verified

317
318@pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock])
319def test_non_blocking(lock_type: type[BaseFileLock], tmp_path: Path) -> None:
320 # raises Timeout error when the lock cannot be acquired
321 lock_path = tmp_path / "a"
322 lock_1, lock_2 = lock_type(str(lock_path)), lock_type(str(lock_path))
323 lock_3 = lock_type(str(lock_path), blocking=False)
324 lock_4 = lock_type(str(lock_path), timeout=0)
325 lock_5 = lock_type(str(lock_path), blocking=False, timeout=-1)
326
327 # acquire lock 1
328 lock_1.acquire()
329 assert lock_1.is_locked
330 assert not lock_2.is_locked
331 assert not lock_3.is_locked
332 assert not lock_4.is_locked
333 assert not lock_5.is_locked
334
335 # try to acquire lock 2
336 with pytest.raises(Timeout, match="The file lock '.*' could not be acquired."):
337 lock_2.acquire(blocking=False)
338 assert not lock_2.is_locked
339 assert lock_1.is_locked
340
341 # try to acquire pre-parametrized `blocking=False` lock 3 with `acquire`
342 with pytest.raises(Timeout, match="The file lock '.*' could not be acquired."):
343 lock_3.acquire()
344 assert not lock_3.is_locked
345 assert lock_1.is_locked
346
347 # try to acquire pre-parametrized `blocking=False` lock 3 with context manager
348 with pytest.raises(Timeout, match="The file lock '.*' could not be acquired."), lock_3:
349 pass
350 assert not lock_3.is_locked
351 assert lock_1.is_locked
352
353 # try to acquire pre-parametrized `timeout=0` lock 4 with `acquire`
354 with pytest.raises(Timeout, match="The file lock '.*' could not be acquired."):
355 lock_4.acquire()
356 assert not lock_4.is_locked
357 assert lock_1.is_locked
358
359 # try to acquire pre-parametrized `timeout=0` lock 4 with context manager
360 with pytest.raises(Timeout, match="The file lock '.*' could not be acquired."), lock_4:
361 pass
362 assert not lock_4.is_locked
363 assert lock_1.is_locked
364
365 # blocking precedence over timeout
366 # try to acquire pre-parametrized `timeout=-1,blocking=False` lock 5 with `acquire`
367 with pytest.raises(Timeout, match="The file lock '.*' could not be acquired."):
368 lock_5.acquire()
369 assert not lock_5.is_locked
370 assert lock_1.is_locked
371
372 # try to acquire pre-parametrized `timeout=-1,blocking=False` lock 5 with context manager
373 with pytest.raises(Timeout, match="The file lock '.*' could not be acquired."), lock_5:
374 pass
375 assert not lock_5.is_locked
376 assert lock_1.is_locked

Callers

nothing calls this directly

Calls 2

acquireMethod · 0.45
releaseMethod · 0.45

Tested by

no test coverage detected