Beispiel #1
0
    def test_handle_handler_error(self, context, span, baseplate, name, message):
        handler_fn = mock.Mock(side_effect=ValueError("something went wrong"))
        message_unpack_fn = mock.Mock(
            return_value={"endpoint_timestamp": 1000.0, "body": "some text"}
        )
        on_success_fn = mock.Mock()

        handler = KafkaMessageHandler(baseplate, name, handler_fn, message_unpack_fn, on_success_fn)

        with pytest.raises(ValueError):
            handler.handle(message)

        baseplate.make_context_object.assert_called_once()
        baseplate.make_server_span.assert_called_once_with(context, f"{name}.handler")
        baseplate.make_server_span().__enter__.assert_called_once()
        span.set_tag.assert_has_calls(
            [
                mock.call("kind", "consumer"),
                mock.call("kafka.topic", "topic_1"),
                mock.call("kafka.key", "key_1"),
                mock.call("kafka.partition", 3),
                mock.call("kafka.offset", 33),
                mock.call("kafka.timestamp", 123456),
            ],
            any_order=True,
        )
        message_unpack_fn.assert_called_once_with(b"message-payload")
        handler_fn.assert_called_once_with(
            context, {"endpoint_timestamp": 1000.0, "body": "some text"}, message
        )
        on_success_fn.assert_not_called()
        context.metrics.timer.assert_not_called()
        context.metrics.gauge.assert_not_called()
Beispiel #2
0
    def test_handle_no_endpoint_timestamp(self, context, span, baseplate, name, message):
        handler_fn = mock.Mock()
        message_unpack_fn = mock.Mock(return_value={"body": "some text"})
        on_success_fn = mock.Mock()

        mock_gauge = mock.Mock()
        context.metrics.gauge.return_value = mock_gauge

        handler = KafkaMessageHandler(baseplate, name, handler_fn, message_unpack_fn, on_success_fn)
        handler.handle(message)
        baseplate.make_context_object.assert_called_once()
        baseplate.make_server_span.assert_called_once_with(context, f"{name}.handler")
        baseplate.make_server_span().__enter__.assert_called_once()
        span.set_tag.assert_has_calls(
            [
                mock.call("kind", "consumer"),
                mock.call("kafka.topic", "topic_1"),
                mock.call("kafka.key", "key_1"),
                mock.call("kafka.partition", 3),
                mock.call("kafka.offset", 33),
                mock.call("kafka.timestamp", 123456),
            ],
            any_order=True,
        )
        message_unpack_fn.assert_called_once_with(b"message-payload")
        handler_fn.assert_called_once_with(context, {"body": "some text"}, message)
        on_success_fn.assert_called_once_with(context, {"body": "some text"}, message)

        context.metrics.timer.assert_not_called()

        context.metrics.gauge.assert_called_once_with(f"{name}.topic_1.offset.3")
        mock_gauge.replace.assert_called_once_with(33)
Beispiel #3
0
    def test_handle_unpack_error(self, context, span, baseplate, name, message):
        handler_fn = mock.Mock()
        message_unpack_fn = mock.Mock(side_effect=ValueError("something bad happened"))
        on_success_fn = mock.Mock()

        context.trace = span

        handler = KafkaMessageHandler(baseplate, name, handler_fn, message_unpack_fn, on_success_fn)
        handler.handle(message)
        baseplate.make_context_object.assert_called_once()
        baseplate.make_server_span.assert_called_once_with(context, f"{name}.handler")
        baseplate.make_server_span().__enter__.assert_called_once()
        span.set_tag.assert_has_calls(
            [
                mock.call("kind", "consumer"),
                mock.call("kafka.topic", "topic_1"),
                mock.call("kafka.key", "key_1"),
                mock.call("kafka.partition", 3),
                mock.call("kafka.offset", 33),
                mock.call("kafka.timestamp", 123456),
            ],
            any_order=True,
        )
        span.incr_tag.assert_called_once_with(f"{name}.topic_1.invalid_message")
        message_unpack_fn.assert_called_once_with(b"message-payload")
        handler_fn.assert_not_called()
        on_success_fn.assert_not_called()
        context.metrics.timer.assert_not_called()
        context.metrics.gauge.assert_not_called()
Beispiel #4
0
    def test_handle_kafka_error(self, context, span, baseplate, name, message):
        handler_fn = mock.Mock()
        message_unpack_fn = mock.Mock()
        on_success_fn = mock.Mock()

        # we can't actually create an instance of KafkaError, so use a mock
        error_mock = mock.Mock()
        error_mock.str.return_value = "kafka error"
        message.error.return_value = error_mock

        handler = KafkaMessageHandler(baseplate, name, handler_fn,
                                      message_unpack_fn, on_success_fn)

        with pytest.raises(ValueError):
            handler.handle(message)

        baseplate.make_context_object.assert_called_once()
        baseplate.make_server_span.assert_called_once_with(
            context, f"{name}.handler")
        baseplate.make_server_span().__enter__.assert_called_once()
        span.set_tag.assert_not_called()
        message_unpack_fn.assert_not_called()
        handler_fn.assert_not_called()
        on_success_fn.assert_not_called()
        context.metrics.timer.assert_not_called()
        context.metrics.gauge.assert_not_called()