2013-10-17 17:40:50 -03:00
|
|
|
"""Tests for queues.py"""
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
import asyncio
|
2022-03-16 11:59:12 -03:00
|
|
|
import unittest
|
2022-01-22 07:28:53 -04:00
|
|
|
from types import GenericAlias
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
|
2018-06-02 00:34:09 -03:00
|
|
|
def tearDownModule():
|
|
|
|
asyncio.set_event_loop_policy(None)
|
|
|
|
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
class QueueBasicTests(unittest.IsolatedAsyncioTestCase):
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def _test_repr_or_str(self, fn, expect_id):
|
2013-10-17 17:40:50 -03:00
|
|
|
"""Test Queue's repr or str.
|
|
|
|
|
|
|
|
fn is repr or str. expect_id is True if we expect the Queue's id to
|
|
|
|
appear in fn(Queue()).
|
|
|
|
"""
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(fn(q).startswith('<Queue'), fn(q))
|
|
|
|
id_is_present = hex(id(q)) in fn(q)
|
|
|
|
self.assertEqual(expect_id, id_is_present)
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
# getters
|
|
|
|
q = asyncio.Queue()
|
|
|
|
async with asyncio.TaskGroup() as tg:
|
2013-10-17 17:40:50 -03:00
|
|
|
# Start a task that waits to get.
|
2022-03-16 11:59:12 -03:00
|
|
|
getter = tg.create_task(q.get())
|
2013-10-17 17:40:50 -03:00
|
|
|
# Let it start waiting.
|
2022-03-22 11:00:23 -03:00
|
|
|
await asyncio.sleep(0)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue('_getters[1]' in fn(q))
|
|
|
|
# resume q.get coroutine to finish generator
|
|
|
|
q.put_nowait(0)
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
self.assertEqual(0, await getter)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
# putters
|
|
|
|
q = asyncio.Queue(maxsize=1)
|
|
|
|
async with asyncio.TaskGroup() as tg:
|
2013-10-17 17:40:50 -03:00
|
|
|
q.put_nowait(1)
|
|
|
|
# Start a task that waits to put.
|
2022-03-16 11:59:12 -03:00
|
|
|
putter = tg.create_task(q.put(2))
|
2013-10-17 17:40:50 -03:00
|
|
|
# Let it start waiting.
|
2022-03-22 11:00:23 -03:00
|
|
|
await asyncio.sleep(0)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue('_putters[1]' in fn(q))
|
|
|
|
# resume q.put coroutine to finish generator
|
|
|
|
q.get_nowait()
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
self.assertTrue(putter.done())
|
|
|
|
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue()
|
2013-10-17 17:40:50 -03:00
|
|
|
q.put_nowait(1)
|
|
|
|
self.assertTrue('_queue=[1]' in fn(q))
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_repr(self):
|
|
|
|
await self._test_repr_or_str(repr, True)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_str(self):
|
|
|
|
await self._test_repr_or_str(str, False)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-01-22 07:28:53 -04:00
|
|
|
def test_generic_alias(self):
|
|
|
|
q = asyncio.Queue[int]
|
|
|
|
self.assertEqual(q.__args__, (int,))
|
|
|
|
self.assertIsInstance(q, GenericAlias)
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_empty(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(q.empty())
|
2022-03-16 11:59:12 -03:00
|
|
|
await q.put(1)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(q.empty())
|
2022-03-16 11:59:12 -03:00
|
|
|
self.assertEqual(1, await q.get())
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(q.empty())
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_full(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertFalse(q.full())
|
|
|
|
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue(maxsize=1)
|
2022-03-16 11:59:12 -03:00
|
|
|
await q.put(1)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(q.full())
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_order(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue()
|
2013-10-17 17:40:50 -03:00
|
|
|
for i in [1, 3, 2]:
|
2022-03-16 11:59:12 -03:00
|
|
|
await q.put(i)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
items = [await q.get() for _ in range(3)]
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual([1, 3, 2], items)
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_maxsize(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue(maxsize=2)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(2, q.maxsize)
|
|
|
|
have_been_put = []
|
|
|
|
|
2017-12-08 18:23:48 -04:00
|
|
|
async def putter():
|
2013-10-17 17:40:50 -03:00
|
|
|
for i in range(3):
|
2017-12-08 18:23:48 -04:00
|
|
|
await q.put(i)
|
2013-10-17 17:40:50 -03:00
|
|
|
have_been_put.append(i)
|
|
|
|
return True
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
t = asyncio.create_task(putter())
|
2022-03-22 11:00:23 -03:00
|
|
|
for i in range(2):
|
|
|
|
await asyncio.sleep(0)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
# The putter is blocked after putting two items.
|
|
|
|
self.assertEqual([0, 1], have_been_put)
|
|
|
|
self.assertEqual(0, await q.get())
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
# Let the putter resume and put last item.
|
2022-03-22 11:00:23 -03:00
|
|
|
await asyncio.sleep(0)
|
2022-03-16 11:59:12 -03:00
|
|
|
self.assertEqual([0, 1, 2], have_been_put)
|
|
|
|
self.assertEqual(1, await q.get())
|
|
|
|
self.assertEqual(2, await q.get())
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
self.assertTrue(t.done())
|
|
|
|
self.assertTrue(t.result())
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
class QueueGetTests(unittest.IsolatedAsyncioTestCase):
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_blocking_get(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue()
|
2013-10-17 17:40:50 -03:00
|
|
|
q.put_nowait(1)
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
self.assertEqual(1, await q.get())
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_get_with_putters(self):
|
|
|
|
loop = asyncio.get_running_loop()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue(1)
|
2022-03-16 11:59:12 -03:00
|
|
|
await q.put(1)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
waiter = loop.create_future()
|
2015-08-05 14:52:33 -03:00
|
|
|
q._putters.append(waiter)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
self.assertEqual(1, await q.get())
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(waiter.done())
|
|
|
|
self.assertIsNone(waiter.result())
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_blocking_get_wait(self):
|
|
|
|
loop = asyncio.get_running_loop()
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue()
|
|
|
|
started = asyncio.Event()
|
2013-10-17 17:40:50 -03:00
|
|
|
finished = False
|
|
|
|
|
2017-12-08 18:23:48 -04:00
|
|
|
async def queue_get():
|
2013-10-17 17:40:50 -03:00
|
|
|
nonlocal finished
|
|
|
|
started.set()
|
2017-12-08 18:23:48 -04:00
|
|
|
res = await q.get()
|
2013-10-17 17:40:50 -03:00
|
|
|
finished = True
|
|
|
|
return res
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
queue_get_task = asyncio.create_task(queue_get())
|
|
|
|
await started.wait()
|
|
|
|
self.assertFalse(finished)
|
2022-03-22 11:00:23 -03:00
|
|
|
loop.call_later(0.01, q.put_nowait, 1)
|
2022-03-16 11:59:12 -03:00
|
|
|
res = await queue_get_task
|
|
|
|
self.assertTrue(finished)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(1, res)
|
|
|
|
|
|
|
|
def test_nonblocking_get(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue()
|
2013-10-17 17:40:50 -03:00
|
|
|
q.put_nowait(1)
|
|
|
|
self.assertEqual(1, q.get_nowait())
|
|
|
|
|
|
|
|
def test_nonblocking_get_exception(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue()
|
2014-01-25 21:24:51 -04:00
|
|
|
self.assertRaises(asyncio.QueueEmpty, q.get_nowait)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_get_cancelled_race(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
t1 = asyncio.create_task(q.get())
|
|
|
|
t2 = asyncio.create_task(q.get())
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
await asyncio.sleep(0)
|
2013-10-17 17:40:50 -03:00
|
|
|
t1.cancel()
|
2022-03-16 11:59:12 -03:00
|
|
|
await asyncio.sleep(0)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(t1.done())
|
2022-03-16 11:59:12 -03:00
|
|
|
await q.put('a')
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
self.assertEqual('a', await t2)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_get_with_waiting_putters(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue(maxsize=1)
|
2022-03-16 11:59:12 -03:00
|
|
|
asyncio.create_task(q.put('a'))
|
|
|
|
asyncio.create_task(q.put('b'))
|
|
|
|
self.assertEqual(await q.get(), 'a')
|
|
|
|
self.assertEqual(await q.get(), 'b')
|
2015-09-28 11:42:34 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_why_are_getters_waiting(self):
|
2017-12-08 18:23:48 -04:00
|
|
|
async def consumer(queue, num_expected):
|
2015-09-28 11:42:34 -03:00
|
|
|
for _ in range(num_expected):
|
2017-12-08 18:23:48 -04:00
|
|
|
await queue.get()
|
2015-09-28 11:42:34 -03:00
|
|
|
|
2017-12-08 18:23:48 -04:00
|
|
|
async def producer(queue, num_items):
|
2015-09-28 11:42:34 -03:00
|
|
|
for i in range(num_items):
|
2017-12-08 18:23:48 -04:00
|
|
|
await queue.put(i)
|
2015-09-28 11:42:34 -03:00
|
|
|
|
|
|
|
producer_num_items = 5
|
2019-09-10 07:55:07 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
q = asyncio.Queue(1)
|
|
|
|
async with asyncio.TaskGroup() as tg:
|
|
|
|
tg.create_task(producer(q, producer_num_items))
|
|
|
|
tg.create_task(consumer(q, producer_num_items))
|
2017-11-07 13:35:23 -04:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_cancelled_getters_not_being_held_in_self_getters(self):
|
|
|
|
queue = asyncio.Queue(maxsize=5)
|
2017-12-08 18:23:48 -04:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
with self.assertRaises(TimeoutError):
|
|
|
|
await asyncio.wait_for(queue.get(), 0.1)
|
2017-11-07 13:35:23 -04:00
|
|
|
|
|
|
|
self.assertEqual(len(queue._getters), 0)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
class QueuePutTests(unittest.IsolatedAsyncioTestCase):
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_blocking_put(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
# No maxsize, won't block.
|
|
|
|
await q.put(1)
|
|
|
|
self.assertEqual(1, await q.get())
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_blocking_put_wait(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue(maxsize=1)
|
|
|
|
started = asyncio.Event()
|
2013-10-17 17:40:50 -03:00
|
|
|
finished = False
|
|
|
|
|
2017-12-08 18:23:48 -04:00
|
|
|
async def queue_put():
|
2013-10-17 17:40:50 -03:00
|
|
|
nonlocal finished
|
|
|
|
started.set()
|
2017-12-08 18:23:48 -04:00
|
|
|
await q.put(1)
|
|
|
|
await q.put(2)
|
2013-10-17 17:40:50 -03:00
|
|
|
finished = True
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
loop = asyncio.get_running_loop()
|
|
|
|
loop.call_later(0.01, q.get_nowait)
|
|
|
|
queue_put_task = asyncio.create_task(queue_put())
|
|
|
|
await started.wait()
|
|
|
|
self.assertFalse(finished)
|
|
|
|
await queue_put_task
|
|
|
|
self.assertTrue(finished)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_nonblocking_put(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue()
|
2013-10-17 17:40:50 -03:00
|
|
|
q.put_nowait(1)
|
|
|
|
self.assertEqual(1, q.get_nowait())
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_get_cancel_drop_one_pending_reader(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue()
|
2015-08-05 14:52:33 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
reader = asyncio.create_task(q.get())
|
2015-08-05 14:52:33 -03:00
|
|
|
|
2022-03-22 11:00:23 -03:00
|
|
|
await asyncio.sleep(0)
|
2015-08-05 14:52:33 -03:00
|
|
|
|
|
|
|
q.put_nowait(1)
|
|
|
|
q.put_nowait(2)
|
|
|
|
reader.cancel()
|
|
|
|
|
|
|
|
try:
|
2022-03-16 11:59:12 -03:00
|
|
|
await reader
|
2015-08-05 14:52:33 -03:00
|
|
|
except asyncio.CancelledError:
|
|
|
|
# try again
|
2022-03-16 11:59:12 -03:00
|
|
|
reader = asyncio.create_task(q.get())
|
|
|
|
await reader
|
2015-08-05 14:52:33 -03:00
|
|
|
|
|
|
|
result = reader.result()
|
|
|
|
# if we get 2, it means 1 got dropped!
|
|
|
|
self.assertEqual(1, result)
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_get_cancel_drop_many_pending_readers(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue()
|
2015-08-06 15:03:38 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async with asyncio.TaskGroup() as tg:
|
|
|
|
reader1 = tg.create_task(q.get())
|
|
|
|
reader2 = tg.create_task(q.get())
|
|
|
|
reader3 = tg.create_task(q.get())
|
2015-08-06 15:03:38 -03:00
|
|
|
|
2022-03-22 11:00:23 -03:00
|
|
|
await asyncio.sleep(0)
|
2015-08-06 15:03:38 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
q.put_nowait(1)
|
|
|
|
q.put_nowait(2)
|
|
|
|
reader1.cancel()
|
2015-08-06 15:03:38 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
with self.assertRaises(asyncio.CancelledError):
|
|
|
|
await reader1
|
2015-08-06 15:03:38 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
await reader3
|
2015-08-06 15:03:38 -03:00
|
|
|
|
2015-09-28 11:42:34 -03:00
|
|
|
# It is undefined in which order concurrent readers receive results.
|
|
|
|
self.assertEqual({reader2.result(), reader3.result()}, {1, 2})
|
2015-08-06 15:03:38 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_put_cancel_drop(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue(1)
|
2015-08-05 14:52:33 -03:00
|
|
|
|
|
|
|
q.put_nowait(1)
|
|
|
|
|
|
|
|
# putting a second item in the queue has to block (qsize=1)
|
2022-03-16 11:59:12 -03:00
|
|
|
writer = asyncio.create_task(q.put(2))
|
2022-03-22 11:00:23 -03:00
|
|
|
await asyncio.sleep(0)
|
2015-08-05 14:52:33 -03:00
|
|
|
|
|
|
|
value1 = q.get_nowait()
|
|
|
|
self.assertEqual(value1, 1)
|
|
|
|
|
|
|
|
writer.cancel()
|
|
|
|
try:
|
2022-03-16 11:59:12 -03:00
|
|
|
await writer
|
2015-08-05 14:52:33 -03:00
|
|
|
except asyncio.CancelledError:
|
|
|
|
# try again
|
2022-03-16 11:59:12 -03:00
|
|
|
writer = asyncio.create_task(q.put(2))
|
|
|
|
await writer
|
2015-08-05 14:52:33 -03:00
|
|
|
|
|
|
|
value2 = q.get_nowait()
|
|
|
|
self.assertEqual(value2, 2)
|
|
|
|
self.assertEqual(q.qsize(), 0)
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
def test_nonblocking_put_exception(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue(maxsize=1, )
|
2013-10-17 17:40:50 -03:00
|
|
|
q.put_nowait(1)
|
2014-01-25 21:24:51 -04:00
|
|
|
self.assertRaises(asyncio.QueueFull, q.put_nowait, 2)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_float_maxsize(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue(maxsize=1.3, )
|
2014-06-17 18:36:21 -03:00
|
|
|
q.put_nowait(1)
|
|
|
|
q.put_nowait(2)
|
|
|
|
self.assertTrue(q.full())
|
|
|
|
self.assertRaises(asyncio.QueueFull, q.put_nowait, 3)
|
|
|
|
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue(maxsize=1.3, )
|
2017-12-08 18:23:48 -04:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
await q.put(1)
|
|
|
|
await q.put(2)
|
|
|
|
self.assertTrue(q.full())
|
2014-06-17 18:36:21 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_put_cancelled(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2017-12-08 18:23:48 -04:00
|
|
|
async def queue_put():
|
|
|
|
await q.put(1)
|
2013-10-17 17:40:50 -03:00
|
|
|
return True
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
t = asyncio.create_task(queue_put())
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
self.assertEqual(1, await q.get())
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertTrue(t.done())
|
|
|
|
self.assertTrue(t.result())
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_put_cancelled_race(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue(maxsize=1)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
put_a = asyncio.create_task(q.put('a'))
|
|
|
|
put_b = asyncio.create_task(q.put('b'))
|
|
|
|
put_c = asyncio.create_task(q.put('X'))
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
await asyncio.sleep(0)
|
2014-06-25 18:11:21 -03:00
|
|
|
self.assertTrue(put_a.done())
|
|
|
|
self.assertFalse(put_b.done())
|
|
|
|
|
|
|
|
put_c.cancel()
|
2022-03-16 11:59:12 -03:00
|
|
|
await asyncio.sleep(0)
|
2014-06-25 18:11:21 -03:00
|
|
|
self.assertTrue(put_c.done())
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(q.get_nowait(), 'a')
|
2022-03-16 11:59:12 -03:00
|
|
|
await asyncio.sleep(0)
|
2014-06-25 18:11:21 -03:00
|
|
|
self.assertEqual(q.get_nowait(), 'b')
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
await put_b
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_put_with_waiting_getters(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.Queue()
|
2022-03-16 11:59:12 -03:00
|
|
|
t = asyncio.create_task(q.get())
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
await q.put('a')
|
|
|
|
self.assertEqual(await t, 'a')
|
2020-11-24 14:08:54 -04:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_why_are_putters_waiting(self):
|
|
|
|
queue = asyncio.Queue(2)
|
2015-09-28 11:42:34 -03:00
|
|
|
|
2017-12-08 18:23:48 -04:00
|
|
|
async def putter(item):
|
|
|
|
await queue.put(item)
|
2015-09-28 11:42:34 -03:00
|
|
|
|
2017-12-08 18:23:48 -04:00
|
|
|
async def getter():
|
2018-10-02 14:53:06 -03:00
|
|
|
await asyncio.sleep(0)
|
2015-09-28 11:42:34 -03:00
|
|
|
num = queue.qsize()
|
|
|
|
for _ in range(num):
|
2022-03-16 11:59:12 -03:00
|
|
|
queue.get_nowait()
|
2015-09-28 11:42:34 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async with asyncio.TaskGroup() as tg:
|
|
|
|
tg.create_task(getter())
|
|
|
|
tg.create_task(putter(0))
|
|
|
|
tg.create_task(putter(1))
|
|
|
|
tg.create_task(putter(2))
|
|
|
|
tg.create_task(putter(3))
|
2018-01-25 19:45:43 -04:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_cancelled_puts_not_being_held_in_self_putters(self):
|
2018-01-25 19:45:43 -04:00
|
|
|
# Full queue.
|
2020-11-24 14:08:54 -04:00
|
|
|
queue = asyncio.Queue(maxsize=1)
|
2018-01-25 19:45:43 -04:00
|
|
|
queue.put_nowait(1)
|
|
|
|
|
|
|
|
# Task waiting for space to put an item in the queue.
|
2022-03-16 11:59:12 -03:00
|
|
|
put_task = asyncio.create_task(queue.put(1))
|
2022-03-22 11:00:23 -03:00
|
|
|
await asyncio.sleep(0)
|
2018-01-25 19:45:43 -04:00
|
|
|
|
|
|
|
# Check that the putter is correctly removed from queue._putters when
|
|
|
|
# the task is canceled.
|
|
|
|
self.assertEqual(len(queue._putters), 1)
|
|
|
|
put_task.cancel()
|
|
|
|
with self.assertRaises(asyncio.CancelledError):
|
2022-03-16 11:59:12 -03:00
|
|
|
await put_task
|
2018-01-25 19:45:43 -04:00
|
|
|
self.assertEqual(len(queue._putters), 0)
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_cancelled_put_silence_value_error_exception(self):
|
2018-01-25 19:45:43 -04:00
|
|
|
# Full Queue.
|
2020-11-24 14:08:54 -04:00
|
|
|
queue = asyncio.Queue(1)
|
2018-01-25 19:45:43 -04:00
|
|
|
queue.put_nowait(1)
|
|
|
|
|
|
|
|
# Task waiting for space to put a item in the queue.
|
2022-03-16 11:59:12 -03:00
|
|
|
put_task = asyncio.create_task(queue.put(1))
|
2022-03-22 11:00:23 -03:00
|
|
|
await asyncio.sleep(0)
|
2018-01-25 19:45:43 -04:00
|
|
|
|
|
|
|
# get_nowait() remove the future of put_task from queue._putters.
|
|
|
|
queue.get_nowait()
|
|
|
|
# When canceled, queue.put is going to remove its future from
|
|
|
|
# self._putters but it was removed previously by queue.get_nowait().
|
|
|
|
put_task.cancel()
|
|
|
|
|
|
|
|
# The ValueError exception triggered by queue._putters.remove(putter)
|
|
|
|
# inside queue.put should be silenced.
|
|
|
|
# If the ValueError is silenced we should catch a CancelledError.
|
|
|
|
with self.assertRaises(asyncio.CancelledError):
|
2022-03-16 11:59:12 -03:00
|
|
|
await put_task
|
2018-01-25 19:45:43 -04:00
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
class LifoQueueTests(unittest.IsolatedAsyncioTestCase):
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_order(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.LifoQueue()
|
2013-10-17 17:40:50 -03:00
|
|
|
for i in [1, 3, 2]:
|
2022-03-16 11:59:12 -03:00
|
|
|
await q.put(i)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
items = [await q.get() for _ in range(3)]
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual([2, 3, 1], items)
|
|
|
|
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
class PriorityQueueTests(unittest.IsolatedAsyncioTestCase):
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_order(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = asyncio.PriorityQueue()
|
2013-10-17 17:40:50 -03:00
|
|
|
for i in [1, 3, 2]:
|
2022-03-16 11:59:12 -03:00
|
|
|
await q.put(i)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
items = [await q.get() for _ in range(3)]
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual([1, 2, 3], items)
|
|
|
|
|
|
|
|
|
2015-04-20 13:24:24 -03:00
|
|
|
class _QueueJoinTestMixin:
|
|
|
|
|
|
|
|
q_class = None
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_task_done_underflow(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = self.q_class()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertRaises(ValueError, q.task_done)
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_task_done(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = self.q_class()
|
2013-10-17 17:40:50 -03:00
|
|
|
for i in range(100):
|
|
|
|
q.put_nowait(i)
|
|
|
|
|
|
|
|
accumulator = 0
|
|
|
|
|
|
|
|
# Two workers get items from the queue and call task_done after each.
|
|
|
|
# Join the queue and assert all items have been processed.
|
|
|
|
running = True
|
|
|
|
|
2017-12-08 18:23:48 -04:00
|
|
|
async def worker():
|
2013-10-17 17:40:50 -03:00
|
|
|
nonlocal accumulator
|
|
|
|
|
|
|
|
while running:
|
2017-12-08 18:23:48 -04:00
|
|
|
item = await q.get()
|
2013-10-17 17:40:50 -03:00
|
|
|
accumulator += item
|
|
|
|
q.task_done()
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async with asyncio.TaskGroup() as tg:
|
|
|
|
tasks = [tg.create_task(worker())
|
2014-06-25 18:11:21 -03:00
|
|
|
for index in range(2)]
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2017-12-08 18:23:48 -04:00
|
|
|
await q.join()
|
2022-03-16 11:59:12 -03:00
|
|
|
self.assertEqual(sum(range(100)), accumulator)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
# close running generators
|
|
|
|
running = False
|
|
|
|
for i in range(len(tasks)):
|
|
|
|
q.put_nowait(0)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_join_empty_queue(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = self.q_class()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
# Test that a queue join()s successfully, and before anything else
|
|
|
|
# (done twice for insurance).
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
await q.join()
|
|
|
|
await q.join()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
async def test_format(self):
|
2020-11-24 14:08:54 -04:00
|
|
|
q = self.q_class()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(q._format(), 'maxsize=0')
|
|
|
|
|
|
|
|
q._unfinished_tasks = 2
|
|
|
|
self.assertEqual(q._format(), 'maxsize=0 tasks=2')
|
|
|
|
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
class QueueJoinTests(_QueueJoinTestMixin, unittest.IsolatedAsyncioTestCase):
|
2015-04-20 13:24:24 -03:00
|
|
|
q_class = asyncio.Queue
|
|
|
|
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
class LifoQueueJoinTests(_QueueJoinTestMixin, unittest.IsolatedAsyncioTestCase):
|
2015-04-20 13:24:24 -03:00
|
|
|
q_class = asyncio.LifoQueue
|
|
|
|
|
|
|
|
|
2022-03-16 11:59:12 -03:00
|
|
|
class PriorityQueueJoinTests(_QueueJoinTestMixin, unittest.IsolatedAsyncioTestCase):
|
2015-04-20 13:24:24 -03:00
|
|
|
q_class = asyncio.PriorityQueue
|
|
|
|
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|