Skip to content

Commit 290c99b

Browse files
committed
PYTHON-5784 Use _run() helper for async tests to match test_network_layer.py style
1 parent 8786964 commit 290c99b

1 file changed

Lines changed: 20 additions & 16 deletions

File tree

test/test_periodic_executor.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ async def _default_target():
5959
)
6060

6161

62+
def _run(coro):
63+
return asyncio.run(coro)
64+
65+
6266
# ---------------------------------------------------------------------------
6367
# PeriodicExecutor (sync / threading)
6468
# ---------------------------------------------------------------------------
@@ -309,34 +313,34 @@ def test_skip_sleep(self):
309313

310314
class TestAsyncPeriodicExecutorLifecycle(unittest.TestCase):
311315
def test_open_creates_task(self):
312-
async def run():
316+
async def _test():
313317
ex = _make_async()
314318
ex.open()
315319
self.assertIsNotNone(ex._task)
316320
ex.close()
317321
await ex.join(timeout=1)
318322

319-
asyncio.run(run())
323+
_run(_test())
320324

321325
def test_close_cancels_task(self):
322-
async def run():
326+
async def _test():
323327
ex = _make_async()
324328
ex.open()
325329
ex.close()
326330
await ex.join(timeout=1)
327331
self.assertTrue(ex._stopped)
328332

329-
asyncio.run(run())
333+
_run(_test())
330334

331335
def test_join_without_open_is_safe(self):
332-
async def run():
336+
async def _test():
333337
ex = _make_async()
334338
await ex.join(timeout=0.01) # Should not raise.
335339

336-
asyncio.run(run())
340+
_run(_test())
337341

338342
def test_multiple_open_calls_have_no_effect(self):
339-
async def run():
343+
async def _test():
340344
ex = _make_async()
341345
ex.open()
342346
task_id = id(ex._task)
@@ -345,12 +349,12 @@ async def run():
345349
ex.close()
346350
await ex.join(timeout=1)
347351

348-
asyncio.run(run())
352+
_run(_test())
349353

350354

351355
class TestAsyncPeriodicExecutorTarget(unittest.TestCase):
352356
def test_target_returning_false_stops_executor(self):
353-
async def run():
357+
async def _test():
354358
ran = asyncio.Event()
355359

356360
async def target():
@@ -363,10 +367,10 @@ async def target():
363367
await ex.join(timeout=2)
364368
self.assertTrue(ex._stopped)
365369

366-
asyncio.run(run())
370+
_run(_test())
367371

368372
def test_target_exception_stops_executor(self):
369-
async def run():
373+
async def _test():
370374
ran = asyncio.Event()
371375

372376
async def target():
@@ -379,10 +383,10 @@ async def target():
379383
await ex.join(timeout=2)
380384
self.assertTrue(ex._stopped)
381385

382-
asyncio.run(run())
386+
_run(_test())
383387

384388
def test_skip_sleep_flag_skips_interval(self):
385-
async def run():
389+
async def _test():
386390
call_times = []
387391

388392
async def target():
@@ -398,10 +402,10 @@ async def target():
398402
self.assertGreaterEqual(len(call_times), 2)
399403
self.assertLess(call_times[1] - call_times[0], 5.0)
400404

401-
asyncio.run(run())
405+
_run(_test())
402406

403407
def test_open_after_target_returns_false_creates_new_task(self):
404-
async def run():
408+
async def _test():
405409
call_count = [0]
406410

407411
async def target():
@@ -417,7 +421,7 @@ async def target():
417421
self.assertGreaterEqual(call_count[0], 2)
418422
self.assertIsNot(ex._task, first_task)
419423

420-
asyncio.run(run())
424+
_run(_test())
421425

422426

423427
if __name__ == "__main__":

0 commit comments

Comments
 (0)