Skip to content

Commit

Permalink
Add test_stdout.py
Browse files Browse the repository at this point in the history
  • Loading branch information
TaiSakuma committed Jun 25, 2024
1 parent 889de53 commit c6102df
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Empty file.
41 changes: 41 additions & 0 deletions tests/plugins/ctrl/schema/subscriptions/test_stdout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import asyncio
from typing import Any

from nextline import Nextline

from nextlinegraphql.plugins.ctrl.cache import CacheStdout
from nextlinegraphql.plugins.ctrl.graphql import SUBSCRIBE_STDOUT
from tests.plugins.ctrl.schema.conftest import Schema

SOURCE = '''
for i in range(10):
print(i)
'''.strip()


async def test_schema(schema: Schema) -> None:
nextline = Nextline(SOURCE, trace_modules=True, trace_threads=True)
cache = list[str]()
nextline.register(CacheStdout(cache))
started = asyncio.Event()
context = {'nextline': nextline, 'stdout_cache': cache}
async with nextline:
task = asyncio.create_task(nextline.run_continue_and_wait(started=started))
await started.wait()
sub = asyncio.create_task(_subscribe_stdout(schema, context=context))
await task
await nextline.reset()
await asyncio.create_task(nextline.run_continue_and_wait(started=started))

stdout = await sub
assert stdout == '0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n' '0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n'


async def _subscribe_stdout(schema: Schema, context: Any) -> str:
sub = await schema.subscribe(SUBSCRIBE_STDOUT, context_value=context)
assert hasattr(sub, '__aiter__')
ret = ''
async for result in sub:
assert (data := result.data)
ret += data['ctrlStdout']
return ret

0 comments on commit c6102df

Please sign in to comment.