From b9810b25cf6b8627022b1eea3b41092de18cd31c Mon Sep 17 00:00:00 2001 From: Petr Heinz Date: Thu, 5 Nov 2026 20:08:38 +0100 Subject: [PATCH 1/2] Add failing tests: a batch Better Stack rejects is reported once per HTTP status A 4xx answer (a wrong source token, a payload too large) drops the batch without retrying it, and today without a word. These tests pin one printed line per rejecting status, with the server's reason, and no retry. Co-Authored-By: Claude Opus 5.5 --- tests/test_flusher.py | 116 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/tests/test_flusher.py b/tests/test_flusher.py index b203372..231ee26 100644 --- a/tests/test_flusher.py +++ b/tests/test_flusher.py @@ -4,6 +4,7 @@ import requests import time import threading +import types import unittest from unittest.mock import patch @@ -201,6 +202,121 @@ def uploader(frame): mock_print.assert_called_once_with('Failed to send logs to Better Stack after 3 retries: network is down') + def _response(self, status_code, reason=None, content=b''): + response = requests.models.Response() + response.status_code = status_code + response.reason = reason + response._content = content + return response + + def _fill(self, pipe): + for log in range(self.buffer_capacity): + pipe.put(log, block=False) + + @patch('logtail.flusher.time.sleep') + def test_step_reports_a_rejected_batch_with_the_server_reason(self, _mock_sleep): + def uploader(frame): + return self._response(401, 'Unauthorized', b'{"errors":"Invalid source token"}') + + pipe, _, fw = self._setup_worker(uploader) + self._fill(pipe) + + with patch('builtins.print') as mock_print: + fw.step() + + self.assertTrue(fw._clean) + mock_print.assert_called_once_with( + 'Better Stack rejected 5 log records with HTTP 401 Unauthorized: {"errors":"Invalid source token"}. ' + "Further rejections with this status won't be reported." + ) + + @patch('logtail.flusher.time.sleep') + def test_step_does_not_retry_a_rejected_batch(self, mock_sleep): + self.uploader_calls = 0 + + def uploader(frame): + self.uploader_calls += 1 + return self._response(401, 'Unauthorized', b'{"errors":"Invalid source token"}') + + pipe, _, fw = self._setup_worker(uploader) + self._fill(pipe) + + with patch('builtins.print'): + fw.step() + + self.assertEqual(self.uploader_calls, 1) + mock_sleep.assert_not_called() + + @patch('logtail.flusher.time.sleep') + def test_step_reports_each_rejecting_status_once(self, _mock_sleep): + responses = iter([ + self._response(401, 'Unauthorized', b'{"errors":"Invalid source token"}'), + self._response(401, 'Unauthorized', b'{"errors":"Invalid source token"}'), + self._response(422, 'Unprocessable Entity'), + self._response(202, 'Accepted'), + self._response(422, 'Unprocessable Entity'), + self._response(401, 'Unauthorized', b'{"errors":"Invalid source token"}'), + ]) + + pipe, _, fw = self._setup_worker(lambda frame: next(responses)) + + with patch('builtins.print') as mock_print: + for _ in range(6): + self._fill(pipe) + fw.step() + + self.assertEqual(mock_print.call_args_list, [ + mock.call( + 'Better Stack rejected 5 log records with HTTP 401 Unauthorized: {"errors":"Invalid source token"}. ' + "Further rejections with this status won't be reported." + ), + mock.call( + 'Better Stack rejected 5 log records with HTTP 422 Unprocessable Entity. ' + "Further rejections with this status won't be reported." + ), + ]) + + def test_step_does_not_report_an_accepted_batch(self): + pipe, _, fw = self._setup_worker(lambda frame: self._response(202, 'Accepted')) + self._fill(pipe) + + with patch('builtins.print') as mock_print: + fw.step() + + mock_print.assert_not_called() + + def test_step_reports_a_rejected_single_record_with_the_start_of_an_html_reason_on_one_line(self): + self.buffer_capacity = 1 + content = ( + b'\r\n413 Request Entity Too Large\r\n\r\n' + b'

413 Request Entity Too Large

\r\n
nginx
\r\n\r\n\r\n' + + b'\r\n' * 6 + ) + pipe, _, fw = self._setup_worker(lambda frame: self._response(413, 'Request Entity Too Large', content)) + self._fill(pipe) + + with patch('builtins.print') as mock_print: + fw.step() + + mock_print.assert_called_once_with( + 'Better Stack rejected 1 log record with HTTP 413 Request Entity Too Large: ' + ' 413 Request Entity Too Large ' + '

413 Request Entity Too Large


nginx
' + '