Skip to content

Commit

Permalink
Fix http request handle issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Ananto30 committed Aug 27, 2023
1 parent da84cf2 commit 11eba6f
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/functional/single_server/client_generation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions tests/functional/single_server/client_server_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime

import pytest
import requests

import zero.error
from zero import AsyncZeroClient, ZeroClient
Expand Down Expand Up @@ -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"
5 changes: 5 additions & 0 deletions tests/functional/single_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pytest
pytest-cov
PyJWT
pytest-asyncio
tornado>=6.1
tornado>=6.1
requests
10 changes: 5 additions & 5 deletions zero/client_server/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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(

Check warning on line 46 in zero/client_server/worker.py

View check run for this annotation

Codecov / codecov/patch

zero/client_server/worker.py#L46

Added line #L46 was not covered by tests
["", {"__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)

Check warning on line 54 in zero/client_server/worker.py

View check run for this annotation

Codecov / codecov/patch

zero/client_server/worker.py#L54

Added line #L54 was not covered by tests
except Exception as exc: # pylint: disable=broad-except
logging.exception(exc)
finally:
Expand Down
3 changes: 3 additions & 0 deletions zero/error.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
SERVER_PROCESSING_ERROR = "server cannot process message, check server logs for more details"


class ZeroException(Exception):
pass

Expand Down

0 comments on commit 11eba6f

Please sign in to comment.