示例#1
0
    def test_send_then_receive_with_keys(self):
        with mock_kafka_python() as kafka_mocks:
            client = kafka_mocks.KafkaClient(mock.ANY, )

            producer = kafka_mocks.KeyedProducer(client, )

            producer.send_messages(
                'test_topic',
                0,
                'some message 5',
                'some message 6',
            )

            consumer = kafka_mocks.SimpleConsumer(
                client,
                group='test_group_name',
                topic='test_topic',
            )

            messages = consumer.get_messages(count=2)
            assert len(messages) == 2
            assert [msg.offset for msg in messages] == [0, 1]
            assert [msg.message.value for msg in messages
                    ] == ['some message 5', 'some message 6']
            assert [msg.message.key for msg in messages] == [0, 0]
示例#2
0
    def test_simple_consumer_get_partition_info(self):
        topic = 'random_topic_name'
        with mock_kafka_python() as kmocks:
            client = kmocks.KafkaClient(mock.ANY)
            producer = kmocks.SimpleProducer(client)
            producer.send_messages(topic, *range(7))
            consumer = kmocks.SimpleConsumer(
                client=mock.ANY,
                group='test_group_name',
                topic=topic,
            )

        assert_is_offset_and_message(consumer.get_message())
        assert_is_partition_message(
            consumer.get_message(get_partition_info=True))
        assert_is_offset_and_message(
            consumer.get_message(get_partition_info=False))
        assert_is_offset_and_message(consumer.get_message())
        consumer.provide_partition_info()
        assert_is_partition_message(consumer.get_message())
        assert_is_offset_and_message(
            consumer.get_message(get_partition_info=False))
        assert_is_partition_message(
            consumer.get_message(get_partition_info=None))
        assert consumer.get_message(get_partition_info=True) is None
        assert consumer.get_message(get_partition_info=False) is None
示例#3
0
def kafka_mocks_with_messages():
    with mock_kafka_python() as kafka_mocks:
        client = kafka_mocks.KafkaClient(mock.ANY, )

        producer = kafka_mocks.KeyedProducer(client, )

        producer.send_messages(
            'test_topic',
            0,
            'some message 5',
            'some message 6',
        )
        yield kafka_mocks
示例#4
0
    def test_simple_consumer_auto_commit(self):
        topic = 'random_topic_name'
        with mock_kafka_python() as kmocks:
            client = kmocks.KafkaClient(mock.ANY)
            producer = kmocks.SimpleProducer(client)
            producer.send_messages(topic, *range(1, 5))
            consumer = kmocks.SimpleConsumer(
                client=mock.ANY,
                group='test_group_name',
                topic=topic,
            )

        consumer.get_messages(count=2)
        assert consumer.get_message().message.value == 3
        assert consumer.get_message().message.value == 4
        assert consumer._offset == 4