Skip to content

Commit 336c850

Browse files
committed
chore: add a test to describe upload behaviour when there are errors
1 parent 4e4cd18 commit 336c850

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

posthog/test/test_consumer.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,40 @@ def mock_post_fn(_, data, **kwargs):
194194
q.put(track)
195195
q.join()
196196
self.assertEqual(mock_post.call_count, 2)
197+
198+
def test_upload_exception_calls_on_error_and_does_not_raise(self):
199+
on_error_called = []
200+
201+
def on_error(e, batch):
202+
on_error_called.append((e, batch))
203+
204+
q = Queue()
205+
consumer = Consumer(q, TEST_API_KEY, on_error=on_error)
206+
track = {"type": "track", "event": "python event", "distinct_id": "distinct_id"}
207+
q.put(track)
208+
209+
with mock.patch.object(
210+
consumer, "request", side_effect=Exception("request failed")
211+
):
212+
result = consumer.upload()
213+
214+
self.assertFalse(result)
215+
self.assertEqual(len(on_error_called), 1)
216+
self.assertIsInstance(on_error_called[0][0], Exception)
217+
self.assertEqual(str(on_error_called[0][0]), "request failed")
218+
219+
def test_upload_exception_in_on_error_does_not_raise(self):
220+
def on_error(e, batch):
221+
raise Exception("on_error failed")
222+
223+
q = Queue()
224+
consumer = Consumer(q, TEST_API_KEY, on_error=on_error)
225+
track = {"type": "track", "event": "python event", "distinct_id": "distinct_id"}
226+
q.put(track)
227+
228+
with mock.patch.object(
229+
consumer, "request", side_effect=Exception("request failed")
230+
):
231+
result = consumer.upload()
232+
233+
self.assertFalse(result)

0 commit comments

Comments
 (0)