|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | + |
| 3 | +""" |
| 4 | +Test to reproduce a bug where progress reporting via update_status() does not |
| 5 | +propagate from the background adaptor to the frontend process's stdout. |
| 6 | +
|
| 7 | +The flow is: |
| 8 | + 1. Application outputs progress info |
| 9 | + 2. Background adaptor (backend process) calls update_status(progress=X) |
| 10 | + 3. update_status writes "openjd_progress: X" to sys.stdout |
| 11 | + 4. Frontend process should emit "openjd_progress: X" on its own stdout |
| 12 | + so the worker agent can detect it |
| 13 | +
|
| 14 | +The bug: In daemon/background mode, the backend process replaces its stream |
| 15 | +handler with a LogBufferHandler. But update_status() writes directly to |
| 16 | +sys.stdout (not through the logging framework), so the progress message |
| 17 | +never reaches the log buffer, and thus never reaches the frontend. |
| 18 | +""" |
| 19 | + |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +import json |
| 23 | +import os |
| 24 | +import pathlib |
| 25 | +import sys |
| 26 | +from logging import INFO |
| 27 | +from unittest.mock import patch |
| 28 | +from pathlib import Path |
| 29 | + |
| 30 | +import pytest |
| 31 | + |
| 32 | +import openjd.adaptor_runtime._entrypoint as runtime_entrypoint |
| 33 | +from openjd.adaptor_runtime import EntryPoint |
| 34 | + |
| 35 | +mod_path = Path(__file__).parent.resolve() |
| 36 | +sys.path.append(str(mod_path)) |
| 37 | + |
| 38 | +if (_pypath := os.environ.get("PYTHONPATH")) is not None: |
| 39 | + os.environ["PYTHONPATH"] = os.pathsep.join([_pypath, str(mod_path)]) |
| 40 | +else: |
| 41 | + os.environ["PYTHONPATH"] = str(mod_path) |
| 42 | + |
| 43 | +from ProgressReportingAdaptor import ProgressReportingAdaptor # noqa: E402 |
| 44 | + |
| 45 | + |
| 46 | +class TestProgressReportingInDaemonMode: |
| 47 | + """ |
| 48 | + Tests that progress messages from update_status() in the background adaptor |
| 49 | + are propagated to the frontend process's stdout so the worker agent can |
| 50 | + detect them. |
| 51 | + """ |
| 52 | + |
| 53 | + def test_progress_propagates_to_frontend_stdout( |
| 54 | + self, |
| 55 | + capfd: pytest.CaptureFixture, |
| 56 | + caplog: pytest.LogCaptureFixture, |
| 57 | + tmp_path: pathlib.Path, |
| 58 | + ): |
| 59 | + """ |
| 60 | + Reproduces the bug: when running in daemon mode, calling |
| 61 | + update_status(progress=50) in the backend adaptor should result in |
| 62 | + "openjd_progress: 50" appearing on the frontend's stdout. |
| 63 | +
|
| 64 | + The worker agent monitors the frontend process's stdout for these |
| 65 | + messages. If they don't appear, the worker never knows about progress. |
| 66 | + """ |
| 67 | + caplog.set_level(INFO) |
| 68 | + connection_file = tmp_path / "connection.json" |
| 69 | + entrypoint = EntryPoint(ProgressReportingAdaptor) |
| 70 | + |
| 71 | + # Start the daemon |
| 72 | + start_argv = [ |
| 73 | + "program_filename.py", |
| 74 | + "daemon", |
| 75 | + "start", |
| 76 | + "--connection-file", |
| 77 | + str(connection_file), |
| 78 | + ] |
| 79 | + with ( |
| 80 | + patch.object(runtime_entrypoint.sys, "argv", start_argv), |
| 81 | + patch.object(runtime_entrypoint.logging.Logger, "setLevel"), |
| 82 | + ): |
| 83 | + entrypoint.start() |
| 84 | + |
| 85 | + # Run with progress=50 |
| 86 | + run_argv = [ |
| 87 | + "program_filename.py", |
| 88 | + "daemon", |
| 89 | + "run", |
| 90 | + "--connection-file", |
| 91 | + str(connection_file), |
| 92 | + "--run-data", |
| 93 | + json.dumps({"progress": 50.0}), |
| 94 | + ] |
| 95 | + with ( |
| 96 | + patch.object(runtime_entrypoint.sys, "argv", run_argv), |
| 97 | + patch.object(runtime_entrypoint.logging.Logger, "setLevel"), |
| 98 | + ): |
| 99 | + entrypoint.start() |
| 100 | + |
| 101 | + # Stop the daemon |
| 102 | + stop_argv = [ |
| 103 | + "program_filename.py", |
| 104 | + "daemon", |
| 105 | + "stop", |
| 106 | + "--connection-file", |
| 107 | + str(connection_file), |
| 108 | + ] |
| 109 | + with ( |
| 110 | + patch.object(runtime_entrypoint.sys, "argv", stop_argv), |
| 111 | + patch.object(runtime_entrypoint.logging.Logger, "setLevel"), |
| 112 | + ): |
| 113 | + entrypoint.start() |
| 114 | + |
| 115 | + # Verify that "openjd_progress: 50" appeared on stdout. |
| 116 | + # The worker agent is watching stdout for this pattern. |
| 117 | + captured = capfd.readouterr() |
| 118 | + assert "openjd_progress: 50" in captured.out, ( |
| 119 | + f"Expected 'openjd_progress: 50' on stdout but got:\n{captured.out}\n\n" |
| 120 | + f"Log output:\n{caplog.text}" |
| 121 | + ) |
| 122 | + |
| 123 | + def test_progress_works_in_run_mode( |
| 124 | + self, |
| 125 | + capfd: pytest.CaptureFixture, |
| 126 | + caplog: pytest.LogCaptureFixture, |
| 127 | + ): |
| 128 | + """ |
| 129 | + Sanity check: progress reporting works fine in non-daemon 'run' mode |
| 130 | + because update_status writes directly to sys.stdout and the worker |
| 131 | + agent is directly reading from that process's stdout. |
| 132 | + """ |
| 133 | + caplog.set_level(INFO) |
| 134 | + entrypoint = EntryPoint(ProgressReportingAdaptor) |
| 135 | + |
| 136 | + run_argv = [ |
| 137 | + "program_filename.py", |
| 138 | + "run", |
| 139 | + "--run-data", |
| 140 | + json.dumps({"progress": 75.0}), |
| 141 | + ] |
| 142 | + with ( |
| 143 | + patch.object(runtime_entrypoint.sys, "argv", run_argv), |
| 144 | + patch.object(runtime_entrypoint.logging.Logger, "setLevel"), |
| 145 | + ): |
| 146 | + entrypoint.start() |
| 147 | + |
| 148 | + captured = capfd.readouterr() |
| 149 | + assert ( |
| 150 | + "openjd_progress: 75" in captured.out |
| 151 | + ), f"Expected 'openjd_progress: 75' on stdout but got:\n{captured.out}" |
0 commit comments