MCPcopy
hub / github.com/python-trio/trio / test_Lock_and_StrictFIFOLock

Function test_Lock_and_StrictFIFOLock

src/trio/_tests/test_sync.py:333–407  ·  view source on GitHub ↗
(
    lockcls: type[Lock | StrictFIFOLock],
)

Source from the content-addressed store, hash-verified

331
332@pytest.mark.parametrize("lockcls", [Lock, StrictFIFOLock], ids=lambda fn: fn.__name__)
333async def test_Lock_and_StrictFIFOLock(
334 lockcls: type[Lock | StrictFIFOLock],
335) -> None:
336 l = lockcls() # noqa
337 assert not l.locked()
338
339 # make sure locks can be weakref'ed (gh-331)
340 r = weakref.ref(l)
341 assert r() is l
342
343 repr(l) # smoke test
344 # make sure repr uses the right name for subclasses
345 assert lockcls.__name__ in repr(l)
346 with assert_checkpoints():
347 async with l:
348 assert l.locked()
349 repr(l) # smoke test (repr branches on locked/unlocked)
350 assert not l.locked()
351 l.acquire_nowait()
352 assert l.locked()
353 l.release()
354 assert not l.locked()
355 with assert_checkpoints():
356 await l.acquire()
357 assert l.locked()
358 l.release()
359 assert not l.locked()
360
361 l.acquire_nowait()
362 with pytest.raises(RuntimeError):
363 # Error out if we already own the lock
364 l.acquire_nowait()
365 l.release()
366 with pytest.raises(RuntimeError):
367 # Error out if we don't own the lock
368 l.release()
369
370 holder_task = None
371
372 async def holder() -> None:
373 nonlocal holder_task
374 holder_task = _core.current_task()
375 async with l:
376 await sleep_forever()
377
378 async with _core.open_nursery() as nursery:
379 assert not l.locked()
380 nursery.start_soon(holder)
381 await wait_all_tasks_blocked()
382 assert l.locked()
383 # WouldBlock if someone else holds the lock
384 with pytest.raises(_core.WouldBlock):
385 l.acquire_nowait()
386 # Can't release a lock someone else holds
387 with pytest.raises(RuntimeError):
388 l.release()
389
390 statistics = l.statistics()

Callers

nothing calls this directly

Calls 9

assert_checkpointsFunction · 0.85
wait_all_tasks_blockedFunction · 0.85
start_soonMethod · 0.80
cancelMethod · 0.80
lockedMethod · 0.45
acquire_nowaitMethod · 0.45
releaseMethod · 0.45
acquireMethod · 0.45
statisticsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…