예제 #1
0
async def test_first_task_rescheduling():
    s = AsyncScheduler()

    t0 = time()
    times = []

    wait_orig = s._event.wait

    async def wait_logging():
        try:
            rv = await wait_orig()
        finally:
            times.append(time() - t0)
        return rv

    s._event.wait = wait_logging
    s.EMPTY_QUEUE_TIMEOUT = 0.1

    async def noop():
        pass

    await s.enter(0.4, noop)
    t = create_task(s.run())
    await s.enter(0.6, None)  # this task doesn't trigger a reschedule
    await asyncio.sleep(0.1)
    await s.enter(0.1, noop)  # this triggers a reschedule
    await asyncio.gather(t)
    times.append(time() - t0)
    for got, want in zip(times, [0.1, 0.2, 0.4, 0.6, 0.6]):
        assert got == pytest.approx(want, 0.1), times
예제 #2
0
async def test_empty_queue_timeout():
    s = AsyncScheduler()

    t0 = time()
    times = []

    wait_orig = s._event.wait

    async def wait_logging():
        try:
            rv = await wait_orig()
        finally:
            times.append(time() - t0)
        return rv

    s._event.wait = wait_logging
    s.EMPTY_QUEUE_TIMEOUT = 0.2

    t = create_task(s.run())
    await asyncio.sleep(0.5)
    await s.enter(0.5, None)
    await asyncio.gather(t)
    times.append(time() - t0)
    for got, want in zip(times, [0.2, 0.4, 0.5, 1.0]):
        assert got == pytest.approx(want, 0.1), times