From 11eba6f51314a9d644ab3ef0d16dc060a031ab8b Mon Sep 17 00:00:00 2001 From: Azizul Haque Ananto Date: Sat, 26 Aug 2023 21:55:09 +0200 Subject: [PATCH] Fix http request handle issue --- .github/workflows/codecov.yml | 2 +- .../single_server/client_generation_test.py | 3 ++ .../single_server/client_server_test.py | 28 +++++++++++++++++++ tests/functional/single_server/server.py | 5 ++++ tests/requirements.txt | 3 +- zero/client_server/worker.py | 10 +++---- zero/error.py | 3 ++ 7 files changed, 47 insertions(+), 7 deletions(-) diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 7f9dc3f..e7a67da 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -22,7 +22,7 @@ jobs: with: python-version: ${{ matrix.python-version }} - name: Generate Report - timeout-minutes: 10 + timeout-minutes: 20 run: | pip install -r tests/requirements.txt make test diff --git a/tests/functional/single_server/client_generation_test.py b/tests/functional/single_server/client_generation_test.py index fbfbd6b..22d3fc7 100644 --- a/tests/functional/single_server/client_generation_test.py +++ b/tests/functional/single_server/client_generation_test.py @@ -41,6 +41,9 @@ def error(self, msg: str) -> str: def msgspec_struct(self, start: datetime.datetime) -> Message: return self._zero_client.call("msgspec_struct", start) + def send_bytes(self, msg: bytes) -> bytes: + return self._zero_client.call("send_bytes", msg) + def echo(self, msg: str) -> str: return self._zero_client.call("echo", msg) diff --git a/tests/functional/single_server/client_server_test.py b/tests/functional/single_server/client_server_test.py index 444c585..1dbf8de 100644 --- a/tests/functional/single_server/client_server_test.py +++ b/tests/functional/single_server/client_server_test.py @@ -1,6 +1,7 @@ import datetime import pytest +import requests import zero.error from zero import AsyncZeroClient, ZeroClient @@ -100,3 +101,30 @@ def test_msgspec_struct(): msg = zero_client.call("msgspec_struct", now, return_type=Message) assert msg.msg == "hello world" assert msg.start_time == now + + +def test_send_bytes(): + zero_client = ZeroClient(server.HOST, server.PORT) + msg = zero_client.call("send_bytes", b"hello") + assert msg == b"hello" + + +def test_send_http_request(): + with pytest.raises(requests.exceptions.ReadTimeout): + requests.get(f"http://{server.HOST}:{server.PORT}", timeout=2) + + +def test_server_works_after_multiple_http_requests(): + """Because of this issue https://github.com/Ananto30/zero/issues/41""" + try: + requests.get(f"http://{server.HOST}:{server.PORT}", timeout=2) + requests.get(f"http://{server.HOST}:{server.PORT}", timeout=2) + requests.get(f"http://{server.HOST}:{server.PORT}", timeout=2) + requests.get(f"http://{server.HOST}:{server.PORT}", timeout=2) + requests.get(f"http://{server.HOST}:{server.PORT}", timeout=2) + requests.get(f"http://{server.HOST}:{server.PORT}", timeout=2) + except requests.exceptions.ReadTimeout: + pass + zero_client = ZeroClient(server.HOST, server.PORT) + msg = zero_client.call("hello_world", "") + assert msg == "hello world" diff --git a/tests/functional/single_server/server.py b/tests/functional/single_server/server.py index 7b97f2f..7d4d278 100644 --- a/tests/functional/single_server/server.py +++ b/tests/functional/single_server/server.py @@ -79,6 +79,11 @@ def msgspec_struct(start: datetime.datetime) -> Message: return Message(msg="hello world", start_time=start) +@app.register_rpc +def send_bytes(msg: bytes) -> bytes: + return msg + + def run(port): print("Starting server on port", port) app.register_rpc(echo) diff --git a/tests/requirements.txt b/tests/requirements.txt index 412e089..eeed49c 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -4,4 +4,5 @@ pytest pytest-cov PyJWT pytest-asyncio -tornado>=6.1 \ No newline at end of file +tornado>=6.1 +requests \ No newline at end of file diff --git a/zero/client_server/worker.py b/zero/client_server/worker.py index ca559a7..217e0d7 100644 --- a/zero/client_server/worker.py +++ b/zero/client_server/worker.py @@ -7,6 +7,7 @@ from zero import config from zero.codegen.codegen import CodeGen from zero.encoder.protocols import Encoder +from zero.error import SERVER_PROCESSING_ERROR from zero.zero_mq.factory import get_worker @@ -42,16 +43,15 @@ def process_message(data: bytes) -> Optional[bytes]: return self._encoder.encode([req_id, response]) except Exception as inner_exc: # pylint: disable=broad-except logging.exception(inner_exc) - # TODO what to return - return None + return self._encoder.encode( + ["", {"__zerror__server_exception": SERVER_PROCESSING_ERROR}] + ) worker = get_worker(config.ZEROMQ_PATTERN, worker_id) try: worker.listen(self._device_comm_channel, process_message) except KeyboardInterrupt: - logging.warning( - "Caught KeyboardInterrupt, terminating worker %d", worker_id - ) + logging.warning("Caught KeyboardInterrupt, terminating worker %d", worker_id) except Exception as exc: # pylint: disable=broad-except logging.exception(exc) finally: diff --git a/zero/error.py b/zero/error.py index d04d514..fc0fed8 100644 --- a/zero/error.py +++ b/zero/error.py @@ -1,3 +1,6 @@ +SERVER_PROCESSING_ERROR = "server cannot process message, check server logs for more details" + + class ZeroException(Exception): pass