Skip to content

Commit abe9625

Browse files
committed
asyncio: Fix sporadic failing unittests in debug mode
1 parent 9cccfce commit abe9625

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

‎Lib/test/test_asyncio/test_tasks.py‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,31 +2101,37 @@ def outer():
21012101

21022102

21032103
class RunCoroutineThreadsafeTests(test_utils.TestCase):
2104-
"""Test case for futures.submit_to_loop."""
2104+
"""Test case for asyncio.run_coroutine_threadsafe."""
21052105

21062106
def setUp(self):
2107-
self.loop = self.new_test_loop(self.time_gen)
2108-
2109-
def time_gen(self):
2110-
"""Handle the timer."""
2111-
yield 0 # second
2112-
yield 1 # second
2107+
self.loop = asyncio.new_event_loop()
2108+
self.set_event_loop(self.loop) # Will cleanup properly
21132109

21142110
@asyncio.coroutine
21152111
def add(self, a, b, fail=False, cancel=False):
2116-
"""Wait 1 second and return a + b."""
2117-
yield from asyncio.sleep(1, loop=self.loop)
2112+
"""Wait 0.05 second and return a + b."""
2113+
yield from asyncio.sleep(0.05, loop=self.loop)
21182114
if fail:
21192115
raise RuntimeError("Fail!")
21202116
if cancel:
21212117
asyncio.tasks.Task.current_task(self.loop).cancel()
21222118
yield
21232119
return a + b
21242120

2125-
def target(self, fail=False, cancel=False, timeout=None):
2121+
def target(self, fail=False, cancel=False, timeout=None,
2122+
advance_coro=False):
21262123
"""Run add coroutine in the event loop."""
21272124
coro = self.add(1, 2, fail=fail, cancel=cancel)
21282125
future = asyncio.run_coroutine_threadsafe(coro, self.loop)
2126+
if advance_coro:
2127+
# this is for test_run_coroutine_threadsafe_task_factory_exception;
2128+
# otherwise it spills errors and breaks **other** unittests, since
2129+
# 'target' is interacting with threads.
2130+
2131+
# With this call, `coro` will be advanced, so that
2132+
# CoroWrapper.__del__ won't do anything when asyncio tests run
2133+
# in debug mode.
2134+
self.loop.call_soon_threadsafe(coro.send, None)
21292135
try:
21302136
return future.result(timeout)
21312137
finally:
@@ -2152,7 +2158,6 @@ def test_run_coroutine_threadsafe_with_timeout(self):
21522158
future = self.loop.run_in_executor(None, callback)
21532159
with self.assertRaises(asyncio.TimeoutError):
21542160
self.loop.run_until_complete(future)
2155-
# Clear the time generator and tasks
21562161
test_utils.run_briefly(self.loop)
21572162
# Check that there's no pending task (add has been cancelled)
21582163
for task in asyncio.Task.all_tasks(self.loop):
@@ -2169,10 +2174,9 @@ def test_run_coroutine_threadsafe_task_cancelled(self):
21692174
def test_run_coroutine_threadsafe_task_factory_exception(self):
21702175
"""Test coroutine submission from a tread to an event loop
21712176
when the task factory raise an exception."""
2172-
# Clear the time generator
2173-
asyncio.ensure_future(self.add(1, 2), loop=self.loop)
21742177
# Schedule the target
2175-
future = self.loop.run_in_executor(None, self.target)
2178+
future = self.loop.run_in_executor(
2179+
None, lambda: self.target(advance_coro=True))
21762180
# Set corrupted task factory
21772181
self.loop.set_task_factory(lambda loop, coro: wrong_name)
21782182
# Set exception handler

0 commit comments

Comments
 (0)