Ejemplo n.º 1
0
    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
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
    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()
Ejemplo n.º 4
0
    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()
Ejemplo n.º 5
0
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()
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
 async def test_acquire_nowait(self):
     condition = create_condition()
     condition.acquire_nowait()
     assert condition.locked()
Ejemplo n.º 8
0
 async def test_condition_release(self):
     condition = create_condition()
     condition.acquire_nowait()
     with pytest.deprecated_call():
         await condition.release()