asyncio: Ensure call_soon(), call_later() and call_at() are invoked on current

loop in debug mode. Raise a RuntimeError if the event loop of the current
thread is different.  The check should help to debug thread-safetly issue.
Patch written by David Foster.
This commit is contained in:
Victor Stinner 2014-03-21 10:00:52 +01:00
parent 4137465bf5
commit 93569c2b3d
2 changed files with 45 additions and 1 deletions

View file

@ -136,6 +136,29 @@ class BaseEventLoopTests(unittest.TestCase):
# are really slow
self.assertLessEqual(dt, 0.9, dt)
def test_assert_is_current_event_loop(self):
def cb():
pass
other_loop = base_events.BaseEventLoop()
other_loop._selector = unittest.mock.Mock()
asyncio.set_event_loop(other_loop)
# raise RuntimeError if the event loop is different in debug mode
self.loop.set_debug(True)
with self.assertRaises(RuntimeError):
self.loop.call_soon(cb)
with self.assertRaises(RuntimeError):
self.loop.call_later(60, cb)
with self.assertRaises(RuntimeError):
self.loop.call_at(self.loop.time() + 60, cb)
# check disabled if debug mode is disabled
self.loop.set_debug(False)
self.loop.call_soon(cb)
self.loop.call_later(60, cb)
self.loop.call_at(self.loop.time() + 60, cb)
def test_run_once_in_executor_handle(self):
def cb():
pass