Skip to content

Commit c2c90cb

Browse files
committed
Use asyncio events to get deterministic execution.
1 parent 9220223 commit c2c90cb

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

Lib/test/test_warnings/__init__.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,23 +1540,33 @@ class AsyncTests(BaseTest):
15401540
def test_async_context(self):
15411541
import asyncio
15421542

1543+
# Events to force the execution interleaving we want.
1544+
step_a1 = asyncio.Event()
1545+
step_a2 = asyncio.Event()
1546+
step_b1 = asyncio.Event()
1547+
step_b2 = asyncio.Event()
1548+
15431549
async def run_a():
15441550
with self.module.catch_warnings(record=True) as w:
1545-
await asyncio.sleep(0)
1551+
await step_a1.wait()
15461552
# The warning emitted here should be caught be the enclosing
15471553
# context manager.
15481554
self.module.warn('run_a warning', UserWarning)
1549-
await asyncio.sleep(0)
1555+
step_b1.set()
1556+
await step_a2.wait()
15501557
self.assertEqual(len(w), 1)
15511558
self.assertEqual(w[0].message.args[0], 'run_a warning')
1559+
step_b2.set()
15521560

15531561
async def run_b():
15541562
with self.module.catch_warnings(record=True) as w:
1555-
await asyncio.sleep(0)
1563+
step_a1.set()
1564+
await step_b1.wait()
15561565
# The warning emitted here should be caught be the enclosing
15571566
# context manager.
15581567
self.module.warn('run_b warning', UserWarning)
1559-
await asyncio.sleep(0)
1568+
step_a2.set()
1569+
await step_b2.wait()
15601570
self.assertEqual(len(w), 1)
15611571
self.assertEqual(w[0].message.args[0], 'run_b warning')
15621572

0 commit comments

Comments
 (0)