Beispiel #1
0
async def test_restart(loop):
    counter = 0

    def task():
        nonlocal counter
        counter += 1

    cron = CronCallback(task)
    cron.start("* * * * * *", loop)

    await asyncio.sleep(2)
    cron.stop()

    assert counter == 2

    await asyncio.sleep(2)

    assert counter == 2

    cron.start("* * * * * *", loop)

    await asyncio.sleep(2)
    cron.stop()

    assert counter == 4

    await asyncio.sleep(2)

    assert counter == 4
Beispiel #2
0
async def test_shield(loop):
    counter = 0
    condition = asyncio.Condition()

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

    cron = CronCallback(task)
    cron.start("* * * * * *", loop, shield=True)

    # Wait for cron callback to start
    async with condition:
        await condition.wait_for(lambda: counter >= 1)

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

    await asyncio.sleep(1.5)

    # Shielded
    assert counter == 2
Beispiel #3
0
async def test_long_func(loop):
    counter = 0

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

    cron = CronCallback(task)
    cron.start("* * * * * *", loop)

    await asyncio.sleep(1.5)
    cron.stop()

    assert counter == 1
Beispiel #4
0
async def test_cron(loop):
    counter = 0

    def task():
        nonlocal counter
        counter += 1

    cron = CronCallback(task)
    cron.start("* * * * * *", loop)

    await asyncio.sleep(2)
    with pytest.raises(asyncio.CancelledError):
        await cron.stop()

    assert counter == 2

    await asyncio.sleep(2)

    assert counter == 2
Beispiel #5
0
async def test_shield(loop):
    counter = 0

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

    cron = CronCallback(task)
    cron.start("* * * * * *", loop, shield=True)

    # Wait for cron callback to start
    while cron.task is None:
        await asyncio.sleep(0.1)

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

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

    # Shielded
    assert counter == 1

    # No shield
    counter = 0
    cron = CronCallback(task)
    cron.start("* * * * * *", loop, shield=False)

    # Wait for cron callback to start
    while cron.task is None:
        await asyncio.sleep(0.1)

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

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

    # Cancelled
    assert counter == 0
Beispiel #6
0
async def test_long_func(loop):
    counter = 0
    condition = asyncio.Condition()

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

    cron = CronCallback(task)
    cron.start("* * * * * *", loop)

    async with condition:
        await condition.wait_for(lambda: counter >= 1)

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

    assert counter == 1
Beispiel #7
0
async def test_restart(loop):
    counter = 0
    condition = asyncio.Condition()

    async def task():
        nonlocal counter, condition
        counter += 1
        async with condition:
            condition.notify_all()

    cron = CronCallback(task)
    cron.start("* * * * * *", loop)

    async with condition:
        await condition.wait_for(lambda: counter >= 2)

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

    assert counter == 2

    await asyncio.sleep(2)

    assert counter == 2

    cron.start("* * * * * *", loop)

    async with condition:
        await condition.wait_for(lambda: counter >= 4)

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

    assert counter == 4

    await asyncio.sleep(2)

    assert counter == 4
Beispiel #8
0
async def test_tz_next(loop, now):
    cron = CronCallback(lambda: True)
    with freeze_time(now):
        cron.start("*/10 * * * *", loop)
        expected = timedelta(minutes=10).total_seconds()
        assert 0 <= cron.get_current() - loop.time() <= expected