Пример #1
0
async def test_workqueue_deduplicates():
    q = WorkQueue()

    q.put("foo")
    q.put("bar")
    q.put("foo")

    assert await q.get() == "foo"
    assert await q.get() == "bar"
    assert q.is_empty()
Пример #2
0
async def test_workqueue_close():
    q = WorkQueue()
    q.put("foo")
    q.close()
    with pytest.raises(WorkQueueClosed):
        await q.get()

    assert q.closed

    q = WorkQueue()

    fs = [asyncio.ensure_future(q.get()) for _ in range(3)]
    done, pending = await asyncio.wait(fs, timeout=0.001)
    assert not done

    q.close()

    for f in fs:
        with pytest.raises(WorkQueueClosed):
            await f
Пример #3
0
async def test_workqueue_put_after():
    q = WorkQueue()

    # Fine if already enqueued
    q.put("foo")
    q.put_after("foo", 0.001)
    assert len(q._queue) == 1
    assert q._timers

    assert await q.get() == "foo"

    # Cannot concurrently process "foo", even though it's scheduled
    f = asyncio.ensure_future(q.get())
    done, pending = await asyncio.wait([f], timeout=0.005)
    assert not done

    # But after marking done, can continue processing
    q.task_done("foo")
    assert q._queue
    res = await f
    assert res == "foo"
Пример #4
0
async def test_workqueue_cancellable():
    q = WorkQueue()

    # No items, blocks
    f = asyncio.ensure_future(q.get())
    done, pending = await asyncio.wait([f], timeout=0.001)
    assert not done

    # Cancel the get task, waiter cleaned up
    await cancel_task(f)
    assert not q._waiting

    # No items, blocks
    f = asyncio.ensure_future(q.get())
    done, pending = await asyncio.wait([f], timeout=0.001)
    assert not done

    # Cancel after a put, item still available
    q.put("foo")
    await cancel_task(f)
    assert await q.get() == "foo"
Пример #5
0
async def test_workqueue_no_concurrent_execution():
    q = WorkQueue()

    q.put("foo")

    assert await q.get() == "foo"
    assert q.is_empty()
    q.put("foo")
    assert not q.is_empty()
    assert not q._queue
    assert "foo" in q._dirty

    # Cannot concurrently process "foo", even though it's requeued
    f = asyncio.ensure_future(q.get())
    done, pending = await asyncio.wait([f], timeout=0.001)
    assert not done

    # But after marking done, can continue processing
    q.task_done("foo")
    assert q._queue
    res = await f
    assert res == "foo"