Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python client: Support empty lines #46

Merged
merged 1 commit into from
Jun 20, 2024
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
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
Loading