def test_fast_forward_delay(self): """Test that fast forwarding the time works properly""" notify_event = threading.Event() virtualtime.notify_on_change(notify_event) delay_event = threading.Event() offsets = [] positions = ["start_job", ""] msg_dict = {'offsets': offsets, 'positions': positions} catcher_thread = threading.Thread(target=self.fast_forward_delayer, args=(notify_event, delay_event, msg_dict)) catcher_thread.start() start_time = virtualtime._original_time() virtualtime.fast_forward_time(2) assert virtualtime._time_offset == 2 virtualtime.restore_time() msg_dict['stop'] = True notify_event.set() catcher_thread.join() completion_time = virtualtime._original_time() assert offsets[:3] == [1.0, 2.0, 0] # depends on how long the stop event takes? assert (not offsets[3:]) or offsets[3:] == [0] assert completion_time - start_time < 0.2 assert delay_event.is_set()
def test_fast_forward_time(self): """Test that fast forwarding the time works properly""" event = threading.Event() virtualtime.notify_on_change(event) offsets = [] msg_dict = {'offsets': offsets} catcher_thread = threading.Thread(target=self.fast_forward_catcher, args=(event, msg_dict)) catcher_thread.start() start_time = virtualtime._original_time() virtualtime.fast_forward_time(1) assert virtualtime._time_offset == 1 virtualtime.fast_forward_time(2.5) assert virtualtime._time_offset == 3.5 virtualtime.fast_forward_time(target=start_time + 9.1, step_size=2.0) assert 9 <= virtualtime._time_offset <= 9.2 virtualtime.restore_time() virtualtime.fast_forward_time(-1.3, step_size=0.9) virtualtime.restore_time() msg_dict['stop'] = True event.set() catcher_thread.join() assert offsets[:6] == [1.0, 2.0, 3.0, 3.5, 5.5, 7.5] assert 9 <= offsets[6] <= 9.2 assert offsets[7:11] == [0, -0.9, -1.3, 0] # depends on how long the stop event takes? assert (not offsets[11:]) or offsets[11:] == [0]
def test_fast_forward_time_long(self): """Test that fast forwarding the time a long way works properly""" event = threading.Event() virtualtime.notify_on_change(event) offsets = [] msg_dict = {'offsets': offsets} catcher_thread = threading.Thread(target=self.fast_forward_catcher, args=(event, msg_dict)) catcher_thread.start() start_time = virtualtime._original_time() virtualtime.fast_forward_time(1000, step_size=1) virtualtime.restore_time() msg_dict['stop'] = True event.set() catcher_thread.join() assert offsets == list(range(1, 1001)) + [0]
def finish_wait(self, thread, error_list, expected_sleep=0): """Waits for the thread to finish, checks for any errors in the given list. expected_sleep says how long we should have to wait for this...""" if expected_sleep: virtualtime.fast_forward_time(expected_sleep) super(TestVirtualTimer, self).finish_wait(thread, error_list, expected_sleep)
def sleep(self, seconds): virtualtime.fast_forward_time(seconds)