summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_asyncio/test_queues.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2015-09-28 20:35:54 (GMT)
committerGuido van Rossum <guido@python.org>2015-09-28 20:35:54 (GMT)
commit174d059248b47fca19781b79e5da2d927bd28cc7 (patch)
tree16d7a08386049690d22152f5a98a81e53135b60d /Lib/test/test_asyncio/test_queues.py
parentff6ee25102acbfa61e30a410c3fc75311a3413f0 (diff)
parentd2f184652ca992c7653730540fa1da45489c451e (diff)
downloadcpython-174d059248b47fca19781b79e5da2d927bd28cc7.zip
cpython-174d059248b47fca19781b79e5da2d927bd28cc7.tar.gz
cpython-174d059248b47fca19781b79e5da2d927bd28cc7.tar.bz2
Issue #25233: Rewrite the guts of Queue to be more understandable and correct. (Merge 3.5->default.)
Diffstat (limited to 'Lib/test/test_asyncio/test_queues.py')
-rw-r--r--Lib/test/test_asyncio/test_queues.py55
1 files changed, 48 insertions, 7 deletions
diff --git a/Lib/test/test_asyncio/test_queues.py b/Lib/test/test_asyncio/test_queues.py
index 8e38175..591a9bb 100644
--- a/Lib/test/test_asyncio/test_queues.py
+++ b/Lib/test/test_asyncio/test_queues.py
@@ -271,6 +271,29 @@ class QueueGetTests(_QueueTestBase):
self.assertEqual(self.loop.run_until_complete(q.get()), 'a')
self.assertEqual(self.loop.run_until_complete(q.get()), 'b')
+ def test_why_are_getters_waiting(self):
+ # From issue #268.
+
+ @asyncio.coroutine
+ def consumer(queue, num_expected):
+ for _ in range(num_expected):
+ yield from queue.get()
+
+ @asyncio.coroutine
+ def producer(queue, num_items):
+ for i in range(num_items):
+ yield from queue.put(i)
+
+ queue_size = 1
+ producer_num_items = 5
+ q = asyncio.Queue(queue_size, loop=self.loop)
+
+ self.loop.run_until_complete(
+ asyncio.gather(producer(q, producer_num_items),
+ consumer(q, producer_num_items),
+ loop=self.loop),
+ )
+
class QueuePutTests(_QueueTestBase):
@@ -377,13 +400,8 @@ class QueuePutTests(_QueueTestBase):
loop.run_until_complete(reader3)
- # reader2 will receive `2`, because it was added to the
- # queue of pending readers *before* put_nowaits were called.
- self.assertEqual(reader2.result(), 2)
- # reader3 will receive `1`, because reader1 was cancelled
- # before is had a chance to execute, and `2` was already
- # pushed to reader2 by second `put_nowait`.
- self.assertEqual(reader3.result(), 1)
+ # It is undefined in which order concurrent readers receive results.
+ self.assertEqual({reader2.result(), reader3.result()}, {1, 2})
def test_put_cancel_drop(self):
@@ -479,6 +497,29 @@ class QueuePutTests(_QueueTestBase):
self.loop.run_until_complete(q.put('a'))
self.assertEqual(self.loop.run_until_complete(t), 'a')
+ def test_why_are_putters_waiting(self):
+ # From issue #265.
+
+ queue = asyncio.Queue(2, loop=self.loop)
+
+ @asyncio.coroutine
+ def putter(item):
+ yield from queue.put(item)
+
+ @asyncio.coroutine
+ def getter():
+ yield
+ num = queue.qsize()
+ for _ in range(num):
+ item = queue.get_nowait()
+
+ t0 = putter(0)
+ t1 = putter(1)
+ t2 = putter(2)
+ t3 = putter(3)
+ self.loop.run_until_complete(
+ asyncio.gather(getter(), t0, t1, t2, t3, loop=self.loop))
+
class LifoQueueTests(_QueueTestBase):