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

Function test_Condition

src/trio/_tests/test_sync.py:410–491  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

408
409
410async def test_Condition() -> None:
411 with pytest.raises(TypeError):
412 Condition(Semaphore(1)) # type: ignore[arg-type]
413 with pytest.raises(TypeError):
414 Condition(StrictFIFOLock) # type: ignore[arg-type]
415 l = Lock() # noqa
416 c = Condition(l)
417 assert not l.locked()
418 assert not c.locked()
419 with assert_checkpoints():
420 await c.acquire()
421 assert l.locked()
422 assert c.locked()
423
424 c = Condition()
425 assert not c.locked()
426 c.acquire_nowait()
427 assert c.locked()
428 with pytest.raises(RuntimeError):
429 c.acquire_nowait()
430 c.release()
431
432 with pytest.raises(RuntimeError):
433 # Can't wait without holding the lock
434 await c.wait()
435 with pytest.raises(RuntimeError):
436 # Can't notify without holding the lock
437 c.notify()
438 with pytest.raises(RuntimeError):
439 # Can't notify without holding the lock
440 c.notify_all()
441
442 finished_waiters = set()
443
444 async def waiter(i: int) -> None:
445 async with c:
446 await c.wait()
447 finished_waiters.add(i)
448
449 async with _core.open_nursery() as nursery:
450 for i in range(3):
451 nursery.start_soon(waiter, i)
452 await wait_all_tasks_blocked()
453 async with c:
454 c.notify()
455 assert c.locked()
456 await wait_all_tasks_blocked()
457 assert finished_waiters == {0}
458 async with c:
459 c.notify_all()
460 await wait_all_tasks_blocked()
461 assert finished_waiters == {0, 1, 2}
462
463 finished_waiters = set()
464 async with _core.open_nursery() as nursery:
465 for i in range(3):
466 nursery.start_soon(waiter, i)
467 await wait_all_tasks_blocked()

Callers

nothing calls this directly

Calls 15

lockedMethod · 0.95
acquireMethod · 0.95
acquire_nowaitMethod · 0.95
releaseMethod · 0.95
waitMethod · 0.95
notifyMethod · 0.95
notify_allMethod · 0.95
statisticsMethod · 0.95
ConditionClass · 0.85
SemaphoreClass · 0.85
LockClass · 0.85
assert_checkpointsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…