Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6402de7

Browse filesBrowse files
committed
simplify and unify tests
1 parent 4b127b6 commit 6402de7
Copy full SHA for 6402de7

File tree

Expand file treeCollapse file tree

1 file changed

+62
-29
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+62
-29
lines changed

‎Lib/test/test_asyncio/test_queues.py

Copy file name to clipboardExpand all lines: Lib/test/test_asyncio/test_queues.py
+62-29Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -570,41 +570,49 @@ async def test_shutdown_repr(self):
570570
self.assertIn("shutdown-immediate", repr(q))
571571

572572
async def test_get_shutdown_immediate(self):
573+
q = self.q_class()
573574
results = []
574-
maxsize = 2
575-
delay = 1e-3
575+
go = asyncio.Event()
576576

577-
async def get_once(q):
577+
async def get_once(q, go):
578+
await go.wait()
578579
try:
579580
msg = await q.get()
580581
results.append(False)
581582
except asyncio.QueueShutDown:
582583
results.append(True)
583584
return True
584585

585-
async def shutdown(q, delay, immediate):
586-
await asyncio.sleep(delay)
586+
async def shutdown(q, go, immediate):
587587
q.shutdown(immediate)
588+
go.set()
588589
return True
589590

590-
q = self.q_class(maxsize)
591-
t = [asyncio.create_task(get_once(q)) for _ in range(maxsize)]
592-
t += [asyncio.create_task(shutdown(q, delay, True))]
591+
tasks = (
592+
(get_once, (q, go)),
593+
(get_once, (q, go)),
594+
)
595+
t = []
596+
for coro, params in tasks:
597+
t.append(asyncio.create_task(coro(*params)))
598+
t.append(asyncio.create_task(shutdown(q, go, True)))
593599
res = await asyncio.gather(*t)
594600

595-
self.assertEqual(results, [True]*maxsize)
601+
self.assertEqual(results, [True]*len(tasks))
596602

597603

598-
async def test_put_shutdown(self):
599-
maxsize = 2
604+
async def _put_shutdown(self, immediate):
605+
q = self.q_class(2)
600606
results = []
601607
go = asyncio.Event()
608+
await q.put("Y")
609+
await q.put("D")
610+
# queue fulled
602611

603-
async def put_twice(q, go, msg):
604-
await q.put(msg)
612+
async def put_once(q, go, msg):
605613
await go.wait()
606614
try:
607-
await q.put(msg+maxsize)
615+
await q.put(msg)
608616
results.append(False)
609617
except asyncio.QueueShutDown:
610618
results.append(True)
@@ -614,24 +622,37 @@ async def shutdown(q, go, immediate):
614622
q.shutdown(immediate)
615623
go.set()
616624

617-
q = self.q_class(maxsize)
618-
t = [asyncio.create_task(put_twice(q, go, i+1)) for i in range(maxsize)]
619-
t += [asyncio.create_task(shutdown(q, go, False))]
625+
tasks = (
626+
(put_once, (q, go, 100)),
627+
(put_once, (q, go, 200)),
628+
)
629+
t = []
630+
for coro, params in tasks:
631+
t.append(asyncio.create_task(coro(*params)))
632+
t.append(asyncio.create_task(shutdown(q, go, immediate)))
620633
res = await asyncio.gather(*t)
621634

622-
self.assertEqual(results, [True]*maxsize)
635+
self.assertEqual(results, [True]*len(tasks))
623636

637+
async def test_put_shutdown(self):
638+
return await self._put_shutdown(False)
624639

625-
async def test_put_and_join_shutdown(self):
626-
maxsize = 2
640+
async def test_put_shutdown_immediate(self):
641+
return await self._put_shutdown(True)
642+
643+
644+
async def _put_and_join_shutdown(self, immediate):
645+
q = self.q_class(2)
627646
results = []
628647
go = asyncio.Event()
648+
await q.put("Y")
649+
await q.put("D")
650+
# queue fulled
629651

630-
async def put_twice(q, go, msg):
631-
await q.put(msg)
652+
async def put_once(q, go, msg):
632653
await go.wait()
633654
try:
634-
await q.put(msg+100)
655+
await q.put(msg)
635656
results.append(False)
636657
except asyncio.QueueShutDown:
637658
results.append(True)
@@ -641,19 +662,31 @@ async def shutdown(q, go, immediate):
641662
q.shutdown(immediate)
642663
go.set()
643664

644-
async def join(q, delay):
665+
async def join(q, go):
645666
await go.wait()
646667
await q.join()
647668
results.append(True)
648669
return True
649670

650-
q = self.q_class(maxsize)
651-
t = [asyncio.create_task(put_twice(q, go, i+1)) for i in range(maxsize)]
652-
t += [asyncio.create_task(shutdown(q, go, True)),
653-
asyncio.create_task(join(q, go))]
671+
tasks = (
672+
(put_once, (q, go, 'E')),
673+
(put_once, (q, go, 'W')),
674+
(join, (q, go)),
675+
(join, (q, go)),
676+
)
677+
t = []
678+
for coro, params in tasks:
679+
t.append(asyncio.create_task(coro(*params)))
680+
t.append(asyncio.create_task(shutdown(q, go, immediate)))
654681
res = await asyncio.gather(*t)
655682

656-
self.assertEqual(results, [True]*(maxsize+1))
683+
self.assertEqual(results, [True]*len(tasks))
684+
685+
async def test_put_and_join_shutdown(self):
686+
return await self._put_and_join_shutdown(False)
687+
688+
async def test_put_and_join_shutdown_immediate(self):
689+
return await self._put_and_join_shutdown(True)
657690

658691

659692
class QueueShutdownTests(

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.