Пример #1
0
 def test_callback(self):
     self.notify_event = threading.Event()
     virtualtime.notify_on_change(self.notify_event)
     self.callback_stop = False
     self.callback_event = threading.Event()
     self.callback_logs = []
     self.callback_missed = []
     ct = threading.Thread(target=self.callback_thread)
     ct.start()
     virtualtime.wait_for_callback_on_change(self.callback_event)
     try:
         start_time = virtualtime._original_time()
         virtualtime.set_offset(1)
         assert len(self.callback_logs) == 1 and not self.callback_missed
         assert self.callback_logs[0][1:] == (1, False)
         offset_time = virtualtime._original_time()
         assert offset_time - start_time < 0.1
         virtualtime.set_time(0)
         assert len(self.callback_logs) == 2 and not self.callback_missed
         assert self.callback_logs[1][1] < -start_time + 1 and self.callback_logs[1][2] is False
         set_time = virtualtime._original_time()
         assert set_time - offset_time < 0.1
         virtualtime.restore_time()
         assert len(self.callback_logs) == 3 and not self.callback_missed
         assert self.callback_logs[1][1] < -start_time + 1 and self.callback_logs[1][2] is False
         restore_time = virtualtime._original_time()
         assert restore_time - set_time < 0.1
     finally:
         # deleting this should ensure it drops out of the weak set and doesn't hang things up later...
         del self.callback_event
         self.callback_stop = True
         self.notify_event.set()
         ct.join()
Пример #2
0
 def test_parallel_sleeps(self):
     """Tests that sleep comes back quicker than normal when time is advanced, and that this works with lots of threads"""
     first_time = virtualtime._original_time()
     sleeper_threads = {}
     REPEATS = 100
     for n in range(REPEATS):
         sleeper_threads[n] = sleeper_thread = threading.Thread(
             target=time.sleep,
             args=(3, ),
             name="test_sleep_sleeper_%d" % n)
         sleeper_thread.start()
     self.wait_sleep_started(REPEATS, 0.5)
     thread_time = virtualtime._original_time()
     setup_duration = thread_time - first_time
     assert setup_duration < 0.5
     virtualtime.set_time(thread_time + 20)
     for n in range(REPEATS):
         sleeper_threads[n].join()
     join_time = virtualtime._original_time()
     sleep_duration = join_time - thread_time
     virtualtime.restore_time()
     if self.virtual_time_enabled:
         assert sleep_duration < 0.2
     else:
         assert sleep_duration >= 3
Пример #3
0
 def test_callback(self):
     self.notify_event = threading.Event()
     virtualtime.notify_on_change(self.notify_event)
     self.callback_stop = False
     self.callback_event = threading.Event()
     self.callback_logs = []
     self.callback_missed = []
     ct = threading.Thread(target=self.callback_thread)
     ct.start()
     virtualtime.wait_for_callback_on_change(self.callback_event)
     try:
         start_time = virtualtime._original_time()
         virtualtime.set_offset(1)
         assert len(self.callback_logs) == 1 and not self.callback_missed
         assert self.callback_logs[0][1:] == (1, False)
         offset_time = virtualtime._original_time()
         assert offset_time - start_time < 0.1
         virtualtime.set_time(0)
         assert len(self.callback_logs) == 2 and not self.callback_missed
         assert self.callback_logs[1][
             1] < -start_time + 1 and self.callback_logs[1][2] is False
         set_time = virtualtime._original_time()
         assert set_time - offset_time < 0.1
         virtualtime.restore_time()
         assert len(self.callback_logs) == 3 and not self.callback_missed
         assert self.callback_logs[1][
             1] < -start_time + 1 and self.callback_logs[1][2] is False
         restore_time = virtualtime._original_time()
         assert restore_time - set_time < 0.1
     finally:
         # deleting this should ensure it drops out of the weak set and doesn't hang things up later...
         del self.callback_event
         self.callback_stop = True
         self.notify_event.set()
         ct.join()
Пример #4
0
 def test_sleep(self):
     """Tests that sleep comes back quicker than normal when time is advanced"""
     first_time = time.time()
     sleeper_thread = threading.Thread(target=time.sleep, args=(3,), name="test_sleep_sleeper")
     sleeper_thread.start()
     self.wait_sleep_started(1, 0.2)
     virtualtime.set_time(first_time + 5)
     sleeper_thread.join()
     virtualtime.restore_time()
     join_time = time.time()
     if self.virtual_time_enabled:
         assert join_time - first_time < 0.5
     else:
         assert join_time - first_time >= 3
Пример #5
0
 def test_sleep(self):
     """Tests that sleep comes back quicker than normal when time is advanced"""
     first_time = time.time()
     sleeper_thread = threading.Thread(target=time.sleep,
                                       args=(3, ),
                                       name="test_sleep_sleeper")
     sleeper_thread.start()
     self.wait_sleep_started(1, 0.2)
     virtualtime.set_time(first_time + 5)
     sleeper_thread.join()
     virtualtime.restore_time()
     join_time = time.time()
     if self.virtual_time_enabled:
         assert join_time - first_time < 0.5
     else:
         assert join_time - first_time >= 3
Пример #6
0
 def test_notify_on_change(self):
     self.notify_event = threading.Event()
     virtualtime.notify_on_change(self.notify_event)
     start_time = virtualtime._original_time()
     virtualtime.set_offset(1)
     assert self.notify_event.wait(0.1)
     self.notify_event.clear()
     offset_time = virtualtime._original_time()
     assert offset_time - start_time < 0.1
     virtualtime.set_time(0)
     assert self.notify_event.wait(0.1)
     self.notify_event.clear()
     set_time = virtualtime._original_time()
     assert set_time - offset_time < 0.1
     virtualtime.restore_time()
     assert self.notify_event.wait(0.1)
     self.notify_event.clear()
     restore_time = virtualtime._original_time()
     assert restore_time - set_time < 0.1
Пример #7
0
 def test_notify_on_change(self):
     self.notify_event = threading.Event()
     virtualtime.notify_on_change(self.notify_event)
     start_time = virtualtime._original_time()
     virtualtime.set_offset(1)
     assert self.notify_event.wait(0.1)
     self.notify_event.clear()
     offset_time = virtualtime._original_time()
     assert offset_time - start_time < 0.1
     virtualtime.set_time(0)
     assert self.notify_event.wait(0.1)
     self.notify_event.clear()
     set_time = virtualtime._original_time()
     assert set_time - offset_time < 0.1
     virtualtime.restore_time()
     assert self.notify_event.wait(0.1)
     self.notify_event.clear()
     restore_time = virtualtime._original_time()
     assert restore_time - set_time < 0.1
Пример #8
0
 def test_parallel_sleeps(self):
     """Tests that sleep comes back quicker than normal when time is advanced, and that this works with lots of threads"""
     first_time = virtualtime._original_time()
     sleeper_threads = {}
     REPEATS = 100
     for n in range(REPEATS):
         sleeper_threads[n] = sleeper_thread = threading.Thread(target=time.sleep, args=(3,), name="test_sleep_sleeper_%d" % n)
         sleeper_thread.start()
     self.wait_sleep_started(REPEATS, 0.5)
     thread_time = virtualtime._original_time()
     setup_duration = thread_time - first_time
     assert setup_duration < 0.5
     virtualtime.set_time(thread_time + 20)
     for n in range(REPEATS):
         sleeper_threads[n].join()
     join_time = virtualtime._original_time()
     sleep_duration = join_time - thread_time
     virtualtime.restore_time()
     if self.virtual_time_enabled:
         assert sleep_duration < 0.2
     else:
         assert sleep_duration >= 3