Example #1
0
async def test_waiting_until_off_fails_when_not_turned_off():
    toggle = Toggle(True)
    with pytest.raises(asyncio.TimeoutError):
        async with async_timeout.timeout(0.1) as timeout:
            await toggle.wait_for(False)

    assert toggle.is_on()
    assert timeout.expired
Example #2
0
async def test_waiting_until_on_wakes_when_turned_on(timer):
    toggle = Toggle(False)

    async def delayed_turning_on(delay: float):
        await asyncio.sleep(delay)
        await toggle.turn_to(True)

    with timer:
        asyncio.create_task(delayed_turning_on(0.05))
        await toggle.wait_for(True)

    assert toggle.is_on()
    assert timer.seconds < 0.5  # approx. 0.05 plus some code overhead
Example #3
0
async def test_created_as_off():
    toggle = Toggle()
    assert not toggle.is_on()
    assert toggle.is_off()
Example #4
0
async def test_turning_off():
    toggle = Toggle(True)
    await toggle.turn_to(False)
    assert not toggle.is_on()
    assert toggle.is_off()
Example #5
0
async def test_initialised_as_on():
    toggle = Toggle(True)
    assert toggle.is_on()
    assert not toggle.is_off()
Example #6
0
async def test_waiting_until_off_fails_when_not_turned_off():
    toggle = Toggle(True)
    with pytest.raises(asyncio.TimeoutError):
        await asyncio.wait_for(toggle.wait_for(False), timeout=0.1)

    assert toggle.is_on()