def test_smoke(self, sleep_fn, wakeup_fn): processor = autosuspend.Processor([_StubCheck('stub', None)], [], 2, 0, 0, sleep_fn, wakeup_fn, False) # should init the timestamp initially start = datetime.now(timezone.utc) processor.iteration(start, False) assert not sleep_fn.called # not yet reached processor.iteration(start + timedelta(seconds=1), False) assert not sleep_fn.called # time must be greater, not equal processor.iteration(start + timedelta(seconds=2), False) assert not sleep_fn.called # go to sleep processor.iteration(start + timedelta(seconds=3), False) assert sleep_fn.called assert sleep_fn.call_arg is None sleep_fn.reset() # second iteration to check that the idle time got reset processor.iteration(start + timedelta(seconds=4), False) assert not sleep_fn.called # go to sleep again processor.iteration(start + timedelta(seconds=6, milliseconds=2), False) assert sleep_fn.called assert wakeup_fn.call_arg is None
def test_wakeup_delta_applied(self, mocker, sleep_fn, wakeup_fn): start = datetime.now(timezone.utc) wakeup = mocker.MagicMock(spec=autosuspend.Wakeup) wakeup.check.return_value = start + timedelta(seconds=25) processor = autosuspend.Processor([_StubCheck('stub', None)], [wakeup], 2, 10, 4, sleep_fn, wakeup_fn, False) # init iteration processor.iteration(start, False) # no activity and enough time passed to start sleeping processor.iteration(start + timedelta(seconds=3), False) assert sleep_fn.called assert wakeup_fn.call_arg == start + timedelta(seconds=21)
def test_wakeup_blocks_sleep(self, mocker, sleep_fn, wakeup_fn) -> None: start = datetime.now(timezone.utc) wakeup = mocker.MagicMock(spec=autosuspend.Wakeup) wakeup.check.return_value = start + timedelta(seconds=6) processor = autosuspend.Processor([_StubCheck("stub", None)], [wakeup], 2, 3.1, 0, sleep_fn, wakeup_fn, False) # init iteration processor.iteration(start, False) # no activity and enough time passed to start sleeping processor.iteration(start + timedelta(seconds=3), False) assert not sleep_fn.called assert wakeup_fn.call_arg is None
def test_wakeup_scheduled(self, mocker, sleep_fn, wakeup_fn) -> None: start = datetime.now(timezone.utc) wakeup = mocker.MagicMock(spec=autosuspend.Wakeup) wakeup.check.return_value = start + timedelta(seconds=25) processor = autosuspend.Processor([_StubCheck("stub", None)], [wakeup], 2, 10, 0, sleep_fn, wakeup_fn, False) # init iteration processor.iteration(start, False) # no activity and enough time passed to start sleeping processor.iteration(start + timedelta(seconds=3), False) assert sleep_fn.called assert sleep_fn.call_arg == start + timedelta(seconds=25) assert wakeup_fn.call_arg == start + timedelta(seconds=25) sleep_fn.reset() wakeup_fn.reset() # ensure that wake up is not scheduled again processor.iteration(start + timedelta(seconds=25), False) assert wakeup_fn.call_arg is None
def test_just_woke_up_handling(self, sleep_fn, wakeup_fn): processor = autosuspend.Processor([_StubCheck('stub', None)], [], 2, 0, 0, sleep_fn, wakeup_fn, False) # should init the timestamp initially start = datetime.now(timezone.utc) processor.iteration(start, False) assert not sleep_fn.called # should go to sleep but we just woke up processor.iteration(start + timedelta(seconds=3), True) assert not sleep_fn.called # start over again processor.iteration(start + timedelta(seconds=4), False) assert not sleep_fn.called # not yet sleeping processor.iteration(start + timedelta(seconds=6), False) assert not sleep_fn.called # now go to sleep processor.iteration(start + timedelta(seconds=7), False) assert sleep_fn.called assert wakeup_fn.call_arg is None