Skip to content

Commit

Permalink
Add a basic test of guest mode
Browse files Browse the repository at this point in the history
  • Loading branch information
njsmith committed May 28, 2020
1 parent 5631de4 commit ca593ad
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions trio/_core/tests/test_guest_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pytest
import asyncio

import trio

def test_guest_mode_basic():
async def aio_main():
loop = asyncio.get_running_loop()

trio_done_fut = asyncio.Future()
def trio_done_callback(main_outcome):
print(f"trio_main finished: {main_outcome!r}")
trio_done_fut.set_result(main_outcome)

trio.lowlevel.start_guest_run(
trio_main,
run_sync_soon_threadsafe=loop.call_soon_threadsafe,
done_callback=trio_done_callback,
)

return (await trio_done_fut).unwrap()

async def trio_main():
print("trio_main!")

to_trio, from_aio = trio.open_memory_channel(float("inf"))
from_trio = asyncio.Queue()

aio_task = asyncio.create_task(aio_pingpong(from_trio, to_trio))

from_trio.put_nowait(0)

async for n in from_aio:
print(f"trio got: {n}")
from_trio.put_nowait(n + 1)
if n >= 10:
aio_task.cancel()
return "trio-main-done"

async def aio_pingpong(from_trio, to_trio):
print("aio_pingpong!")

while True:
n = await from_trio.get()
print(f"aio got: {n}")
to_trio.send_nowait(n + 1)

loop = asyncio.new_event_loop()
assert loop.run_until_complete(aio_main()) == "trio-main-done"
loop.close()

0 comments on commit ca593ad

Please sign in to comment.