Skip to content
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
14 changes: 11 additions & 3 deletions src/openjd/adaptor_runtime/_background/frontend_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@
_FRONTEND_RUNNER_REQUEST_TIMEOUT: float = 5.0

if OSName.is_windows():
from ...adaptor_runtime_client.named_pipe.named_pipe_helper import NamedPipeHelper
from ...adaptor_runtime_client.named_pipe.named_pipe_helper import (
NamedPipeHelper,
NamedPipeTimeoutError,
PipeDisconnectedException,
)
import pywintypes

_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -335,6 +339,7 @@ def _send_request(
# This is used for aligning to the Linux's behavior in order to reuse the code in handler.
# In linux, query string params will always be put in a list.
params = {key: [value] for key, value in params.items()}
start = time.monotonic()
try:
response = NamedPipeHelper.send_named_pipe_request(
self.connection_settings.socket,
Expand All @@ -349,8 +354,11 @@ def _send_request(
errmsg = f"Received unexpected HTTP status code {status}: {response['body']}"
_logger.error(errmsg)
raise HTTPError(response, errmsg)
except pywintypes.error as e:
_logger.error(f"Failed to send {path} request: {e}")
# send_named_pipe_request raises its own types for disconnects and timeouts. The elapsed
# time shows whether the backend closed the pipe at once or after its read timeout.
except (pywintypes.error, PipeDisconnectedException, NamedPipeTimeoutError) as e:
elapsed = time.monotonic() - start
_logger.error(f"Failed to send {path} request after {elapsed:.2f}s: {e}")
raise
return response
else: # pragma: is-windows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -812,17 +812,31 @@ def test_sends_request(
mock_read_from_pipe.assert_called_once()
assert response == json.loads(mock_response)

@pytest.mark.parametrize(
"error_kind", ["pywintypes-error", "pipe-disconnected", "read-timeout"]
)
def test_raises_when_request_fails(
self,
error_kind: str,
mock_read_from_pipe: MagicMock,
mock_response: str,
runner: FrontendRunner,
caplog: pytest.LogCaptureFixture,
):
# GIVEN
import pywintypes
from openjd.adaptor_runtime_client.named_pipe.named_pipe_helper import (
NamedPipeReadTimeoutError,
PipeDisconnectedException,
)

error_instance = pywintypes.error(1, "FunctionName", "An error message")
error_instance = {
"pywintypes-error": pywintypes.error(1, "FunctionName", "An error message"),
"pipe-disconnected": PipeDisconnectedException(
pywintypes.error(109, "ReadFile", "The pipe has been ended.")
),
"read-timeout": NamedPipeReadTimeoutError(5.0),
}[error_kind]
mock_read_from_pipe.side_effect = error_instance
method = "GET"
path = "/path"
Expand All @@ -834,12 +848,15 @@ def test_raises_when_request_fails(
with patch.object(
frontend_runner.NamedPipeHelper, "establish_named_pipe_connection"
) as mock_establish_named_pipe_connection:
with pytest.raises(pywintypes.error) as raised_exc:
with pytest.raises(type(error_instance)) as raised_exc:
runner._send_request(method, path)

# THEN
assert raised_exc.value is error_instance
assert f"Failed to send {path} request: " in caplog.text
assert re.search(
rf"Failed to send {path} request after \d+\.\d\ds: {re.escape(str(error_instance))}",
caplog.text,
)
mock_write_to_pipe.assert_called_once_with(
mock_establish_named_pipe_connection(), '{"method": "GET", "path": "/path"}'
)
Expand Down
Loading