def test_timeout(self): class Inc(Runnable): def next(self, state): return state.updated(cnt=state.cnt + 1) # timeout after exactly two runs with mock.patch('time.time', side_effect=itertools.count(), create=True): loop = LoopUntilNoImprovement(Inc(), max_time=2) state = loop.run(State(cnt=0)).result() self.assertEqual(state.cnt, 2) # timeout after the second run with mock.patch('time.time', side_effect=itertools.count(), create=True): loop = LoopUntilNoImprovement(Inc(), max_time=2.5) state = loop.run(State(cnt=0)).result() self.assertEqual(state.cnt, 3)
def test_decorator(self): with mock.patch('hybrid.profiling.perf_counter', side_effect=[0, 1]): def f(): pass deco = tictoc('f') ff = deco(f) ff() self.assertEqual(deco.tick, 0) self.assertEqual(deco.dt, 1)
def test_ctx_mgr(self): with mock.patch('hybrid.profiling.perf_counter', side_effect=[0, 1]): with tictoc() as t: pass self.assertEqual(t.tick, 0) self.assertEqual(t.dt, 1)