async def test_event_update(u: UpdatedTime): m = MagicMock() u.set(pd_now(UTC)) list = HABApp.core.EventBusListener( 'test', HABApp.core.WrappedFunction(m, name='MockFunc')) HABApp.core.EventBus.add_listener(list) u.set(pd_now(UTC)) await asyncio.sleep(1) m.assert_not_called() await asyncio.sleep(0.1) m.assert_called_once() c = m.call_args[0][0] assert isinstance(c, HABApp.core.events.ItemNoUpdateEvent) assert c.name == 'test' assert c.seconds == 1 await asyncio.sleep(2) assert m.call_count == 2 c = m.call_args[0][0] assert isinstance(c, HABApp.core.events.ItemNoUpdateEvent) assert c.name == 'test' assert c.seconds == 3 list.cancel()
def u(): a = UpdatedTime('test', pd_now(UTC)) w1 = a.add_watch(1) w2 = a.add_watch(3) yield a # cancel the rest of the running tasks w1.cancel() w2.cancel()
def u(): a = UpdatedTime('test', datetime.now(tz=pytz.utc)) w1 = a.add_watch(1) w2 = a.add_watch(3) yield a # cancel the rest of the running tasks w1.cancel() w2.cancel()
async def test_cancel_running(u: UpdatedTime): u.set(datetime.now(tz=pytz.utc)) w1 = u.tasks[0] w2 = u.tasks[1] await asyncio.sleep(1.1) assert w1._task.done() assert not w2._task.done() assert w2 in u.tasks w2.cancel() u.set(datetime.now(tz=pytz.utc)) await asyncio.sleep(0.05) assert w2 not in u.tasks
async def test_cancel_running(u: UpdatedTime): u.set(pd_now(UTC)) w1 = u.tasks[0] w2 = u.tasks[1] await asyncio.sleep(1.1) assert w1.fut.task.done() assert not w2.fut.task.done() assert w2 in u.tasks w2.cancel() await asyncio.sleep(0.05) u.set(pd_now(UTC)) await asyncio.sleep(0.05) assert w2 not in u.tasks
def test_sec_timedelta(): a = UpdatedTime('test', pd_now(UTC)) w1 = a.add_watch(1) # We return the same object because it is the same time assert w1 is a.add_watch(timedelta(seconds=1)) w2 = a.add_watch(timedelta(seconds=3)) assert w2.fut.secs == 3 w3 = a.add_watch(timedelta(minutes=3)) assert w3.fut.secs == 3 * 60 w1.cancel() w2.cancel() w3.cancel()