Example #1
0
    def test_make_kafka_consumer_unknown_topic(self, kafka_consumer, name,
                                               baseplate, bootstrap_servers,
                                               group_id, topics):
        mock_consumer = mock.Mock()
        mock_consumer.list_topics.return_value = mock.Mock(
            topics={
                "topic_1": mock.Mock(),
                "topic_2": mock.Mock(),
                "topic_3": mock.Mock()
            })
        kafka_consumer.return_value = mock_consumer

        with pytest.raises(AssertionError):
            InOrderConsumerFactory.make_kafka_consumer(bootstrap_servers,
                                                       group_id, ["topic_4"])

        kafka_consumer.assert_called_once_with({
            "bootstrap.servers": bootstrap_servers,
            "group.id": group_id,
            "auto.offset.reset": "latest",
            "heartbeat.interval.ms": 3000,
            "session.timeout.ms": 10000,
            "max.poll.interval.ms": 300000,
            "enable.auto.commit": "false",
        })
        mock_consumer.subscribe.assert_not_called()
Example #2
0
    def test_init(self, kafka_consumer, name, baseplate, bootstrap_servers, group_id, topics):
        mock_consumer = mock.Mock()
        mock_consumer.list_topics.return_value = mock.Mock(
            topics={"topic_1": mock.Mock(), "topic_2": mock.Mock(), "topic_3": mock.Mock()}
        )
        kafka_consumer.return_value = mock_consumer

        handler_fn = mock.Mock()
        message_unpack_fn = mock.Mock()
        health_check_fn = mock.Mock()
        factory = InOrderConsumerFactory.new(
            name=name,
            baseplate=baseplate,
            bootstrap_servers=bootstrap_servers,
            group_id=group_id,
            topics=topics,
            handler_fn=handler_fn,
            message_unpack_fn=message_unpack_fn,
            health_check_fn=health_check_fn,
        )
        assert factory.name == name
        assert factory.baseplate == baseplate
        assert factory.handler_fn == handler_fn
        assert factory.message_unpack_fn == message_unpack_fn
        assert factory.health_check_fn == health_check_fn
        assert factory.consumer == mock_consumer
Example #3
0
    def test_make_kafka_consumer(
        self, kafka_consumer, name, baseplate, bootstrap_servers, group_id, topics
    ):
        mock_consumer = mock.Mock()
        mock_consumer.list_topics.return_value = mock.Mock(
            topics={"topic_1": mock.Mock(), "topic_2": mock.Mock(), "topic_3": mock.Mock()}
        )
        kafka_consumer.return_value = mock_consumer

        _consumer = InOrderConsumerFactory.make_kafka_consumer(bootstrap_servers, group_id, topics)

        assert _consumer == mock_consumer

        kafka_consumer.assert_called_once_with(
            {
                "bootstrap.servers": bootstrap_servers,
                "group.id": group_id,
                "auto.offset.reset": "latest",
                "heartbeat.interval.ms": 3000,
                "session.timeout.ms": 10000,
                "max.poll.interval.ms": 300000,
                "enable.auto.commit": "false",
            }
        )
        mock_consumer.subscribe.assert_called_once()
        assert mock_consumer.subscribe.call_args_list[0][0][0] == topics
Example #4
0
        def _make_queue_consumer_factory(kafka_consumer, health_check_fn=None):
            mock_consumer = mock.Mock()
            mock_consumer.list_topics.return_value = mock.Mock(
                topics={"topic_1": mock.Mock(), "topic_2": mock.Mock(), "topic_3": mock.Mock()}
            )
            kafka_consumer.return_value = mock_consumer

            return InOrderConsumerFactory.new(
                name=name,
                baseplate=baseplate,
                bootstrap_servers=bootstrap_servers,
                group_id=group_id,
                topics=topics,
                handler_fn=lambda ctx, data, msg: True,
                message_unpack_fn=lambda b: {},
                health_check_fn=health_check_fn,
            )