Skip to content

Run test webserver within main async loop #1266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/reactpy/testing/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import asyncio
import logging
from contextlib import AsyncExitStack
from threading import Thread
from types import TracebackType
from typing import Any, Callable
from urllib.parse import urlencode, urlunparse
Expand All @@ -16,7 +15,6 @@
from reactpy.config import REACTPY_TESTS_DEFAULT_TIMEOUT
from reactpy.core.component import component
from reactpy.core.hooks import use_callback, use_effect, use_state
from reactpy.testing.common import GITHUB_ACTIONS
from reactpy.testing.logs import (
LogAssertionError,
capture_reactpy_logs,
Expand Down Expand Up @@ -121,7 +119,8 @@ async def __aenter__(self) -> BackendFixture:
self.log_records = self._exit_stack.enter_context(capture_reactpy_logs())

# Wait for the server to start
Thread(target=self.webserver.run, daemon=True).start()
self.webserver.config.setup_event_loop()
self.webserver_task = asyncio.create_task(self.webserver.serve())
await asyncio.sleep(1)

return self
Expand All @@ -139,9 +138,8 @@ async def __aexit__(
msg = "Unexpected logged exception"
raise LogAssertionError(msg) from logged_errors[0]

await asyncio.wait_for(
self.webserver.shutdown(), timeout=90 if GITHUB_ACTIONS else 5
)
await self.webserver.shutdown()
self.webserver_task.cancel()

async def restart(self) -> None:
"""Restart the server"""
Expand Down
5 changes: 3 additions & 2 deletions tests/test_asgi/test_standalone.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
from collections.abc import MutableMapping

import pytest
Expand Down Expand Up @@ -155,8 +156,8 @@ def sample():
async with DisplayFixture(backend=server, driver=page) as new_display:
await new_display.show(sample)
url = f"http://{server.host}:{server.port}"
response = request(
"HEAD", url, timeout=REACTPY_TESTS_DEFAULT_TIMEOUT.current
response = await asyncio.to_thread(
request, "HEAD", url, timeout=REACTPY_TESTS_DEFAULT_TIMEOUT.current
)
assert response.status_code == 200
assert response.headers["content-type"] == "text/html; charset=utf-8"
Expand Down