def test_publish_retry(self): # raise two errors and then return a mock response self.session.post.side_effect = [ requests.HTTPError(504), OSError, mock.Mock() ] spans = b"[]" self.publisher.publish(SerializedBatch(item_count=1, serialized=spans)) self.assertEqual(self.session.post.call_count, 3)
def test_client_error(self): self.session.post.side_effect = [ requests.HTTPError(400, response=mock.Mock(status_code=400)) ] spans = b"[]" with self.assertRaises(requests.HTTPError): self.publisher.publish( SerializedBatch(item_count=1, serialized=spans)) self.assertEqual(self.session.post.call_count, 1)
def test_fail_on_client_error(self, mock_sleep): self.session.post.side_effect = [ requests.HTTPError(400, response=mock.Mock(status_code=400)) ] events = b'[{"example": "value"}]' with self.assertRaises(requests.HTTPError): self.publisher.publish( SerializedBatch(item_count=1, serialized=events)) self.assertEqual(mock_sleep.call_count, 0) self.assertEqual(self.session.post.call_count, 1)
def test_publish_success(self): events = b'[{"example": "value"}]' self.publisher.publish(SerializedBatch(item_count=1, serialized=events)) self.assertEqual(self.session.post.call_count, 1) args, kwargs = self.session.post.call_args headers = kwargs.get("headers", {}) self.assertEqual(args[0], "https://test.local/v1") self.assertEqual(headers["Content-Type"], "application/json") self.assertEqual( headers["X-Signature"], "key=TestKey, mac=7c46d56b99cd4cb05e08238c1d4c10a2f330795e9d7327f17cc66fd206bf1179", )
def test_empty_batch(self): self.publisher.publish(SerializedBatch(item_count=0, serialized=b"")) self.assertEqual(self.session.post.call_count, 0)
def serialize(self) -> SerializedBatch: serialized = self._header.encode() + b",".join(self._items) + self._end return SerializedBatch(item_count=len(self._items), serialized=serialized)
def serialize(self) -> SerializedBatch: header = (self._header % len(self._items)).encode() return SerializedBatch(item_count=len(self._items), serialized=header + b",".join(self._items) + self._end)