Skip to content

Commit

Permalink
Merge pull request #3284 from bdarnell/asyncio-test
Browse files Browse the repository at this point in the history
asyncio_test: Use inequality when looking for thread leaks
  • Loading branch information
bdarnell committed Jun 19, 2023
2 parents 5a488b4 + 2405a21 commit 6f2d093
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions tornado/test/asyncio_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,8 @@ async def add_callback():
@gen_test
def test_asyncio_future(self):
# Test that we can yield an asyncio future from a tornado coroutine.
# Without 'yield from', we must wrap coroutines in ensure_future,
# which was introduced during Python 3.4, deprecating the prior "async".
if hasattr(asyncio, "ensure_future"):
ensure_future = asyncio.ensure_future
else:
# async is a reserved word in Python 3.7
ensure_future = getattr(asyncio, "async")

x = yield ensure_future(
# Without 'yield from', we must wrap coroutines in ensure_future.
x = yield asyncio.ensure_future(
asyncio.get_event_loop().run_in_executor(None, lambda: 42)
)
self.assertEqual(x, 42)
Expand Down Expand Up @@ -171,13 +164,15 @@ def assert_no_thread_leak(self):
# For some reason we see transient failures here, but I haven't been able
# to catch it to identify which thread is causing it. Whatever thread it
# is, it appears to quickly clean up on its own, so just retry a few times.
# At least some of the time the errant thread was running at the time we
# captured self.orig_thread_count, so use inequalities.
deadline = time.time() + 1
while time.time() < deadline:
threads = list(threading.enumerate())
if len(threads) == self.orig_thread_count:
if len(threads) <= self.orig_thread_count:
break
time.sleep(0.1)
self.assertEqual(self.orig_thread_count, len(threads), threads)
self.assertLessEqual(len(threads), self.orig_thread_count, threads)

async def dummy_tornado_coroutine(self):
# Just access the IOLoop to initialize the selector thread.
Expand Down

0 comments on commit 6f2d093

Please sign in to comment.