Skip to content
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

Isoliere State Dir für run-Tests #125

Merged
merged 2 commits into from
Oct 7, 2024
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
13 changes: 9 additions & 4 deletions questionpy_sdk/commands/run.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
# This file is part of the QuestionPy SDK. (https://questionpy.org)
# The QuestionPy SDK is free software released under terms of the MIT license. See LICENSE.md.
# (c) Technische Universität Berlin, innoCampus <info@isis.tu-berlin.de>

from pathlib import Path

import click

from questionpy_sdk.commands._helper import get_package_location
from questionpy_sdk.webserver.app import WebServer
from questionpy_sdk.webserver.app import DEFAULT_STATE_STORAGE_PATH, WebServer


@click.command()
@click.argument("package")
def run(package: str) -> None:
@click.option(
"--state-storage-path",
type=click.Path(path_type=Path, exists=False, file_okay=False, dir_okay=True, resolve_path=True),
default=DEFAULT_STATE_STORAGE_PATH,
envvar="QPY_STATE_STORAGE_PATH",
)
def run(package: str, state_storage_path: Path) -> None:
"""Run a package.

\b
Expand All @@ -22,5 +27,5 @@ def run(package: str) -> None:
- a source directory (built on-the-fly).
""" # noqa: D301
pkg_path = Path(package).resolve()
web_server = WebServer(get_package_location(package, pkg_path))
web_server = WebServer(get_package_location(package, pkg_path), state_storage_path)
web_server.start_server()
5 changes: 4 additions & 1 deletion questionpy_sdk/webserver/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ class StateFilename(StrEnum):
LAST_ATTEMPT_DATA = "last_attempt_data.json"


DEFAULT_STATE_STORAGE_PATH = Path(__file__).parent / "question_state_storage"


class WebServer:
def __init__(
self,
package_location: PackageLocation,
state_storage_path: Path = Path(__file__).parent / "question_state_storage",
state_storage_path: Path,
) -> None:
# We import here, so we don't have to work around circular imports.
from questionpy_sdk.webserver.routes.attempt import routes as attempt_routes # noqa: PLC0415
Expand Down
32 changes: 18 additions & 14 deletions tests/questionpy_sdk/commands/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import signal
import sys
import tempfile
from asyncio.subprocess import PIPE, Process
from collections.abc import AsyncIterator, Iterable, Iterator
from pathlib import Path
Expand Down Expand Up @@ -71,23 +72,26 @@ async def client_session() -> AsyncIterator[aiohttp.ClientSession]:
# can't test long-running processes with `CliRunner` (https://github.com/pallets/click/issues/2171)
@contextlib.asynccontextmanager
async def long_running_cmd(args: Iterable[str], timeout: float = 5) -> AsyncIterator[Process]:
try:
popen_args = [sys.executable, "-m", "questionpy_sdk", "--", *args]
proc = await asyncio.create_subprocess_exec(*popen_args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
with tempfile.TemporaryDirectory("qpy-state-storage") as state_dir:
try:
popen_args = [sys.executable, "-m", "questionpy_sdk", "--", *args]
proc = await asyncio.create_subprocess_exec(
*popen_args, stdin=PIPE, stdout=PIPE, stderr=PIPE, env={"QPY_STATE_STORAGE_PATH": state_dir}
)

# ensure tests don't hang indefinitely
async def kill_after_timeout() -> None:
await asyncio.sleep(timeout)
proc.send_signal(signal.SIGINT)
# ensure tests don't hang indefinitely
async def kill_after_timeout() -> None:
await asyncio.sleep(timeout)
proc.send_signal(signal.SIGINT)

kill_task = asyncio.create_task(kill_after_timeout())
yield proc
kill_task = asyncio.create_task(kill_after_timeout())
yield proc

finally:
if kill_task:
kill_task.cancel()
proc.send_signal(signal.SIGINT)
await proc.wait()
finally:
if kill_task:
kill_task.cancel()
proc.send_signal(signal.SIGINT)
await proc.wait()


async def assert_webserver_is_up(session: aiohttp.ClientSession, url: str = "http://localhost:8080/") -> None:
Expand Down