def _task_receiver(self, state, details): task_name = details['task_name'] if state == states.PENDING: self._timers.pop(task_name, None) elif state in STARTING_STATES: self._timers[task_name] = misc.StopWatch() self._timers[task_name].start() elif state in FINISHED_STATES: if task_name in self._timers: self._record_ending(self._timers[task_name], task_name)
def test_pause_resume(self): watch = misc.StopWatch() watch.start() time.sleep(0.05) watch.stop() elapsed = watch.elapsed() time.sleep(0.05) self.assertAlmostEqual(elapsed, watch.elapsed()) watch.resume() self.assertNotEqual(elapsed, watch.elapsed())
def __init__(self, task, uuid, action, arguments, progress_callback, timeout, **kwargs): self._task = task self._name = reflection.get_class_name(task) self._uuid = uuid self._action = action self._event = pr.ACTION_TO_EVENT[action] self._arguments = arguments self._progress_callback = progress_callback self._kwargs = kwargs self._watch = misc.StopWatch(duration=timeout).start() self._state = pr.PENDING self.result = futures.Future()
def test_context_manager(self): with misc.StopWatch() as watch: time.sleep(0.05) self.assertGreater(0.01, watch.elapsed())
def test_elapsed(self): watch = misc.StopWatch() watch.start() time.sleep(0.2) # NOTE(harlowja): Allow for a slight variation by using 0.19. self.assertGreaterEqual(0.19, watch.elapsed())
def test_no_expiry(self): watch = misc.StopWatch(0.1) watch.start() self.assertFalse(watch.expired())
def test_expiry(self): watch = misc.StopWatch(0.1) watch.start() time.sleep(0.2) self.assertTrue(watch.expired())
def test_no_states(self): watch = misc.StopWatch() self.assertRaises(RuntimeError, watch.stop) self.assertRaises(RuntimeError, watch.resume)