1 | n/a | """Tests for queues.py""" |
---|
2 | n/a | |
---|
3 | n/a | import unittest |
---|
4 | n/a | from unittest import mock |
---|
5 | n/a | |
---|
6 | n/a | import asyncio |
---|
7 | n/a | from asyncio import test_utils |
---|
8 | n/a | |
---|
9 | n/a | |
---|
10 | n/a | class _QueueTestBase(test_utils.TestCase): |
---|
11 | n/a | |
---|
12 | n/a | def setUp(self): |
---|
13 | n/a | super().setUp() |
---|
14 | n/a | self.loop = self.new_test_loop() |
---|
15 | n/a | |
---|
16 | n/a | |
---|
17 | n/a | class QueueBasicTests(_QueueTestBase): |
---|
18 | n/a | |
---|
19 | n/a | def _test_repr_or_str(self, fn, expect_id): |
---|
20 | n/a | """Test Queue's repr or str. |
---|
21 | n/a | |
---|
22 | n/a | fn is repr or str. expect_id is True if we expect the Queue's id to |
---|
23 | n/a | appear in fn(Queue()). |
---|
24 | n/a | """ |
---|
25 | n/a | def gen(): |
---|
26 | n/a | when = yield |
---|
27 | n/a | self.assertAlmostEqual(0.1, when) |
---|
28 | n/a | when = yield 0.1 |
---|
29 | n/a | self.assertAlmostEqual(0.2, when) |
---|
30 | n/a | yield 0.1 |
---|
31 | n/a | |
---|
32 | n/a | loop = self.new_test_loop(gen) |
---|
33 | n/a | |
---|
34 | n/a | q = asyncio.Queue(loop=loop) |
---|
35 | n/a | self.assertTrue(fn(q).startswith('<Queue'), fn(q)) |
---|
36 | n/a | id_is_present = hex(id(q)) in fn(q) |
---|
37 | n/a | self.assertEqual(expect_id, id_is_present) |
---|
38 | n/a | |
---|
39 | n/a | @asyncio.coroutine |
---|
40 | n/a | def add_getter(): |
---|
41 | n/a | q = asyncio.Queue(loop=loop) |
---|
42 | n/a | # Start a task that waits to get. |
---|
43 | n/a | asyncio.Task(q.get(), loop=loop) |
---|
44 | n/a | # Let it start waiting. |
---|
45 | n/a | yield from asyncio.sleep(0.1, loop=loop) |
---|
46 | n/a | self.assertTrue('_getters[1]' in fn(q)) |
---|
47 | n/a | # resume q.get coroutine to finish generator |
---|
48 | n/a | q.put_nowait(0) |
---|
49 | n/a | |
---|
50 | n/a | loop.run_until_complete(add_getter()) |
---|
51 | n/a | |
---|
52 | n/a | @asyncio.coroutine |
---|
53 | n/a | def add_putter(): |
---|
54 | n/a | q = asyncio.Queue(maxsize=1, loop=loop) |
---|
55 | n/a | q.put_nowait(1) |
---|
56 | n/a | # Start a task that waits to put. |
---|
57 | n/a | asyncio.Task(q.put(2), loop=loop) |
---|
58 | n/a | # Let it start waiting. |
---|
59 | n/a | yield from asyncio.sleep(0.1, loop=loop) |
---|
60 | n/a | self.assertTrue('_putters[1]' in fn(q)) |
---|
61 | n/a | # resume q.put coroutine to finish generator |
---|
62 | n/a | q.get_nowait() |
---|
63 | n/a | |
---|
64 | n/a | loop.run_until_complete(add_putter()) |
---|
65 | n/a | |
---|
66 | n/a | q = asyncio.Queue(loop=loop) |
---|
67 | n/a | q.put_nowait(1) |
---|
68 | n/a | self.assertTrue('_queue=[1]' in fn(q)) |
---|
69 | n/a | |
---|
70 | n/a | def test_ctor_loop(self): |
---|
71 | n/a | loop = mock.Mock() |
---|
72 | n/a | q = asyncio.Queue(loop=loop) |
---|
73 | n/a | self.assertIs(q._loop, loop) |
---|
74 | n/a | |
---|
75 | n/a | q = asyncio.Queue(loop=self.loop) |
---|
76 | n/a | self.assertIs(q._loop, self.loop) |
---|
77 | n/a | |
---|
78 | n/a | def test_ctor_noloop(self): |
---|
79 | n/a | asyncio.set_event_loop(self.loop) |
---|
80 | n/a | q = asyncio.Queue() |
---|
81 | n/a | self.assertIs(q._loop, self.loop) |
---|
82 | n/a | |
---|
83 | n/a | def test_repr(self): |
---|
84 | n/a | self._test_repr_or_str(repr, True) |
---|
85 | n/a | |
---|
86 | n/a | def test_str(self): |
---|
87 | n/a | self._test_repr_or_str(str, False) |
---|
88 | n/a | |
---|
89 | n/a | def test_empty(self): |
---|
90 | n/a | q = asyncio.Queue(loop=self.loop) |
---|
91 | n/a | self.assertTrue(q.empty()) |
---|
92 | n/a | q.put_nowait(1) |
---|
93 | n/a | self.assertFalse(q.empty()) |
---|
94 | n/a | self.assertEqual(1, q.get_nowait()) |
---|
95 | n/a | self.assertTrue(q.empty()) |
---|
96 | n/a | |
---|
97 | n/a | def test_full(self): |
---|
98 | n/a | q = asyncio.Queue(loop=self.loop) |
---|
99 | n/a | self.assertFalse(q.full()) |
---|
100 | n/a | |
---|
101 | n/a | q = asyncio.Queue(maxsize=1, loop=self.loop) |
---|
102 | n/a | q.put_nowait(1) |
---|
103 | n/a | self.assertTrue(q.full()) |
---|
104 | n/a | |
---|
105 | n/a | def test_order(self): |
---|
106 | n/a | q = asyncio.Queue(loop=self.loop) |
---|
107 | n/a | for i in [1, 3, 2]: |
---|
108 | n/a | q.put_nowait(i) |
---|
109 | n/a | |
---|
110 | n/a | items = [q.get_nowait() for _ in range(3)] |
---|
111 | n/a | self.assertEqual([1, 3, 2], items) |
---|
112 | n/a | |
---|
113 | n/a | def test_maxsize(self): |
---|
114 | n/a | |
---|
115 | n/a | def gen(): |
---|
116 | n/a | when = yield |
---|
117 | n/a | self.assertAlmostEqual(0.01, when) |
---|
118 | n/a | when = yield 0.01 |
---|
119 | n/a | self.assertAlmostEqual(0.02, when) |
---|
120 | n/a | yield 0.01 |
---|
121 | n/a | |
---|
122 | n/a | loop = self.new_test_loop(gen) |
---|
123 | n/a | |
---|
124 | n/a | q = asyncio.Queue(maxsize=2, loop=loop) |
---|
125 | n/a | self.assertEqual(2, q.maxsize) |
---|
126 | n/a | have_been_put = [] |
---|
127 | n/a | |
---|
128 | n/a | @asyncio.coroutine |
---|
129 | n/a | def putter(): |
---|
130 | n/a | for i in range(3): |
---|
131 | n/a | yield from q.put(i) |
---|
132 | n/a | have_been_put.append(i) |
---|
133 | n/a | return True |
---|
134 | n/a | |
---|
135 | n/a | @asyncio.coroutine |
---|
136 | n/a | def test(): |
---|
137 | n/a | t = asyncio.Task(putter(), loop=loop) |
---|
138 | n/a | yield from asyncio.sleep(0.01, loop=loop) |
---|
139 | n/a | |
---|
140 | n/a | # The putter is blocked after putting two items. |
---|
141 | n/a | self.assertEqual([0, 1], have_been_put) |
---|
142 | n/a | self.assertEqual(0, q.get_nowait()) |
---|
143 | n/a | |
---|
144 | n/a | # Let the putter resume and put last item. |
---|
145 | n/a | yield from asyncio.sleep(0.01, loop=loop) |
---|
146 | n/a | self.assertEqual([0, 1, 2], have_been_put) |
---|
147 | n/a | self.assertEqual(1, q.get_nowait()) |
---|
148 | n/a | self.assertEqual(2, q.get_nowait()) |
---|
149 | n/a | |
---|
150 | n/a | self.assertTrue(t.done()) |
---|
151 | n/a | self.assertTrue(t.result()) |
---|
152 | n/a | |
---|
153 | n/a | loop.run_until_complete(test()) |
---|
154 | n/a | self.assertAlmostEqual(0.02, loop.time()) |
---|
155 | n/a | |
---|
156 | n/a | |
---|
157 | n/a | class QueueGetTests(_QueueTestBase): |
---|
158 | n/a | |
---|
159 | n/a | def test_blocking_get(self): |
---|
160 | n/a | q = asyncio.Queue(loop=self.loop) |
---|
161 | n/a | q.put_nowait(1) |
---|
162 | n/a | |
---|
163 | n/a | @asyncio.coroutine |
---|
164 | n/a | def queue_get(): |
---|
165 | n/a | return (yield from q.get()) |
---|
166 | n/a | |
---|
167 | n/a | res = self.loop.run_until_complete(queue_get()) |
---|
168 | n/a | self.assertEqual(1, res) |
---|
169 | n/a | |
---|
170 | n/a | def test_get_with_putters(self): |
---|
171 | n/a | q = asyncio.Queue(1, loop=self.loop) |
---|
172 | n/a | q.put_nowait(1) |
---|
173 | n/a | |
---|
174 | n/a | waiter = asyncio.Future(loop=self.loop) |
---|
175 | n/a | q._putters.append(waiter) |
---|
176 | n/a | |
---|
177 | n/a | res = self.loop.run_until_complete(q.get()) |
---|
178 | n/a | self.assertEqual(1, res) |
---|
179 | n/a | self.assertTrue(waiter.done()) |
---|
180 | n/a | self.assertIsNone(waiter.result()) |
---|
181 | n/a | |
---|
182 | n/a | def test_blocking_get_wait(self): |
---|
183 | n/a | |
---|
184 | n/a | def gen(): |
---|
185 | n/a | when = yield |
---|
186 | n/a | self.assertAlmostEqual(0.01, when) |
---|
187 | n/a | yield 0.01 |
---|
188 | n/a | |
---|
189 | n/a | loop = self.new_test_loop(gen) |
---|
190 | n/a | |
---|
191 | n/a | q = asyncio.Queue(loop=loop) |
---|
192 | n/a | started = asyncio.Event(loop=loop) |
---|
193 | n/a | finished = False |
---|
194 | n/a | |
---|
195 | n/a | @asyncio.coroutine |
---|
196 | n/a | def queue_get(): |
---|
197 | n/a | nonlocal finished |
---|
198 | n/a | started.set() |
---|
199 | n/a | res = yield from q.get() |
---|
200 | n/a | finished = True |
---|
201 | n/a | return res |
---|
202 | n/a | |
---|
203 | n/a | @asyncio.coroutine |
---|
204 | n/a | def queue_put(): |
---|
205 | n/a | loop.call_later(0.01, q.put_nowait, 1) |
---|
206 | n/a | queue_get_task = asyncio.Task(queue_get(), loop=loop) |
---|
207 | n/a | yield from started.wait() |
---|
208 | n/a | self.assertFalse(finished) |
---|
209 | n/a | res = yield from queue_get_task |
---|
210 | n/a | self.assertTrue(finished) |
---|
211 | n/a | return res |
---|
212 | n/a | |
---|
213 | n/a | res = loop.run_until_complete(queue_put()) |
---|
214 | n/a | self.assertEqual(1, res) |
---|
215 | n/a | self.assertAlmostEqual(0.01, loop.time()) |
---|
216 | n/a | |
---|
217 | n/a | def test_nonblocking_get(self): |
---|
218 | n/a | q = asyncio.Queue(loop=self.loop) |
---|
219 | n/a | q.put_nowait(1) |
---|
220 | n/a | self.assertEqual(1, q.get_nowait()) |
---|
221 | n/a | |
---|
222 | n/a | def test_nonblocking_get_exception(self): |
---|
223 | n/a | q = asyncio.Queue(loop=self.loop) |
---|
224 | n/a | self.assertRaises(asyncio.QueueEmpty, q.get_nowait) |
---|
225 | n/a | |
---|
226 | n/a | def test_get_cancelled(self): |
---|
227 | n/a | |
---|
228 | n/a | def gen(): |
---|
229 | n/a | when = yield |
---|
230 | n/a | self.assertAlmostEqual(0.01, when) |
---|
231 | n/a | when = yield 0.01 |
---|
232 | n/a | self.assertAlmostEqual(0.061, when) |
---|
233 | n/a | yield 0.05 |
---|
234 | n/a | |
---|
235 | n/a | loop = self.new_test_loop(gen) |
---|
236 | n/a | |
---|
237 | n/a | q = asyncio.Queue(loop=loop) |
---|
238 | n/a | |
---|
239 | n/a | @asyncio.coroutine |
---|
240 | n/a | def queue_get(): |
---|
241 | n/a | return (yield from asyncio.wait_for(q.get(), 0.051, loop=loop)) |
---|
242 | n/a | |
---|
243 | n/a | @asyncio.coroutine |
---|
244 | n/a | def test(): |
---|
245 | n/a | get_task = asyncio.Task(queue_get(), loop=loop) |
---|
246 | n/a | yield from asyncio.sleep(0.01, loop=loop) # let the task start |
---|
247 | n/a | q.put_nowait(1) |
---|
248 | n/a | return (yield from get_task) |
---|
249 | n/a | |
---|
250 | n/a | self.assertEqual(1, loop.run_until_complete(test())) |
---|
251 | n/a | self.assertAlmostEqual(0.06, loop.time()) |
---|
252 | n/a | |
---|
253 | n/a | def test_get_cancelled_race(self): |
---|
254 | n/a | q = asyncio.Queue(loop=self.loop) |
---|
255 | n/a | |
---|
256 | n/a | t1 = asyncio.Task(q.get(), loop=self.loop) |
---|
257 | n/a | t2 = asyncio.Task(q.get(), loop=self.loop) |
---|
258 | n/a | |
---|
259 | n/a | test_utils.run_briefly(self.loop) |
---|
260 | n/a | t1.cancel() |
---|
261 | n/a | test_utils.run_briefly(self.loop) |
---|
262 | n/a | self.assertTrue(t1.done()) |
---|
263 | n/a | q.put_nowait('a') |
---|
264 | n/a | test_utils.run_briefly(self.loop) |
---|
265 | n/a | self.assertEqual(t2.result(), 'a') |
---|
266 | n/a | |
---|
267 | n/a | def test_get_with_waiting_putters(self): |
---|
268 | n/a | q = asyncio.Queue(loop=self.loop, maxsize=1) |
---|
269 | n/a | asyncio.Task(q.put('a'), loop=self.loop) |
---|
270 | n/a | asyncio.Task(q.put('b'), loop=self.loop) |
---|
271 | n/a | test_utils.run_briefly(self.loop) |
---|
272 | n/a | self.assertEqual(self.loop.run_until_complete(q.get()), 'a') |
---|
273 | n/a | self.assertEqual(self.loop.run_until_complete(q.get()), 'b') |
---|
274 | n/a | |
---|
275 | n/a | def test_why_are_getters_waiting(self): |
---|
276 | n/a | # From issue #268. |
---|
277 | n/a | |
---|
278 | n/a | @asyncio.coroutine |
---|
279 | n/a | def consumer(queue, num_expected): |
---|
280 | n/a | for _ in range(num_expected): |
---|
281 | n/a | yield from queue.get() |
---|
282 | n/a | |
---|
283 | n/a | @asyncio.coroutine |
---|
284 | n/a | def producer(queue, num_items): |
---|
285 | n/a | for i in range(num_items): |
---|
286 | n/a | yield from queue.put(i) |
---|
287 | n/a | |
---|
288 | n/a | queue_size = 1 |
---|
289 | n/a | producer_num_items = 5 |
---|
290 | n/a | q = asyncio.Queue(queue_size, loop=self.loop) |
---|
291 | n/a | |
---|
292 | n/a | self.loop.run_until_complete( |
---|
293 | n/a | asyncio.gather(producer(q, producer_num_items), |
---|
294 | n/a | consumer(q, producer_num_items), |
---|
295 | n/a | loop=self.loop), |
---|
296 | n/a | ) |
---|
297 | n/a | |
---|
298 | n/a | |
---|
299 | n/a | class QueuePutTests(_QueueTestBase): |
---|
300 | n/a | |
---|
301 | n/a | def test_blocking_put(self): |
---|
302 | n/a | q = asyncio.Queue(loop=self.loop) |
---|
303 | n/a | |
---|
304 | n/a | @asyncio.coroutine |
---|
305 | n/a | def queue_put(): |
---|
306 | n/a | # No maxsize, won't block. |
---|
307 | n/a | yield from q.put(1) |
---|
308 | n/a | |
---|
309 | n/a | self.loop.run_until_complete(queue_put()) |
---|
310 | n/a | |
---|
311 | n/a | def test_blocking_put_wait(self): |
---|
312 | n/a | |
---|
313 | n/a | def gen(): |
---|
314 | n/a | when = yield |
---|
315 | n/a | self.assertAlmostEqual(0.01, when) |
---|
316 | n/a | yield 0.01 |
---|
317 | n/a | |
---|
318 | n/a | loop = self.new_test_loop(gen) |
---|
319 | n/a | |
---|
320 | n/a | q = asyncio.Queue(maxsize=1, loop=loop) |
---|
321 | n/a | started = asyncio.Event(loop=loop) |
---|
322 | n/a | finished = False |
---|
323 | n/a | |
---|
324 | n/a | @asyncio.coroutine |
---|
325 | n/a | def queue_put(): |
---|
326 | n/a | nonlocal finished |
---|
327 | n/a | started.set() |
---|
328 | n/a | yield from q.put(1) |
---|
329 | n/a | yield from q.put(2) |
---|
330 | n/a | finished = True |
---|
331 | n/a | |
---|
332 | n/a | @asyncio.coroutine |
---|
333 | n/a | def queue_get(): |
---|
334 | n/a | loop.call_later(0.01, q.get_nowait) |
---|
335 | n/a | queue_put_task = asyncio.Task(queue_put(), loop=loop) |
---|
336 | n/a | yield from started.wait() |
---|
337 | n/a | self.assertFalse(finished) |
---|
338 | n/a | yield from queue_put_task |
---|
339 | n/a | self.assertTrue(finished) |
---|
340 | n/a | |
---|
341 | n/a | loop.run_until_complete(queue_get()) |
---|
342 | n/a | self.assertAlmostEqual(0.01, loop.time()) |
---|
343 | n/a | |
---|
344 | n/a | def test_nonblocking_put(self): |
---|
345 | n/a | q = asyncio.Queue(loop=self.loop) |
---|
346 | n/a | q.put_nowait(1) |
---|
347 | n/a | self.assertEqual(1, q.get_nowait()) |
---|
348 | n/a | |
---|
349 | n/a | def test_get_cancel_drop_one_pending_reader(self): |
---|
350 | n/a | def gen(): |
---|
351 | n/a | yield 0.01 |
---|
352 | n/a | yield 0.1 |
---|
353 | n/a | |
---|
354 | n/a | loop = self.new_test_loop(gen) |
---|
355 | n/a | |
---|
356 | n/a | q = asyncio.Queue(loop=loop) |
---|
357 | n/a | |
---|
358 | n/a | reader = loop.create_task(q.get()) |
---|
359 | n/a | |
---|
360 | n/a | loop.run_until_complete(asyncio.sleep(0.01, loop=loop)) |
---|
361 | n/a | |
---|
362 | n/a | q.put_nowait(1) |
---|
363 | n/a | q.put_nowait(2) |
---|
364 | n/a | reader.cancel() |
---|
365 | n/a | |
---|
366 | n/a | try: |
---|
367 | n/a | loop.run_until_complete(reader) |
---|
368 | n/a | except asyncio.CancelledError: |
---|
369 | n/a | # try again |
---|
370 | n/a | reader = loop.create_task(q.get()) |
---|
371 | n/a | loop.run_until_complete(reader) |
---|
372 | n/a | |
---|
373 | n/a | result = reader.result() |
---|
374 | n/a | # if we get 2, it means 1 got dropped! |
---|
375 | n/a | self.assertEqual(1, result) |
---|
376 | n/a | |
---|
377 | n/a | def test_get_cancel_drop_many_pending_readers(self): |
---|
378 | n/a | def gen(): |
---|
379 | n/a | yield 0.01 |
---|
380 | n/a | yield 0.1 |
---|
381 | n/a | |
---|
382 | n/a | loop = self.new_test_loop(gen) |
---|
383 | n/a | loop.set_debug(True) |
---|
384 | n/a | |
---|
385 | n/a | q = asyncio.Queue(loop=loop) |
---|
386 | n/a | |
---|
387 | n/a | reader1 = loop.create_task(q.get()) |
---|
388 | n/a | reader2 = loop.create_task(q.get()) |
---|
389 | n/a | reader3 = loop.create_task(q.get()) |
---|
390 | n/a | |
---|
391 | n/a | loop.run_until_complete(asyncio.sleep(0.01, loop=loop)) |
---|
392 | n/a | |
---|
393 | n/a | q.put_nowait(1) |
---|
394 | n/a | q.put_nowait(2) |
---|
395 | n/a | reader1.cancel() |
---|
396 | n/a | |
---|
397 | n/a | try: |
---|
398 | n/a | loop.run_until_complete(reader1) |
---|
399 | n/a | except asyncio.CancelledError: |
---|
400 | n/a | pass |
---|
401 | n/a | |
---|
402 | n/a | loop.run_until_complete(reader3) |
---|
403 | n/a | |
---|
404 | n/a | # It is undefined in which order concurrent readers receive results. |
---|
405 | n/a | self.assertEqual({reader2.result(), reader3.result()}, {1, 2}) |
---|
406 | n/a | |
---|
407 | n/a | def test_put_cancel_drop(self): |
---|
408 | n/a | |
---|
409 | n/a | def gen(): |
---|
410 | n/a | yield 0.01 |
---|
411 | n/a | yield 0.1 |
---|
412 | n/a | |
---|
413 | n/a | loop = self.new_test_loop(gen) |
---|
414 | n/a | q = asyncio.Queue(1, loop=loop) |
---|
415 | n/a | |
---|
416 | n/a | q.put_nowait(1) |
---|
417 | n/a | |
---|
418 | n/a | # putting a second item in the queue has to block (qsize=1) |
---|
419 | n/a | writer = loop.create_task(q.put(2)) |
---|
420 | n/a | loop.run_until_complete(asyncio.sleep(0.01, loop=loop)) |
---|
421 | n/a | |
---|
422 | n/a | value1 = q.get_nowait() |
---|
423 | n/a | self.assertEqual(value1, 1) |
---|
424 | n/a | |
---|
425 | n/a | writer.cancel() |
---|
426 | n/a | try: |
---|
427 | n/a | loop.run_until_complete(writer) |
---|
428 | n/a | except asyncio.CancelledError: |
---|
429 | n/a | # try again |
---|
430 | n/a | writer = loop.create_task(q.put(2)) |
---|
431 | n/a | loop.run_until_complete(writer) |
---|
432 | n/a | |
---|
433 | n/a | value2 = q.get_nowait() |
---|
434 | n/a | self.assertEqual(value2, 2) |
---|
435 | n/a | self.assertEqual(q.qsize(), 0) |
---|
436 | n/a | |
---|
437 | n/a | def test_nonblocking_put_exception(self): |
---|
438 | n/a | q = asyncio.Queue(maxsize=1, loop=self.loop) |
---|
439 | n/a | q.put_nowait(1) |
---|
440 | n/a | self.assertRaises(asyncio.QueueFull, q.put_nowait, 2) |
---|
441 | n/a | |
---|
442 | n/a | def test_float_maxsize(self): |
---|
443 | n/a | q = asyncio.Queue(maxsize=1.3, loop=self.loop) |
---|
444 | n/a | q.put_nowait(1) |
---|
445 | n/a | q.put_nowait(2) |
---|
446 | n/a | self.assertTrue(q.full()) |
---|
447 | n/a | self.assertRaises(asyncio.QueueFull, q.put_nowait, 3) |
---|
448 | n/a | |
---|
449 | n/a | q = asyncio.Queue(maxsize=1.3, loop=self.loop) |
---|
450 | n/a | @asyncio.coroutine |
---|
451 | n/a | def queue_put(): |
---|
452 | n/a | yield from q.put(1) |
---|
453 | n/a | yield from q.put(2) |
---|
454 | n/a | self.assertTrue(q.full()) |
---|
455 | n/a | self.loop.run_until_complete(queue_put()) |
---|
456 | n/a | |
---|
457 | n/a | def test_put_cancelled(self): |
---|
458 | n/a | q = asyncio.Queue(loop=self.loop) |
---|
459 | n/a | |
---|
460 | n/a | @asyncio.coroutine |
---|
461 | n/a | def queue_put(): |
---|
462 | n/a | yield from q.put(1) |
---|
463 | n/a | return True |
---|
464 | n/a | |
---|
465 | n/a | @asyncio.coroutine |
---|
466 | n/a | def test(): |
---|
467 | n/a | return (yield from q.get()) |
---|
468 | n/a | |
---|
469 | n/a | t = asyncio.Task(queue_put(), loop=self.loop) |
---|
470 | n/a | self.assertEqual(1, self.loop.run_until_complete(test())) |
---|
471 | n/a | self.assertTrue(t.done()) |
---|
472 | n/a | self.assertTrue(t.result()) |
---|
473 | n/a | |
---|
474 | n/a | def test_put_cancelled_race(self): |
---|
475 | n/a | q = asyncio.Queue(loop=self.loop, maxsize=1) |
---|
476 | n/a | |
---|
477 | n/a | put_a = asyncio.Task(q.put('a'), loop=self.loop) |
---|
478 | n/a | put_b = asyncio.Task(q.put('b'), loop=self.loop) |
---|
479 | n/a | put_c = asyncio.Task(q.put('X'), loop=self.loop) |
---|
480 | n/a | |
---|
481 | n/a | test_utils.run_briefly(self.loop) |
---|
482 | n/a | self.assertTrue(put_a.done()) |
---|
483 | n/a | self.assertFalse(put_b.done()) |
---|
484 | n/a | |
---|
485 | n/a | put_c.cancel() |
---|
486 | n/a | test_utils.run_briefly(self.loop) |
---|
487 | n/a | self.assertTrue(put_c.done()) |
---|
488 | n/a | self.assertEqual(q.get_nowait(), 'a') |
---|
489 | n/a | test_utils.run_briefly(self.loop) |
---|
490 | n/a | self.assertEqual(q.get_nowait(), 'b') |
---|
491 | n/a | |
---|
492 | n/a | self.loop.run_until_complete(put_b) |
---|
493 | n/a | |
---|
494 | n/a | def test_put_with_waiting_getters(self): |
---|
495 | n/a | q = asyncio.Queue(loop=self.loop) |
---|
496 | n/a | t = asyncio.Task(q.get(), loop=self.loop) |
---|
497 | n/a | test_utils.run_briefly(self.loop) |
---|
498 | n/a | self.loop.run_until_complete(q.put('a')) |
---|
499 | n/a | self.assertEqual(self.loop.run_until_complete(t), 'a') |
---|
500 | n/a | |
---|
501 | n/a | def test_why_are_putters_waiting(self): |
---|
502 | n/a | # From issue #265. |
---|
503 | n/a | |
---|
504 | n/a | queue = asyncio.Queue(2, loop=self.loop) |
---|
505 | n/a | |
---|
506 | n/a | @asyncio.coroutine |
---|
507 | n/a | def putter(item): |
---|
508 | n/a | yield from queue.put(item) |
---|
509 | n/a | |
---|
510 | n/a | @asyncio.coroutine |
---|
511 | n/a | def getter(): |
---|
512 | n/a | yield |
---|
513 | n/a | num = queue.qsize() |
---|
514 | n/a | for _ in range(num): |
---|
515 | n/a | item = queue.get_nowait() |
---|
516 | n/a | |
---|
517 | n/a | t0 = putter(0) |
---|
518 | n/a | t1 = putter(1) |
---|
519 | n/a | t2 = putter(2) |
---|
520 | n/a | t3 = putter(3) |
---|
521 | n/a | self.loop.run_until_complete( |
---|
522 | n/a | asyncio.gather(getter(), t0, t1, t2, t3, loop=self.loop)) |
---|
523 | n/a | |
---|
524 | n/a | |
---|
525 | n/a | class LifoQueueTests(_QueueTestBase): |
---|
526 | n/a | |
---|
527 | n/a | def test_order(self): |
---|
528 | n/a | q = asyncio.LifoQueue(loop=self.loop) |
---|
529 | n/a | for i in [1, 3, 2]: |
---|
530 | n/a | q.put_nowait(i) |
---|
531 | n/a | |
---|
532 | n/a | items = [q.get_nowait() for _ in range(3)] |
---|
533 | n/a | self.assertEqual([2, 3, 1], items) |
---|
534 | n/a | |
---|
535 | n/a | |
---|
536 | n/a | class PriorityQueueTests(_QueueTestBase): |
---|
537 | n/a | |
---|
538 | n/a | def test_order(self): |
---|
539 | n/a | q = asyncio.PriorityQueue(loop=self.loop) |
---|
540 | n/a | for i in [1, 3, 2]: |
---|
541 | n/a | q.put_nowait(i) |
---|
542 | n/a | |
---|
543 | n/a | items = [q.get_nowait() for _ in range(3)] |
---|
544 | n/a | self.assertEqual([1, 2, 3], items) |
---|
545 | n/a | |
---|
546 | n/a | |
---|
547 | n/a | class _QueueJoinTestMixin: |
---|
548 | n/a | |
---|
549 | n/a | q_class = None |
---|
550 | n/a | |
---|
551 | n/a | def test_task_done_underflow(self): |
---|
552 | n/a | q = self.q_class(loop=self.loop) |
---|
553 | n/a | self.assertRaises(ValueError, q.task_done) |
---|
554 | n/a | |
---|
555 | n/a | def test_task_done(self): |
---|
556 | n/a | q = self.q_class(loop=self.loop) |
---|
557 | n/a | for i in range(100): |
---|
558 | n/a | q.put_nowait(i) |
---|
559 | n/a | |
---|
560 | n/a | accumulator = 0 |
---|
561 | n/a | |
---|
562 | n/a | # Two workers get items from the queue and call task_done after each. |
---|
563 | n/a | # Join the queue and assert all items have been processed. |
---|
564 | n/a | running = True |
---|
565 | n/a | |
---|
566 | n/a | @asyncio.coroutine |
---|
567 | n/a | def worker(): |
---|
568 | n/a | nonlocal accumulator |
---|
569 | n/a | |
---|
570 | n/a | while running: |
---|
571 | n/a | item = yield from q.get() |
---|
572 | n/a | accumulator += item |
---|
573 | n/a | q.task_done() |
---|
574 | n/a | |
---|
575 | n/a | @asyncio.coroutine |
---|
576 | n/a | def test(): |
---|
577 | n/a | tasks = [asyncio.Task(worker(), loop=self.loop) |
---|
578 | n/a | for index in range(2)] |
---|
579 | n/a | |
---|
580 | n/a | yield from q.join() |
---|
581 | n/a | return tasks |
---|
582 | n/a | |
---|
583 | n/a | tasks = self.loop.run_until_complete(test()) |
---|
584 | n/a | self.assertEqual(sum(range(100)), accumulator) |
---|
585 | n/a | |
---|
586 | n/a | # close running generators |
---|
587 | n/a | running = False |
---|
588 | n/a | for i in range(len(tasks)): |
---|
589 | n/a | q.put_nowait(0) |
---|
590 | n/a | self.loop.run_until_complete(asyncio.wait(tasks, loop=self.loop)) |
---|
591 | n/a | |
---|
592 | n/a | def test_join_empty_queue(self): |
---|
593 | n/a | q = self.q_class(loop=self.loop) |
---|
594 | n/a | |
---|
595 | n/a | # Test that a queue join()s successfully, and before anything else |
---|
596 | n/a | # (done twice for insurance). |
---|
597 | n/a | |
---|
598 | n/a | @asyncio.coroutine |
---|
599 | n/a | def join(): |
---|
600 | n/a | yield from q.join() |
---|
601 | n/a | yield from q.join() |
---|
602 | n/a | |
---|
603 | n/a | self.loop.run_until_complete(join()) |
---|
604 | n/a | |
---|
605 | n/a | def test_format(self): |
---|
606 | n/a | q = self.q_class(loop=self.loop) |
---|
607 | n/a | self.assertEqual(q._format(), 'maxsize=0') |
---|
608 | n/a | |
---|
609 | n/a | q._unfinished_tasks = 2 |
---|
610 | n/a | self.assertEqual(q._format(), 'maxsize=0 tasks=2') |
---|
611 | n/a | |
---|
612 | n/a | |
---|
613 | n/a | class QueueJoinTests(_QueueJoinTestMixin, _QueueTestBase): |
---|
614 | n/a | q_class = asyncio.Queue |
---|
615 | n/a | |
---|
616 | n/a | |
---|
617 | n/a | class LifoQueueJoinTests(_QueueJoinTestMixin, _QueueTestBase): |
---|
618 | n/a | q_class = asyncio.LifoQueue |
---|
619 | n/a | |
---|
620 | n/a | |
---|
621 | n/a | class PriorityQueueJoinTests(_QueueJoinTestMixin, _QueueTestBase): |
---|
622 | n/a | q_class = asyncio.PriorityQueue |
---|
623 | n/a | |
---|
624 | n/a | |
---|
625 | n/a | if __name__ == '__main__': |
---|
626 | n/a | unittest.main() |
---|