-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2ea894
commit afd8113
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |