1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
|
"""Tests for queues.py"""
import asyncio
import time
import re
import pytest
import janus
async def close(_q):
for i in range(5):
time.sleep(0.001)
if not _q._sync_mutex.locked():
break
else:
assert not _q._sync_mutex.locked()
await _q.aclose()
assert _q.closed
assert _q.sync_q.closed
assert _q.async_q.closed
class TestQueueBasic:
async def _test_repr_or_str(self, fn, expect_id):
"""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()).
"""
_q = janus.Queue()
q = _q.async_q
assert fn(q).startswith("<Queue")
id_is_present = hex(id(q)) in fn(q)
assert expect_id == id_is_present
loop = asyncio.get_running_loop()
async def add_getter():
_q = janus.Queue()
q = _q.async_q
# Start a task that waits to get.
loop.create_task(q.get())
# Let it start waiting.
await asyncio.sleep(0.1)
assert "_getters[1]" in fn(q)
# resume q.get coroutine to finish generator
q.put_nowait(0)
await add_getter()
async def add_putter():
_q = janus.Queue(maxsize=1)
q = _q.async_q
q.put_nowait(1)
# Start a task that waits to put.
loop.create_task(q.put(2))
# Let it start waiting.
await asyncio.sleep(0.1)
assert "_putters[1]" in fn(q)
# resume q.put coroutine to finish generator
q.get_nowait()
await add_putter()
_q = janus.Queue()
q = _q.async_q
q.put_nowait(1)
assert "_queue=[1]" in fn(q)
# def test_repr(self):
# self._test_repr_or_str(repr, True)
# def test_str(self):
# self._test_repr_or_str(str, False)
@pytest.mark.asyncio
async def test_empty(self):
_q = janus.Queue()
q = _q.async_q
assert q.empty()
q.put_nowait(1)
assert not q.empty()
assert 1 == q.get_nowait()
assert q.empty()
await close(_q)
@pytest.mark.asyncio
async def test_full(self):
_q = janus.Queue()
q = _q.async_q
assert not q.full()
_q = janus.Queue(maxsize=1)
q = _q.async_q
q.put_nowait(1)
assert q.full()
await close(_q)
@pytest.mark.asyncio
async def test_order(self):
_q = janus.Queue()
q = _q.async_q
for i in [1, 3, 2]:
q.put_nowait(i)
items = [q.get_nowait() for _ in range(3)]
assert [1, 3, 2] == items
await close(_q)
@pytest.mark.asyncio
async def test_maxsize(self):
loop = asyncio.get_running_loop()
_q = janus.Queue(maxsize=2)
q = _q.async_q
assert 2 == q.maxsize
have_been_put = []
fut = loop.create_future()
async def putter():
for i in range(3):
await q.put(i)
have_been_put.append(i)
if i == q.maxsize - 1:
fut.set_result(None)
return True
t = loop.create_task(putter())
await fut
# The putter is blocked after putting two items.
assert [0, 1] == have_been_put
assert 0 == q.get_nowait()
# Let the putter resume and put last item.
await t
assert [0, 1, 2] == have_been_put
assert 1 == q.get_nowait()
assert 2 == q.get_nowait()
await close(_q)
class TestQueueGetTests:
@pytest.mark.asyncio
async def test_blocking_get(self):
_q = janus.Queue()
q = _q.async_q
q.put_nowait(1)
res = await q.get()
assert 1 == res
await close(_q)
@pytest.mark.asyncio
async def test_get_with_putters(self):
loop = asyncio.get_running_loop()
_q = janus.Queue(1)
q = _q.async_q
q.put_nowait(1)
fut = loop.create_future()
async def put():
t = asyncio.ensure_future(q.put(2))
await asyncio.sleep(0.01)
fut.set_result(None)
return t
t = await put()
res = await q.get()
assert 1 == res
await t
assert 1 == q.qsize()
await close(_q)
@pytest.mark.asyncio
async def test_blocking_get_wait(self):
loop = asyncio.get_running_loop()
_q = janus.Queue()
q = _q.async_q
started = asyncio.Event()
finished = False
async def queue_get():
nonlocal finished
started.set()
res = await q.get()
finished = True
return res
async def queue_put():
loop.call_later(0.01, q.put_nowait, 1)
queue_get_task = loop.create_task(queue_get())
await started.wait()
assert not finished
res = await queue_get_task
assert finished
return res
res = await queue_put()
assert 1 == res
await close(_q)
@pytest.mark.asyncio
async def test_nonblocking_get(self):
_q = janus.Queue()
q = _q.async_q
q.put_nowait(1)
assert 1 == q.get_nowait()
_q.close()
await _q.wait_closed()
@pytest.mark.asyncio
async def test_nonblocking_get_exception(self):
_q = janus.Queue()
pytest.raises(asyncio.QueueEmpty, _q.async_q.get_nowait)
await close(_q)
@pytest.mark.asyncio
async def test_get_cancelled(self):
loop = asyncio.get_running_loop()
_q = janus.Queue()
q = _q.async_q
async def queue_get():
return await asyncio.wait_for(q.get(), 0.051)
async def test():
get_task = loop.create_task(queue_get())
await asyncio.sleep(0.01) # let the task start
q.put_nowait(1)
return await get_task
assert 1 == await test()
await close(_q)
@pytest.mark.asyncio
async def test_get_cancelled_race(self):
loop = asyncio.get_running_loop()
_q = janus.Queue()
q = _q.async_q
f1 = loop.create_future()
async def g1():
f1.set_result(None)
await q.get()
t1 = loop.create_task(g1())
t2 = loop.create_task(q.get())
await f1
await asyncio.sleep(0.01)
t1.cancel()
with pytest.raises(asyncio.CancelledError):
await t1
assert t1.done()
q.put_nowait("a")
await t2
assert t2.result() == "a"
await close(_q)
@pytest.mark.asyncio
async def test_get_with_waiting_putters(self):
loop = asyncio.get_running_loop()
_q = janus.Queue(maxsize=1)
q = _q.async_q
loop.create_task(q.put("a"))
loop.create_task(q.put("b"))
await asyncio.sleep(0.01)
assert await q.get() == "a"
assert await q.get() == "b"
await close(_q)
class TestQueuePut:
@pytest.mark.asyncio
async def test_blocking_put(self):
_q = janus.Queue()
q = _q.async_q
# No maxsize, won't block.
await q.put(1)
await close(_q)
@pytest.mark.asyncio
async def test_blocking_put_wait(self):
loop = asyncio.get_running_loop()
_q = janus.Queue(maxsize=1)
q = _q.async_q
started = asyncio.Event()
finished = False
async def queue_put():
nonlocal finished
started.set()
await q.put(1)
await q.put(2)
finished = True
async def queue_get():
loop.call_later(0.01, q.get_nowait)
queue_put_task = loop.create_task(queue_put())
await started.wait()
assert not finished
await queue_put_task
assert finished
await queue_get()
await close(_q)
@pytest.mark.asyncio
async def test_nonblocking_put(self):
_q = janus.Queue()
q = _q.async_q
q.put_nowait(1)
assert 1 == q.get_nowait()
await close(_q)
@pytest.mark.asyncio
async def test_nonblocking_put_exception(self):
_q = janus.Queue(maxsize=1)
q = _q.async_q
q.put_nowait(1)
pytest.raises(asyncio.QueueFull, q.put_nowait, 2)
await close(_q)
@pytest.mark.asyncio
async def test_float_maxsize(self):
_q = janus.Queue(maxsize=1.3)
q = _q.async_q
q.put_nowait(1)
q.put_nowait(2)
assert q.full()
pytest.raises(asyncio.QueueFull, q.put_nowait, 3)
_q.close()
await _q.wait_closed()
_q = janus.Queue(maxsize=1.3)
q = _q.async_q
async def queue_put():
await q.put(1)
await q.put(2)
assert q.full()
await queue_put()
await close(_q)
@pytest.mark.asyncio
async def test_put_cancelled(self):
loop = asyncio.get_running_loop()
_q = janus.Queue()
q = _q.async_q
async def queue_put():
await q.put(1)
return True
async def test():
return await q.get()
t = loop.create_task(queue_put())
assert 1 == await test()
assert t.done()
assert t.result()
await close(_q)
@pytest.mark.asyncio
async def test_put_cancelled_race(self):
loop = asyncio.get_running_loop()
_q = janus.Queue(maxsize=1)
q = _q.async_q
put_a = loop.create_task(q.put("a"))
put_b = loop.create_task(q.put("b"))
put_c = loop.create_task(q.put("X"))
await put_a
assert not put_b.done()
put_c.cancel()
with pytest.raises(asyncio.CancelledError):
await put_c
a = await q.get()
assert a == "a"
b = await q.get()
assert b == "b"
assert put_b.done()
assert q.qsize() == 0
await close(_q)
@pytest.mark.asyncio
async def test_put_with_waiting_getters(self):
loop = asyncio.get_running_loop()
fut = loop.create_future()
async def go():
fut.set_result(None)
ret = await q.get()
return ret
async def put():
await q.put("a")
_q = janus.Queue()
q = _q.async_q
t = loop.create_task(go())
await fut
await put()
assert await t == "a"
await close(_q)
class TestQueueShutdown:
@pytest.mark.asyncio
async def test_shutdown_empty(self):
_q = janus.Queue()
q = _q.async_q
q.shutdown()
with pytest.raises(janus.AsyncQueueShutDown):
await q.put("data")
with pytest.raises(janus.AsyncQueueShutDown):
await q.get()
with pytest.raises(janus.AsyncQueueShutDown):
q.get_nowait()
@pytest.mark.asyncio
async def test_shutdown_nonempty(self):
_q = janus.Queue()
q = _q.async_q
await q.put("data")
q.shutdown()
await q.get()
with pytest.raises(janus.AsyncQueueShutDown):
await q.get()
@pytest.mark.asyncio
async def test_shutdown_nonempty_get_nowait(self):
_q = janus.Queue()
q = _q.async_q
await q.put("data")
q.shutdown()
q.get_nowait()
with pytest.raises(janus.AsyncQueueShutDown):
q.get_nowait()
@pytest.mark.asyncio
async def test_shutdown_immediate(self):
_q = janus.Queue()
q = _q.async_q
await q.put("data")
q.shutdown(immediate=True)
with pytest.raises(janus.AsyncQueueShutDown):
await q.get()
with pytest.raises(janus.AsyncQueueShutDown):
q.get_nowait()
@pytest.mark.asyncio
async def test_shutdown_immediate_with_undone_tasks(self):
_q = janus.Queue()
q = _q.async_q
await q.put(1)
await q.put(2)
# artificial .task_done() without .get() for covering specific codeline
# in .shutdown(True)
q.task_done()
q.shutdown(True)
await close(_q)
@pytest.mark.asyncio
async def test_shutdown_putter(self):
_q = janus.Queue(maxsize=1)
q = _q.async_q
await q.put(1)
async def putter():
await q.put(2)
task = asyncio.create_task(putter())
# wait for the task start
await asyncio.sleep(0.01)
q.shutdown()
with pytest.raises(janus.AsyncQueueShutDown):
await task
await close(_q)
@pytest.mark.asyncio
async def test_shutdown_many_putters(self):
_q = janus.Queue(maxsize=1)
q = _q.async_q
await q.put(1)
async def putter(n):
await q.put(n)
tasks = []
for i in range(2):
tasks.append(asyncio.create_task(putter(i)))
# wait for the task start
await asyncio.sleep(0.01)
q.shutdown()
for task in tasks:
with pytest.raises(janus.AsyncQueueShutDown):
await task
await close(_q)
@pytest.mark.asyncio
async def test_shutdown_getter(self):
_q = janus.Queue()
q = _q.async_q
async def getter():
await q.get()
task = asyncio.create_task(getter())
# wait for the task start
await asyncio.sleep(0.01)
q.shutdown()
with pytest.raises(janus.AsyncQueueShutDown):
await task
await close(_q)
@pytest.mark.asyncio
async def test_shutdown_early_getter(self):
_q = janus.Queue()
q = _q.async_q
q.shutdown()
with pytest.raises(janus.AsyncQueueShutDown):
await q.get()
await close(_q)
class TestLifoQueue:
@pytest.mark.asyncio
async def test_order(self):
_q = janus.LifoQueue()
q = _q.async_q
for i in [1, 3, 2]:
q.put_nowait(i)
items = [q.get_nowait() for _ in range(3)]
assert [2, 3, 1] == items
await close(_q)
class TestPriorityQueue:
@pytest.mark.asyncio
async def test_order(self):
_q = janus.PriorityQueue()
q = _q.async_q
for i in [1, 3, 2]:
q.put_nowait(i)
items = [q.get_nowait() for _ in range(3)]
assert [1, 2, 3] == items
await close(_q)
class _QueueJoinTestMixin:
q_class = None
@pytest.mark.asyncio
async def test_task_done_underflow(self):
_q = self.q_class()
q = _q.async_q
with pytest.raises(
ValueError, match=re.escape("task_done() called too many times")
):
q.task_done()
await close(_q)
@pytest.mark.asyncio
async def test_task_done(self):
loop = asyncio.get_running_loop()
_q = self.q_class()
q = _q.async_q
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
async def worker():
nonlocal accumulator
while running:
item = await q.get()
accumulator += item
q.task_done()
async def test():
tasks = [loop.create_task(worker()) for index in range(2)]
await q.join()
return tasks
tasks = await test()
assert sum(range(100)) == accumulator
# close running generators
running = False
for i in range(len(tasks)):
q.put_nowait(0)
await asyncio.wait(tasks)
await close(_q)
@pytest.mark.asyncio
async def test_join_empty_queue(self):
_q = self.q_class()
q = _q.async_q
# Test that a queue join()s successfully, and before anything else
# (done twice for insurance).
async def join():
await q.join()
await q.join()
await join()
await close(_q)
class TestQueueJoin(_QueueJoinTestMixin):
q_class = janus.Queue
class TestLifoQueueJoin(_QueueJoinTestMixin):
q_class = janus.LifoQueue
class TestPriorityQueueJoin(_QueueJoinTestMixin):
q_class = janus.PriorityQueue
|