Skip to content

Commit

Permalink
Wait until quart app is ready before testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
matham committed Aug 10, 2020
1 parent ede4bb8 commit e7f978d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
8 changes: 7 additions & 1 deletion pymoa_remote/app/quart.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,14 @@ def create_app(
return app


async def start_app(app, host='127.0.0.1', port=5000):
async def start_app(
app, host='127.0.0.1', port=5000,
task_status=trio.TASK_STATUS_IGNORED):
async def before_first():
task_status.started()
# start/stop thread executor
app.before_serving(before_first)

executor = app.rest_executor.executor
async with executor:
with ExecutorContext(executor):
Expand Down
23 changes: 18 additions & 5 deletions pymoa_remote/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import pytest
import trio
from trio import socket


async def get_socket_port(server):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
await s.bind((server, 0))
s.listen(1)
port = s.getsockname()[1]
s.close()
return port


@pytest.fixture
async def quart_app(nursery):
from pymoa_remote.app.quart import create_app, start_app
app = create_app()
nursery.start_soon(start_app, app)
await trio.sleep(.01)
port = await get_socket_port('localhost')
app.pymoa_port = port

async with app.app_context():
await nursery.start(start_app, app, 'localhost', port)
yield app


@pytest.fixture
async def quart_rest_executor(quart_app):
from pymoa_remote.rest.client import RestExecutor
from pymoa_remote.client import ExecutorContext
async with RestExecutor(uri='http://127.0.0.1:5000') as executor:
async with RestExecutor(
uri=f'http://localhost:{quart_app.pymoa_port}') as executor:
with ExecutorContext(executor):
yield executor

Expand All @@ -27,7 +39,8 @@ async def quart_socket_executor(quart_app, nursery):
from pymoa_remote.socket.websocket_client import WebSocketExecutor
from pymoa_remote.client import ExecutorContext
async with WebSocketExecutor(
nursery=nursery, server='127.0.0.1', port=5000) as executor:
nursery=nursery, server='localhost',
port=quart_app.pymoa_port) as executor:
with ExecutorContext(executor):
yield executor

Expand All @@ -47,7 +60,7 @@ async def process_executor():
MultiprocessSocketExecutor
from pymoa_remote.client import ExecutorContext
async with MultiprocessSocketExecutor(
server='127.0.0.1', allow_import_from_main=True) as executor:
server='localhost', allow_import_from_main=True) as executor:
with ExecutorContext(executor):
yield executor

Expand Down

0 comments on commit e7f978d

Please sign in to comment.