Exemplo n.º 1
0
def test_cancel_recursively():
    done_order = []

    fut1 = Future()
    fut2 = Future()

    async def some_recursion(n=10):
        if not n:
            await fut1
        else:
            await some_recursion(n - 1)

    async def entry():
        await some_recursion()
        await fut2

    sched = Scheduler()
    ef = sched.run(entry())

    fut1.add_done_callback(done_order.append)
    ef.add_done_callback(done_order.append)

    sched.tick()
    ef.cancel()
    sched.tick()
    assert fut1.cancelled()
    assert ef.cancelled()
    assert done_order == [fut1, ef]
    assert not fut2.cancelled()
Exemplo n.º 2
0
def test_cancel_all_tasks():
    async def entry():
        await suspend()
        assert False

    sched = Scheduler()
    sched.run(entry())
    sched.run(entry())
    sched.run(entry())
    sched.cancel_all_tasks()
    sched.tick()
Exemplo n.º 3
0
def test_cancel_self_late():
    async def entry():
        await suspend()
        future.cancel()

    sched = Scheduler()
    future = sched.run(entry())
    sched.tick()
    assert future.cancelled()
    with pytest.raises(CancelledError):
        future.result()
Exemplo n.º 4
0
def test_gather():
    fut1 = Future()
    fut2 = Future()
    fut3 = Future()
    sched = Scheduler()
    gf = gather(fut1, fut2, fut3, scheduler=sched)
    assert not gf.done()
    fut1.set_result(1)
    assert not gf.done()
    fut3.set_result(3)
    fut2.set_result(2)
    assert not gf.done()
    sched.tick()
    assert gf.done()
    assert gf.result() == [1, 3, 2]
Exemplo n.º 5
0
def test_gather_cancel():
    """
    Check: If a gather() future is cancelled, all source futures are cancelled.
    """
    fut1 = Future()
    fut2 = Future()
    fut3 = Future()
    sched = Scheduler()
    gf = gather(fut1, fut2, fut3, scheduler=sched)
    assert not gf.done()
    gf.cancel()
    sched.tick()
    assert fut1.cancelled()
    assert fut2.cancelled()
    assert fut3.cancelled()
    assert gf.cancelled()
Exemplo n.º 6
0
def test_cancel_done_callback():
    done_called_by = None

    def done(fut):
        nonlocal done_called_by
        done_called_by = fut

    async def entry():
        await suspend()

    sched = Scheduler()
    ef = sched.run(entry())
    ef.add_done_callback(done)
    ef.cancel()
    sched.tick()
    assert done_called_by == ef
Exemplo n.º 7
0
def test_gather_exc():
    fut1 = Future()
    fut2 = Future()
    fut3 = Future()
    sched = Scheduler()
    gf = gather(fut1, fut2, fut3, scheduler=sched)
    assert not gf.done()
    fut1.set_result(1)
    assert not gf.done()
    fut3.set_exception(KeyError)
    assert not gf.done()
    sched.tick()
    assert gf.done()
    # fut3 failed, so the other pending futures should enter cancelled state.
    assert fut2.cancelled()
    with pytest.raises(KeyError):
        gf.result()
Exemplo n.º 8
0
def test_loop():
    f = Future()

    def get_response():
        return f

    async def entry():
        return await get_response()

    sched = Scheduler()
    entry_future = sched.run(entry())

    sched.tick()
    assert not entry_future.done()
    f.set_result(666)
    assert not entry_future.done()
    sched.tick()
    assert entry_future.done()
    assert entry_future.result() == 666
Exemplo n.º 9
0
def test_loop_exc():
    f = Future()

    def get_response():
        return f

    async def entry():
        return await get_response()

    sched = Scheduler()
    entry_future = sched.run(entry())

    sched.tick()
    assert not entry_future.done()
    f.set_exception(KeyError)
    assert not entry_future.done()
    sched.tick()
    assert entry_future.done()
    with pytest.raises(KeyError):
        entry_future.result()
Exemplo n.º 10
0
def test_as_completed_cancel():
    """
    Check: If a source future to as_completed() completes after as_completed() is cancelled,
           no crash occurs and the source future completion is ignored.
    """
    source_fut = Future()
    sched = Scheduler()
    ac = as_completed(source_fut, scheduler=sched)

    as_completed_fut = sched.run(ac.__anext__())
    sched.tick()
    as_completed_fut.cancel()
    sched.tick()
    with pytest.raises(CancelledError):
        sched.run_until_complete(as_completed_fut)
    source_fut.set_exception(KeyError)
    sched.tick()
Exemplo n.º 11
0
    return future


async def waiterA():
    for n in range(5):
        await wait_for(500)
        print('waiterA', n)


async def waiterB():
    for n in range(10):
        await wait_for(100)
        print('waiterB', n)


async def waiterC():
    await wait_for(3000)
    print('Quitting.')
    quit()


scheduler = Scheduler()
scheduler.run(waiterA())
scheduler.run(waiterB())
scheduler.run(waiterC())

# This is the "event loop"
while not done:
    scheduler.tick()
    app.processEvents(QEventLoop.AllEvents | QEventLoop.WaitForMoreEvents)