コード例 #1
0
ファイル: test_periodic.py プロジェクト: MrPainter/aiomisc
async def test_shield(loop):
    counter = 0

    async def task():
        nonlocal counter
        counter += 1
        await asyncio.sleep(2)

    periodic = aiomisc.PeriodicCallback(task)
    periodic.start(0.1, loop, shield=True)

    await asyncio.sleep(0)

    with pytest.raises(asyncio.CancelledError):
        await periodic.stop()

    assert counter == 1

    # No shield
    counter = 0
    periodic = aiomisc.PeriodicCallback(task)
    periodic.start(0.1, loop, shield=False)

    await asyncio.sleep(0)

    with pytest.raises(asyncio.CancelledError):
        await periodic.stop()

    assert counter == 0
コード例 #2
0
async def test_restart(loop):
    counter = 0

    def task():
        nonlocal counter
        counter += 1

    periodic = aiomisc.PeriodicCallback(task)
    periodic.start(0.1, loop)

    await asyncio.sleep(0.5)
    periodic.stop()

    assert 4 < counter < 7

    await asyncio.sleep(0.5)

    assert 4 < counter < 7

    periodic.start(0.1, loop)

    await asyncio.sleep(0.5)
    periodic.stop()

    assert 8 < counter < 14

    await asyncio.sleep(0.5)

    assert 8 < counter < 14
コード例 #3
0
async def test_cancelled_callback(loop):
    counter = 0
    condition = asyncio.Condition()

    async def task():
        nonlocal counter
        async with condition:
            counter += 1
            condition.notify_all()
        raise asyncio.CancelledError

    periodic = aiomisc.PeriodicCallback(task)

    for i in (5, 10, 15):
        periodic.start(0.1, loop)

        async with condition:
            await asyncio.wait_for(
                condition.wait_for(lambda: counter == i),
                timeout=5,
            )

        with pytest.raises(asyncio.CancelledError):
            await periodic.stop()

        assert counter == i
コード例 #4
0
async def test_shield(loop):
    counter = 0

    async def task():
        nonlocal counter
        await asyncio.sleep(0.1)
        counter += 1

    periodic = aiomisc.PeriodicCallback(task)
    periodic.start(0.2, loop, shield=True)

    # Wait for periodic callback to start
    await asyncio.sleep(0.01)

    with pytest.raises(asyncio.CancelledError):
        await periodic.stop()

    # Wait for counter to increment
    await asyncio.sleep(0.1)

    # Shielded
    assert counter == 1

    # No shield
    counter = 0
    periodic = aiomisc.PeriodicCallback(task)
    periodic.start(0.2, loop, shield=False)

    # Wait for periodic callback to start
    await asyncio.sleep(0.01)

    with pytest.raises(asyncio.CancelledError):
        await periodic.stop()

    # Wait for counter to increment
    await asyncio.sleep(0.1)

    # Cancelled
    assert counter == 0
コード例 #5
0
async def test_long_func(loop):
    counter = 0

    async def task():
        nonlocal counter
        counter += 1
        await asyncio.sleep(0.5)

    periodic = aiomisc.PeriodicCallback(task)
    periodic.start(0.1, loop)

    await asyncio.sleep(1)
    periodic.stop()

    assert 1 < counter < 3
コード例 #6
0
async def test_delay(loop):
    counter = 0

    def task():
        nonlocal counter
        counter += 1

    periodic = aiomisc.PeriodicCallback(task)
    periodic.start(0.1, loop, delay=0.5)

    await asyncio.sleep(0.25)

    assert not counter

    await asyncio.sleep(0.5)

    periodic.stop()

    assert 1 < counter < 4
コード例 #7
0
async def test_delay(loop):
    counter = 0

    def task():
        nonlocal counter
        counter += 1

    periodic = aiomisc.PeriodicCallback(task)
    periodic.start(0.1, loop, delay=0.5)

    await asyncio.sleep(0.25)

    assert not counter

    await asyncio.sleep(0.5)

    with pytest.raises(asyncio.CancelledError):
        await periodic.stop()

    assert 1 < counter <= 4
コード例 #8
0
async def test_periodic(loop):
    condition = asyncio.Condition()
    counter = 0

    async def task():
        nonlocal counter
        counter += 1

        async with condition:
            condition.notify_all()

    periodic = aiomisc.PeriodicCallback(task)
    periodic.start(0.1, loop)

    async with condition:
        await asyncio.wait_for(
            condition.wait_for(lambda: counter >= 5),
            timeout=5,
        )

    with pytest.raises(asyncio.CancelledError):
        await periodic.stop()
コード例 #9
0
async def test_long_func(loop):
    counter = 0
    condition = asyncio.Condition()

    async def task():
        nonlocal counter
        async with condition:
            counter += 1
            await asyncio.sleep(0.5)
            condition.notify_all()

    periodic = aiomisc.PeriodicCallback(task)
    periodic.start(0.1, loop)

    await asyncio.sleep(1)
    with pytest.raises(asyncio.CancelledError):
        await periodic.stop()

    async with condition:
        await asyncio.wait_for(
            condition.wait_for(lambda: counter == 2),
            timeout=2,
        )