async def test_acquire_nowait_wouldblock(self): async def try_lock(): pytest.raises(WouldBlock, lock.acquire_nowait) lock = Lock() async with lock, create_task_group() as tg: assert lock.locked() tg.start_soon(try_lock)
async def test_contextmanager(self): async def task(): assert lock.locked() async with lock: results.append('2') results = [] lock = Lock() async with create_task_group() as tg: async with lock: tg.start_soon(task) await wait_all_tasks_blocked() results.append('1') assert not lock.locked() assert results == ['1', '2']
async def test_contextmanager(self) -> None: async def task() -> None: assert lock.locked() async with lock: results.append("2") results = [] lock = Lock() async with create_task_group() as tg: async with lock: tg.start_soon(task) await wait_all_tasks_blocked() results.append("1") assert not lock.locked() assert results == ["1", "2"]
async def test_manual_acquire(self): async def task(): assert lock.locked() await lock.acquire() try: results.append('2') finally: lock.release() results = [] lock = Lock() async with create_task_group() as tg: await lock.acquire() try: tg.start_soon(task) await wait_all_tasks_blocked() results.append('1') finally: lock.release() assert not lock.locked() assert results == ['1', '2']
async def test_asyncio_deadlock(self) -> None: """Regression test for #398.""" lock = Lock() async def acquire() -> None: async with lock: await asyncio.sleep(0) loop = asyncio.get_event_loop() task1 = loop.create_task(acquire()) task2 = loop.create_task(acquire()) await asyncio.sleep(0) task1.cancel() await asyncio.wait_for(task2, 1)
async def test_cancel(self): async def task(): nonlocal task_started, got_lock task_started = True async with lock: got_lock = True task_started = got_lock = False lock = Lock() async with create_task_group() as tg: async with lock: tg.start_soon(task) tg.cancel_scope.cancel() assert task_started assert not got_lock
async def test_statistics(self): async def waiter(): async with lock: pass lock = Lock() async with create_task_group() as tg: assert not lock.statistics().locked assert lock.statistics().tasks_waiting == 0 async with lock: assert lock.statistics().locked assert lock.statistics().tasks_waiting == 0 for i in range(1, 3): tg.start_soon(waiter) await wait_all_tasks_blocked() assert lock.statistics().tasks_waiting == i assert not lock.statistics().locked assert lock.statistics().tasks_waiting == 0
async def test_cancel_during_acquire(self, release_first: bool) -> None: acquired = False async def task(*, task_status: TaskStatus) -> None: nonlocal acquired task_status.started() async with lock: acquired = True lock = Lock() async with create_task_group() as tg: await lock.acquire() await tg.start(task) tg.cancel_scope.cancel() with CancelScope(shield=True): if release_first: lock.release() await wait_all_tasks_blocked() else: await wait_all_tasks_blocked() lock.release() assert not acquired assert not lock.locked()
async def test_acquire_nowait(self): lock = Lock() lock.acquire_nowait() assert lock.locked()
async def test_lock_release(self) -> None: lock = Lock() lock.acquire_nowait() with pytest.deprecated_call(): await lock.release()