async def test_statistics(self): async def waiter(): async with condition: await condition.wait() condition = create_condition() async with create_task_group() as tg: assert not condition.statistics().lock_statistics.locked assert condition.statistics().tasks_waiting == 0 async with condition: assert condition.statistics().lock_statistics.locked assert condition.statistics().tasks_waiting == 0 for i in range(1, 3): tg.spawn(waiter) await wait_all_tasks_blocked() assert condition.statistics().tasks_waiting == i for i in range(1, -1, -1): async with condition: condition.notify(1) await wait_all_tasks_blocked() assert condition.statistics().tasks_waiting == i assert not condition.statistics().lock_statistics.locked assert condition.statistics().tasks_waiting == 0
async def test_acquire_nowait_wouldblock(self): async def try_lock(): pytest.raises(WouldBlock, condition.acquire_nowait) condition = create_condition() async with condition, create_task_group() as tg: assert condition.locked() tg.spawn(try_lock)
async def test_condition(self): async def notifier(): async with condition: await condition.notify_all() condition = create_condition() async with create_task_group() as tg: async with condition: assert condition.locked() await tg.spawn(notifier) await condition.wait()
async def test_manual_acquire(self): async def notifier(): await condition.acquire() try: condition.notify_all() finally: condition.release() condition = create_condition() async with create_task_group() as tg: await condition.acquire() try: assert condition.locked() tg.spawn(notifier) await condition.wait() finally: condition.release()
async def main(): condition = create_condition() async with create_task_group() as tg: for tasknum in range(6): await tg.spawn(listen, tasknum, condition) await sleep(1) async with condition: await condition.notify(1) await sleep(1) async with condition: await condition.notify(2) await sleep(1) async with condition: await condition.notify_all()
async def test_wait_cancel(self): async def task(): nonlocal task_started, notified task_started = True async with condition: event.set() await condition.wait() notified = True task_started = notified = False event = create_event() condition = create_condition() async with create_task_group() as tg: tg.spawn(task) await event.wait() await wait_all_tasks_blocked() tg.cancel_scope.cancel() assert task_started assert not notified
async def test_acquire_nowait(self): condition = create_condition() condition.acquire_nowait() assert condition.locked()
async def test_condition_release(self): condition = create_condition() condition.acquire_nowait() with pytest.deprecated_call(): await condition.release()