1 | n/a | import queue |
---|
2 | n/a | import sched |
---|
3 | n/a | import time |
---|
4 | n/a | import unittest |
---|
5 | n/a | try: |
---|
6 | n/a | import threading |
---|
7 | n/a | except ImportError: |
---|
8 | n/a | threading = None |
---|
9 | n/a | |
---|
10 | n/a | TIMEOUT = 10 |
---|
11 | n/a | |
---|
12 | n/a | |
---|
13 | n/a | class Timer: |
---|
14 | n/a | def __init__(self): |
---|
15 | n/a | self._cond = threading.Condition() |
---|
16 | n/a | self._time = 0 |
---|
17 | n/a | self._stop = 0 |
---|
18 | n/a | |
---|
19 | n/a | def time(self): |
---|
20 | n/a | with self._cond: |
---|
21 | n/a | return self._time |
---|
22 | n/a | |
---|
23 | n/a | # increase the time but not beyond the established limit |
---|
24 | n/a | def sleep(self, t): |
---|
25 | n/a | assert t >= 0 |
---|
26 | n/a | with self._cond: |
---|
27 | n/a | t += self._time |
---|
28 | n/a | while self._stop < t: |
---|
29 | n/a | self._time = self._stop |
---|
30 | n/a | self._cond.wait() |
---|
31 | n/a | self._time = t |
---|
32 | n/a | |
---|
33 | n/a | # advance time limit for user code |
---|
34 | n/a | def advance(self, t): |
---|
35 | n/a | assert t >= 0 |
---|
36 | n/a | with self._cond: |
---|
37 | n/a | self._stop += t |
---|
38 | n/a | self._cond.notify_all() |
---|
39 | n/a | |
---|
40 | n/a | |
---|
41 | n/a | class TestCase(unittest.TestCase): |
---|
42 | n/a | |
---|
43 | n/a | def test_enter(self): |
---|
44 | n/a | l = [] |
---|
45 | n/a | fun = lambda x: l.append(x) |
---|
46 | n/a | scheduler = sched.scheduler(time.time, time.sleep) |
---|
47 | n/a | for x in [0.5, 0.4, 0.3, 0.2, 0.1]: |
---|
48 | n/a | z = scheduler.enter(x, 1, fun, (x,)) |
---|
49 | n/a | scheduler.run() |
---|
50 | n/a | self.assertEqual(l, [0.1, 0.2, 0.3, 0.4, 0.5]) |
---|
51 | n/a | |
---|
52 | n/a | def test_enterabs(self): |
---|
53 | n/a | l = [] |
---|
54 | n/a | fun = lambda x: l.append(x) |
---|
55 | n/a | scheduler = sched.scheduler(time.time, time.sleep) |
---|
56 | n/a | for x in [0.05, 0.04, 0.03, 0.02, 0.01]: |
---|
57 | n/a | z = scheduler.enterabs(x, 1, fun, (x,)) |
---|
58 | n/a | scheduler.run() |
---|
59 | n/a | self.assertEqual(l, [0.01, 0.02, 0.03, 0.04, 0.05]) |
---|
60 | n/a | |
---|
61 | n/a | @unittest.skipUnless(threading, 'Threading required for this test.') |
---|
62 | n/a | def test_enter_concurrent(self): |
---|
63 | n/a | q = queue.Queue() |
---|
64 | n/a | fun = q.put |
---|
65 | n/a | timer = Timer() |
---|
66 | n/a | scheduler = sched.scheduler(timer.time, timer.sleep) |
---|
67 | n/a | scheduler.enter(1, 1, fun, (1,)) |
---|
68 | n/a | scheduler.enter(3, 1, fun, (3,)) |
---|
69 | n/a | t = threading.Thread(target=scheduler.run) |
---|
70 | n/a | t.start() |
---|
71 | n/a | timer.advance(1) |
---|
72 | n/a | self.assertEqual(q.get(timeout=TIMEOUT), 1) |
---|
73 | n/a | self.assertTrue(q.empty()) |
---|
74 | n/a | for x in [4, 5, 2]: |
---|
75 | n/a | z = scheduler.enter(x - 1, 1, fun, (x,)) |
---|
76 | n/a | timer.advance(2) |
---|
77 | n/a | self.assertEqual(q.get(timeout=TIMEOUT), 2) |
---|
78 | n/a | self.assertEqual(q.get(timeout=TIMEOUT), 3) |
---|
79 | n/a | self.assertTrue(q.empty()) |
---|
80 | n/a | timer.advance(1) |
---|
81 | n/a | self.assertEqual(q.get(timeout=TIMEOUT), 4) |
---|
82 | n/a | self.assertTrue(q.empty()) |
---|
83 | n/a | timer.advance(1) |
---|
84 | n/a | self.assertEqual(q.get(timeout=TIMEOUT), 5) |
---|
85 | n/a | self.assertTrue(q.empty()) |
---|
86 | n/a | timer.advance(1000) |
---|
87 | n/a | t.join(timeout=TIMEOUT) |
---|
88 | n/a | self.assertFalse(t.is_alive()) |
---|
89 | n/a | self.assertTrue(q.empty()) |
---|
90 | n/a | self.assertEqual(timer.time(), 5) |
---|
91 | n/a | |
---|
92 | n/a | def test_priority(self): |
---|
93 | n/a | l = [] |
---|
94 | n/a | fun = lambda x: l.append(x) |
---|
95 | n/a | scheduler = sched.scheduler(time.time, time.sleep) |
---|
96 | n/a | for priority in [1, 2, 3, 4, 5]: |
---|
97 | n/a | z = scheduler.enterabs(0.01, priority, fun, (priority,)) |
---|
98 | n/a | scheduler.run() |
---|
99 | n/a | self.assertEqual(l, [1, 2, 3, 4, 5]) |
---|
100 | n/a | |
---|
101 | n/a | def test_cancel(self): |
---|
102 | n/a | l = [] |
---|
103 | n/a | fun = lambda x: l.append(x) |
---|
104 | n/a | scheduler = sched.scheduler(time.time, time.sleep) |
---|
105 | n/a | now = time.time() |
---|
106 | n/a | event1 = scheduler.enterabs(now + 0.01, 1, fun, (0.01,)) |
---|
107 | n/a | event2 = scheduler.enterabs(now + 0.02, 1, fun, (0.02,)) |
---|
108 | n/a | event3 = scheduler.enterabs(now + 0.03, 1, fun, (0.03,)) |
---|
109 | n/a | event4 = scheduler.enterabs(now + 0.04, 1, fun, (0.04,)) |
---|
110 | n/a | event5 = scheduler.enterabs(now + 0.05, 1, fun, (0.05,)) |
---|
111 | n/a | scheduler.cancel(event1) |
---|
112 | n/a | scheduler.cancel(event5) |
---|
113 | n/a | scheduler.run() |
---|
114 | n/a | self.assertEqual(l, [0.02, 0.03, 0.04]) |
---|
115 | n/a | |
---|
116 | n/a | @unittest.skipUnless(threading, 'Threading required for this test.') |
---|
117 | n/a | def test_cancel_concurrent(self): |
---|
118 | n/a | q = queue.Queue() |
---|
119 | n/a | fun = q.put |
---|
120 | n/a | timer = Timer() |
---|
121 | n/a | scheduler = sched.scheduler(timer.time, timer.sleep) |
---|
122 | n/a | now = timer.time() |
---|
123 | n/a | event1 = scheduler.enterabs(now + 1, 1, fun, (1,)) |
---|
124 | n/a | event2 = scheduler.enterabs(now + 2, 1, fun, (2,)) |
---|
125 | n/a | event4 = scheduler.enterabs(now + 4, 1, fun, (4,)) |
---|
126 | n/a | event5 = scheduler.enterabs(now + 5, 1, fun, (5,)) |
---|
127 | n/a | event3 = scheduler.enterabs(now + 3, 1, fun, (3,)) |
---|
128 | n/a | t = threading.Thread(target=scheduler.run) |
---|
129 | n/a | t.start() |
---|
130 | n/a | timer.advance(1) |
---|
131 | n/a | self.assertEqual(q.get(timeout=TIMEOUT), 1) |
---|
132 | n/a | self.assertTrue(q.empty()) |
---|
133 | n/a | scheduler.cancel(event2) |
---|
134 | n/a | scheduler.cancel(event5) |
---|
135 | n/a | timer.advance(1) |
---|
136 | n/a | self.assertTrue(q.empty()) |
---|
137 | n/a | timer.advance(1) |
---|
138 | n/a | self.assertEqual(q.get(timeout=TIMEOUT), 3) |
---|
139 | n/a | self.assertTrue(q.empty()) |
---|
140 | n/a | timer.advance(1) |
---|
141 | n/a | self.assertEqual(q.get(timeout=TIMEOUT), 4) |
---|
142 | n/a | self.assertTrue(q.empty()) |
---|
143 | n/a | timer.advance(1000) |
---|
144 | n/a | t.join(timeout=TIMEOUT) |
---|
145 | n/a | self.assertFalse(t.is_alive()) |
---|
146 | n/a | self.assertTrue(q.empty()) |
---|
147 | n/a | self.assertEqual(timer.time(), 4) |
---|
148 | n/a | |
---|
149 | n/a | def test_empty(self): |
---|
150 | n/a | l = [] |
---|
151 | n/a | fun = lambda x: l.append(x) |
---|
152 | n/a | scheduler = sched.scheduler(time.time, time.sleep) |
---|
153 | n/a | self.assertTrue(scheduler.empty()) |
---|
154 | n/a | for x in [0.05, 0.04, 0.03, 0.02, 0.01]: |
---|
155 | n/a | z = scheduler.enterabs(x, 1, fun, (x,)) |
---|
156 | n/a | self.assertFalse(scheduler.empty()) |
---|
157 | n/a | scheduler.run() |
---|
158 | n/a | self.assertTrue(scheduler.empty()) |
---|
159 | n/a | |
---|
160 | n/a | def test_queue(self): |
---|
161 | n/a | l = [] |
---|
162 | n/a | fun = lambda x: l.append(x) |
---|
163 | n/a | scheduler = sched.scheduler(time.time, time.sleep) |
---|
164 | n/a | now = time.time() |
---|
165 | n/a | e5 = scheduler.enterabs(now + 0.05, 1, fun) |
---|
166 | n/a | e1 = scheduler.enterabs(now + 0.01, 1, fun) |
---|
167 | n/a | e2 = scheduler.enterabs(now + 0.02, 1, fun) |
---|
168 | n/a | e4 = scheduler.enterabs(now + 0.04, 1, fun) |
---|
169 | n/a | e3 = scheduler.enterabs(now + 0.03, 1, fun) |
---|
170 | n/a | # queue property is supposed to return an order list of |
---|
171 | n/a | # upcoming events |
---|
172 | n/a | self.assertEqual(scheduler.queue, [e1, e2, e3, e4, e5]) |
---|
173 | n/a | |
---|
174 | n/a | def test_args_kwargs(self): |
---|
175 | n/a | seq = [] |
---|
176 | n/a | def fun(*a, **b): |
---|
177 | n/a | seq.append((a, b)) |
---|
178 | n/a | |
---|
179 | n/a | now = time.time() |
---|
180 | n/a | scheduler = sched.scheduler(time.time, time.sleep) |
---|
181 | n/a | scheduler.enterabs(now, 1, fun) |
---|
182 | n/a | scheduler.enterabs(now, 1, fun, argument=(1, 2)) |
---|
183 | n/a | scheduler.enterabs(now, 1, fun, argument=('a', 'b')) |
---|
184 | n/a | scheduler.enterabs(now, 1, fun, argument=(1, 2), kwargs={"foo": 3}) |
---|
185 | n/a | scheduler.run() |
---|
186 | n/a | self.assertCountEqual(seq, [ |
---|
187 | n/a | ((), {}), |
---|
188 | n/a | ((1, 2), {}), |
---|
189 | n/a | (('a', 'b'), {}), |
---|
190 | n/a | ((1, 2), {'foo': 3}) |
---|
191 | n/a | ]) |
---|
192 | n/a | |
---|
193 | n/a | def test_run_non_blocking(self): |
---|
194 | n/a | l = [] |
---|
195 | n/a | fun = lambda x: l.append(x) |
---|
196 | n/a | scheduler = sched.scheduler(time.time, time.sleep) |
---|
197 | n/a | for x in [10, 9, 8, 7, 6]: |
---|
198 | n/a | scheduler.enter(x, 1, fun, (x,)) |
---|
199 | n/a | scheduler.run(blocking=False) |
---|
200 | n/a | self.assertEqual(l, []) |
---|
201 | n/a | |
---|
202 | n/a | |
---|
203 | n/a | if __name__ == "__main__": |
---|
204 | n/a | unittest.main() |
---|