예제 #1
0
 async def waiter():
     try:
         async with timeout(2) as t:
             outer_task.cancel()
             await inner_task
     finally:
         assert not t.expired
         assert t._waiter is None
예제 #2
0
async def test_timeout():
    async def inner():
        try:
            await asyncio.sleep(2)
        except asyncio.CancelledError:
            raise
        else:
            assert False, "Not cancelled"

    with pytest.raises(asyncio.TimeoutError):
        async with timeout(0.01) as t:
            await inner()
    assert t.expired
예제 #3
0
async def test_timeout_supports_cancellation():
    # Unrelated CancelledError raises properly
    with pytest.raises(asyncio.CancelledError):
        async with timeout(0.01) as t:
            raise asyncio.CancelledError
    assert t._waiter is None
    assert not t.expired

    # Cancelling an outer task cancels the inner and clears the timeout
    inner_task = asyncio.ensure_future(asyncio.sleep(2))

    async def waiter():
        try:
            async with timeout(2) as t:
                outer_task.cancel()
                await inner_task
        finally:
            assert not t.expired
            assert t._waiter is None

    outer_task = asyncio.ensure_future(waiter())
    with pytest.raises(asyncio.CancelledError):
        await outer_task
    assert inner_task.cancelled()
예제 #4
0
async def test_timeout_forwards_exceptions():
    with pytest.raises(Exception):
        async with timeout(0.01) as t:
            raise Exception
    assert t._waiter is None
    assert not t.expired