Skip to content

Commit

Permalink
python client: Support empty lines
Browse files Browse the repository at this point in the history
  • Loading branch information
eigilhs committed Jun 20, 2024
1 parent 0015115 commit 84df401
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
25 changes: 25 additions & 0 deletions python/zeroeventhub/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,31 @@ async def test_fetch_events_succeeds_when_response_is_empty(
mock_event_receiver.checkpoint.assert_not_called()


async def test_fetch_events_succeeds_when_response_is_empty_line(
client, mock_event_receiver, respx_mock
):
"""Test that fetch_events gracefully handles an empty line in the response."""
# arrange
cursors = [Cursor(1, "cursor1"), Cursor(2, "cursor2")]
page_size_hint = 10
headers = None

respx_mock.get(client.url).mock(
return_value=httpx.Response(
status_code=204,
headers={"content_type": "application/x-ndjson"},
content="\n",
)
)

# act
await receive_events(mock_event_receiver, client.fetch_events(cursors, page_size_hint, headers))

# assert that the event and checkpoint methods were not called
mock_event_receiver.event.assert_not_called()
mock_event_receiver.checkpoint.assert_not_called()


async def test_raises_error_when_response_contains_invalid_json_line(
client, mock_event_receiver, respx_mock
):
Expand Down
2 changes: 2 additions & 0 deletions python/zeroeventhub/zeroeventhub/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ async def _process_response(
res.raise_for_status()

async for line in aiter_lines(res, "\n"):
if not line:
continue
yield self._parse_checkpoint_or_event(line)

def _parse_checkpoint_or_event(self, raw_line: str) -> Union[Event, Cursor]:
Expand Down

0 comments on commit 84df401

Please sign in to comment.