asyncio: Fix sporadic failing unittests in debug mode · python/cpython@abe9625 (original) (raw)
`@@ -2101,31 +2101,37 @@ def outer():
`
2101
2101
``
2102
2102
``
2103
2103
`class RunCoroutineThreadsafeTests(test_utils.TestCase):
`
2104
``
`-
"""Test case for futures.submit_to_loop."""
`
``
2104
`+
"""Test case for asyncio.run_coroutine_threadsafe."""
`
2105
2105
``
2106
2106
`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
`
2113
2109
``
2114
2110
`@asyncio.coroutine
`
2115
2111
`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)
`
2118
2114
`if fail:
`
2119
2115
`raise RuntimeError("Fail!")
`
2120
2116
`if cancel:
`
2121
2117
`asyncio.tasks.Task.current_task(self.loop).cancel()
`
2122
2118
`yield
`
2123
2119
`return a + b
`
2124
2120
``
2125
``
`-
def target(self, fail=False, cancel=False, timeout=None):
`
``
2121
`+
def target(self, fail=False, cancel=False, timeout=None,
`
``
2122
`+
advance_coro=False):
`
2126
2123
`"""Run add coroutine in the event loop."""
`
2127
2124
`coro = self.add(1, 2, fail=fail, cancel=cancel)
`
2128
2125
`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)
`
2129
2135
`try:
`
2130
2136
`return future.result(timeout)
`
2131
2137
`finally:
`
`@@ -2152,7 +2158,6 @@ def test_run_coroutine_threadsafe_with_timeout(self):
`
2152
2158
`future = self.loop.run_in_executor(None, callback)
`
2153
2159
`with self.assertRaises(asyncio.TimeoutError):
`
2154
2160
`self.loop.run_until_complete(future)
`
2155
``
`-
Clear the time generator and tasks
`
2156
2161
`test_utils.run_briefly(self.loop)
`
2157
2162
`# Check that there's no pending task (add has been cancelled)
`
2158
2163
`for task in asyncio.Task.all_tasks(self.loop):
`
`@@ -2169,10 +2174,9 @@ def test_run_coroutine_threadsafe_task_cancelled(self):
`
2169
2174
`def test_run_coroutine_threadsafe_task_factory_exception(self):
`
2170
2175
`"""Test coroutine submission from a tread to an event loop
`
2171
2176
` when the task factory raise an exception."""
`
2172
``
`-
Clear the time generator
`
2173
``
`-
asyncio.ensure_future(self.add(1, 2), loop=self.loop)
`
2174
2177
`# 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))
`
2176
2180
`# Set corrupted task factory
`
2177
2181
`self.loop.set_task_factory(lambda loop, coro: wrong_name)
`
2178
2182
`# Set exception handler
`