From c2abb0d4983182a28c3398d14c958bfa239ae8da Mon Sep 17 00:00:00 2001 From: Xavier Vello Date: Fri, 1 Dec 2023 17:52:15 +0100 Subject: [PATCH] tests --- posthog/consumer.py | 2 +- posthog/test/test_consumer.py | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/posthog/consumer.py b/posthog/consumer.py index 7f452b2..794c6cc 100644 --- a/posthog/consumer.py +++ b/posthog/consumer.py @@ -105,7 +105,7 @@ def next(self): item = queue.get(block=True, timeout=self.flush_interval - elapsed) item_size = len(json.dumps(item, cls=DatetimeSerializer).encode()) if item_size > MAX_MSG_SIZE: - self.log.error("Item exceeds 32kb limit, dropping. (%s)", str(item)) + self.log.error("Item exceeds 900kib limit, dropping. (%s)", str(item)) continue items.append(item) total_size += item_size diff --git a/posthog/test/test_consumer.py b/posthog/test/test_consumer.py index 58600b6..5d4a907 100644 --- a/posthog/test/test_consumer.py +++ b/posthog/test/test_consumer.py @@ -145,15 +145,20 @@ def test_pause(self): def test_max_batch_size(self): q = Queue() consumer = Consumer(q, TEST_API_KEY, flush_at=100000, flush_interval=3) - track = {"type": "track", "event": "python event", "distinct_id": "distinct_id"} + properties = {} + for n in range(0, 500): + properties[str(n)] = "one_long_property_value_to_build_a_big_event" + track = {"type": "track", "event": "python event", "distinct_id": "distinct_id", "properties": properties} msg_size = len(json.dumps(track).encode()) - # number of messages in a maximum-size batch - n_msgs = int(475000 / msg_size) + # Let's capture 8MB of data to trigger two batches + n_msgs = int(8_000_000 / msg_size) def mock_post_fn(_, data, **kwargs): res = mock.Mock() res.status_code = 200 - self.assertTrue(len(data.encode()) < 500000, "batch size (%d) exceeds 500KB limit" % len(data.encode())) + request_size = len(data.encode()) + # Batches close after the first message bringing it bigger than BATCH_SIZE_LIMIT, let's add 5% of margin + self.assertTrue(request_size < (5 * 1024 * 1024) * 1.05, "batch size (%d) higher than limit" % request_size) return res with mock.patch("posthog.request._session.post", side_effect=mock_post_fn) as mock_post: