28 _PATHOLOGICAL_SCHEDULING =
'pathological thread scheduling!'
50 start_time = self._time.time()
51 self._time.sleep_for(_QUANTUM)
52 end_time = self._time.time()
54 self.assertLessEqual(start_time + _QUANTUM, end_time)
57 start_time = self._time.time()
58 self._time.sleep_until(start_time + _QUANTUM)
59 end_time = self._time.time()
61 self.assertLessEqual(start_time + _QUANTUM, end_time)
66 start_time = self._time.time()
67 self._time.call_in(time_noter, _QUANTUM)
68 self._time.sleep_for(_QUANTUM * 2)
69 call_times = time_noter.call_times()
71 self.assertTrue(call_times, msg=_PATHOLOGICAL_SCHEDULING)
72 self.assertLessEqual(start_time + _QUANTUM, call_times[0])
77 start_time = self._time.time()
78 self._time.call_at(time_noter, self._time.time() + _QUANTUM)
79 self._time.sleep_for(_QUANTUM * 2)
80 call_times = time_noter.call_times()
82 self.assertTrue(call_times, msg=_PATHOLOGICAL_SCHEDULING)
83 self.assertLessEqual(start_time + _QUANTUM, call_times[0])
88 future = self._time.call_in(time_noter, _QUANTUM * 2)
89 self._time.sleep_for(_QUANTUM)
90 cancelled = future.cancel()
91 self._time.sleep_for(_QUANTUM * 2)
92 call_times = time_noter.call_times()
94 self.assertFalse(call_times, msg=_PATHOLOGICAL_SCHEDULING)
95 self.assertTrue(cancelled)
96 self.assertTrue(future.cancelled())
99 test_events = tuple(threading.Event()
for _
in range(_MANY))
100 possibly_cancelled_futures = {}
101 background_noise_futures = []
103 for test_event
in test_events:
104 possibly_cancelled_futures[test_event] = self._time.call_in(
105 test_event.set, _QUANTUM * (2 + random.random()))
106 for _
in range(_MANY):
107 background_noise_futures.append(
108 self._time.call_in(threading.Event().set,
109 _QUANTUM * 1000 * random.random()))
110 self._time.sleep_for(_QUANTUM)
112 for test_event, test_future
in possibly_cancelled_futures.items():
113 if bool(random.randint(0, 1))
and test_future.cancel():
114 cancelled.add(test_event)
115 self._time.sleep_for(_QUANTUM * 3)
117 for test_event
in test_events:
118 (self.assertFalse
if test_event
in cancelled
else self.assertTrue)(
120 for background_noise_future
in background_noise_futures:
121 background_noise_future.cancel()
126 start_time = self._time.time()
127 first_future_at_one = self._time.call_in(time_noter, _QUANTUM)
128 second_future_at_one = self._time.call_in(time_noter, _QUANTUM)
129 first_future_at_three = self._time.call_in(time_noter, _QUANTUM * 3)
130 second_future_at_three = self._time.call_in(time_noter, _QUANTUM * 3)
131 self._time.sleep_for(_QUANTUM * 2)
132 first_future_at_one_cancelled = first_future_at_one.cancel()
133 second_future_at_one_cancelled = second_future_at_one.cancel()
134 first_future_at_three_cancelled = first_future_at_three.cancel()
135 self._time.sleep_for(_QUANTUM * 2)
136 second_future_at_three_cancelled = second_future_at_three.cancel()
137 first_future_at_three_cancelled_again = first_future_at_three.cancel()
138 call_times = time_noter.call_times()
140 self.assertEqual(3,
len(call_times), msg=_PATHOLOGICAL_SCHEDULING)
141 self.assertFalse(first_future_at_one_cancelled)
142 self.assertFalse(second_future_at_one_cancelled)
143 self.assertTrue(first_future_at_three_cancelled)
144 self.assertFalse(second_future_at_three_cancelled)
145 self.assertTrue(first_future_at_three_cancelled_again)
146 self.assertLessEqual(start_time + _QUANTUM, call_times[0])
147 self.assertLessEqual(start_time + _QUANTUM, call_times[1])
148 self.assertLessEqual(start_time + _QUANTUM * 3, call_times[2])
161 random.randint(0,
int(time.time())))
164 if __name__ ==
'__main__':
165 unittest.main(verbosity=2)