Skip to content

Commit

Permalink
test: run_sync tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed May 27, 2020
1 parent c2ea894 commit afd8113
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions nbclient/tests/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import asyncio

import tornado

from nbclient.util import run_sync


@run_sync
async def some_async_function():
await asyncio.sleep(0.01)
return 42


def test_nested_asyncio_with_existing_ioloop():
ioloop = asyncio.new_event_loop()
try:
asyncio.set_event_loop(ioloop)
assert some_async_function() == 42
assert asyncio.get_event_loop() is ioloop
print(ioloop, id(ioloop))
finally:
asyncio._set_running_loop(None) # it seems nest_asyncio doesn't reset this


def test_nested_asyncio_with_no_ioloop():
asyncio.set_event_loop(None)
try:
assert some_async_function() == 42
finally:
asyncio._set_running_loop(None) # it seems nest_asyncio doesn't reset this


def test_nested_asyncio_with_tornado():
# This tests if tornado accepts the pure-Python Futures, see
# https://github.com/tornadoweb/tornado/issues/2753
# https://github.com/erdewit/nest_asyncio/issues/23
asyncio.set_event_loop(asyncio.new_event_loop())
ioloop = tornado.ioloop.IOLoop.current()

async def some_async_function():
future = asyncio.ensure_future(asyncio.sleep(0.1))
# this future is a different future after nested-asyncio has patched
# the asyncio module, check if tornado likes it:
ioloop.add_future(future, lambda f: f.result())
await future
return 42

def some_sync_function():
return run_sync(some_async_function)()

async def run():
# calling some_async_function directly should work
assert await some_async_function() == 42
# but via a sync function (using nested-asyncio) can lead to issues:
# https://github.com/tornadoweb/tornado/issues/2753
assert some_sync_function() == 42

ioloop.run_sync(run)

0 comments on commit afd8113

Please sign in to comment.