From 84df4013a0f5e404d5c383c32c81cd94f478d431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eigil=20Skj=C3=A6veland?= Date: Thu, 20 Jun 2024 13:49:18 +0200 Subject: [PATCH] python client: Support empty lines --- python/zeroeventhub/tests/test_client.py | 25 ++++++++++++++++++++++ python/zeroeventhub/zeroeventhub/client.py | 2 ++ 2 files changed, 27 insertions(+) diff --git a/python/zeroeventhub/tests/test_client.py b/python/zeroeventhub/tests/test_client.py index 85cf474..eab7c60 100644 --- a/python/zeroeventhub/tests/test_client.py +++ b/python/zeroeventhub/tests/test_client.py @@ -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 ): diff --git a/python/zeroeventhub/zeroeventhub/client.py b/python/zeroeventhub/zeroeventhub/client.py index 03904a0..fd4715e 100644 --- a/python/zeroeventhub/zeroeventhub/client.py +++ b/python/zeroeventhub/zeroeventhub/client.py @@ -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]: