async def test_waiting_until_on_fails_when_not_turned_on(): toggle = Toggle(False) with pytest.raises(asyncio.TimeoutError): async with async_timeout.timeout(0.1) as timeout: await toggle.wait_for(True) assert toggle.is_off() assert timeout.expired
async def test_waiting_until_off_wakes_when_turned_off(timer): toggle = Toggle(True) async def delayed_turning_off(delay: float): await asyncio.sleep(delay) await toggle.turn_to(False) with timer: asyncio.create_task(delayed_turning_off(0.05)) await toggle.wait_for(False) assert toggle.is_off() assert timer.seconds < 0.5 # approx. 0.05 plus some code overhead
async def test_created_as_off(): toggle = Toggle() assert not toggle.is_on() assert toggle.is_off()
async def test_turning_off(): toggle = Toggle(True) await toggle.turn_to(False) assert not toggle.is_on() assert toggle.is_off()
async def test_initialised_as_on(): toggle = Toggle(True) assert toggle.is_on() assert not toggle.is_off()
async def test_waiting_until_on_fails_when_not_turned_on(): toggle = Toggle(False) with pytest.raises(asyncio.TimeoutError): await asyncio.wait_for(toggle.wait_for(True), timeout=0.1) assert toggle.is_off()