async def test_waiting_until_off_wakes_when_turned_off(timer): toggleset = ToggleSet() toggle = await toggleset.make_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 asyncio.wait_for(toggleset.wait_for(False), timeout=1.0) assert toggleset.is_off() assert timer.seconds < 0.5 # approx. 0.05 plus some code overhead
async def test_waiting_until_off_fails_when_not_turned_off(): toggleset = ToggleSet() await toggleset.make_toggle(True) with pytest.raises(asyncio.TimeoutError): await asyncio.wait_for(toggleset.wait_for(False), timeout=0.1) assert toggleset.is_on()