-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: monkeypatch instead of leaking test specifics into production code
- Loading branch information
Showing
8 changed files
with
96 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,31 +2,6 @@ | |
# The QuestionPy Server is free software released under terms of the MIT license. See LICENSE.md. | ||
# (c) Technische Universität Berlin, innoCampus <[email protected]> | ||
|
||
import sys | ||
from io import BufferedReader, FileIO, StringIO | ||
from questionpy_server.worker.runtime.subprocess import main | ||
|
||
from questionpy_server.worker.runtime.connection import WorkerToServerConnection | ||
from questionpy_server.worker.runtime.manager import WorkerManager | ||
|
||
|
||
def setup_server_communication() -> WorkerToServerConnection: | ||
"""Setup stdin/stdout/stderr. | ||
The application server communicates with its worker through stdin/stdout, which means only this class is allowed to | ||
read from and write to these pipes. Other output should go through stderr. | ||
""" | ||
file_stdin = FileIO(sys.stdin.buffer.fileno(), "r", closefd=False) | ||
file_stdout = FileIO(sys.stdout.fileno(), "w", closefd=False) | ||
connection = WorkerToServerConnection(BufferedReader(file_stdin), file_stdout) | ||
|
||
sys.stdin = StringIO() | ||
sys.stdout = sys.stderr # All writes to sys.stdout should go to stderr. | ||
return connection | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.dont_write_bytecode = True | ||
con = setup_server_communication() | ||
manager = WorkerManager(con) | ||
manager.bootstrap() | ||
manager.loop() | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# This file is part of the QuestionPy Server. (https://questionpy.org) | ||
# The QuestionPy Server is free software released under terms of the MIT license. See LICENSE.md. | ||
# (c) Technische Universität Berlin, innoCampus <[email protected]> | ||
|
||
import sys | ||
from io import BufferedReader, FileIO, StringIO | ||
|
||
from questionpy_server.worker.runtime.connection import WorkerToServerConnection | ||
from questionpy_server.worker.runtime.manager import WorkerManager | ||
|
||
|
||
def setup_server_communication() -> WorkerToServerConnection: | ||
"""Setup stdin/stdout/stderr. | ||
The application server communicates with its worker through stdin/stdout, which means only this class is allowed to | ||
read from and write to these pipes. Other output should go through stderr. | ||
""" | ||
file_stdin = FileIO(sys.stdin.buffer.fileno(), "r", closefd=False) | ||
file_stdout = FileIO(sys.stdout.fileno(), "w", closefd=False) | ||
connection = WorkerToServerConnection(BufferedReader(file_stdin), file_stdout) | ||
|
||
sys.stdin = StringIO() | ||
sys.stdout = sys.stderr # All writes to sys.stdout should go to stderr. | ||
return connection | ||
|
||
|
||
def main() -> None: | ||
sys.dont_write_bytecode = True | ||
con = setup_server_communication() | ||
manager = WorkerManager(con) | ||
manager.bootstrap() | ||
manager.loop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
tests/questionpy_server/worker/worker/_patched_subprocess_worker.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# This file is part of the QuestionPy Server. (https://questionpy.org) | ||
# The QuestionPy Server is free software released under terms of the MIT license. See LICENSE.md. | ||
# (c) Technische Universität Berlin, innoCampus <[email protected]> | ||
|
||
from questionpy_server.worker.runtime.subprocess import main | ||
from tests.questionpy_server.worker.worker.conftest import patched_manager | ||
|
||
if __name__ == "__main__": | ||
with patched_manager(): | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# This file is part of the QuestionPy Server. (https://questionpy.org) | ||
# The QuestionPy Server is free software released under terms of the MIT license. See LICENSE.md. | ||
# (c) Technische Universität Berlin, innoCampus <[email protected]> | ||
|
||
from collections.abc import Iterator | ||
from contextlib import contextmanager | ||
from typing import Any | ||
|
||
import pytest | ||
|
||
from questionpy_server.worker.pool import WorkerPool | ||
from questionpy_server.worker.runtime.manager import WorkerManager | ||
from questionpy_server.worker.worker.subprocess import SubprocessWorker | ||
from questionpy_server.worker.worker.thread import ThreadWorker | ||
|
||
|
||
@contextmanager | ||
def patched_manager() -> Iterator[None]: | ||
with pytest.MonkeyPatch.context() as mp: | ||
|
||
def just_raise(*_: list[Any]) -> None: | ||
msg = "some custom error" | ||
raise RuntimeError(msg) | ||
|
||
mp.setattr(WorkerManager, "on_msg_get_qpy_package_manifest", just_raise) | ||
yield | ||
|
||
|
||
@pytest.fixture | ||
def patched_worker_pool(worker_pool: WorkerPool, monkeypatch: pytest.MonkeyPatch) -> Iterator[WorkerPool]: | ||
if worker_pool._worker_type == ThreadWorker: | ||
with patched_manager(): | ||
yield worker_pool | ||
|
||
# Can't patch stuff inside subprocess, so we use an entrypoint wrapper. | ||
elif worker_pool._worker_type == SubprocessWorker: | ||
with monkeypatch.context() as mp: | ||
patched_entrypoint = "tests.questionpy_server.worker.worker._patched_subprocess_worker" | ||
mp.setattr(SubprocessWorker, "_worker_entrypoint", patched_entrypoint) | ||
yield worker_pool | ||
|
||
else: | ||
msg = "Expected ThreadWorker or SubprocessWorker" | ||
raise TypeError(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters