1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2020 Confluent Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import pytest
from confluent_kafka import TopicPartition, OFFSET_END, KafkaError, KafkaException
from confluent_kafka.error import ConsumeError
from confluent_kafka.serialization import StringSerializer
from tests.common import TestUtils
def test_consume_error(kafka_cluster):
"""
Tests to ensure librdkafka errors are propagated as
an instance of ConsumeError.
"""
topic = kafka_cluster.create_topic_and_wait_propogation("test_commit_transaction")
consumer_conf = {'group.id': 'pytest', 'enable.partition.eof': True}
producer = kafka_cluster.producer()
producer.produce(topic=topic, value="a")
producer.flush()
consumer = kafka_cluster.consumer(consumer_conf,
value_deserializer=StringSerializer())
consumer.assign([TopicPartition(topic, 0, OFFSET_END)])
with pytest.raises(ConsumeError) as exc_info:
# Trigger EOF error
consumer.poll()
assert exc_info.value.args[0].code() == KafkaError._PARTITION_EOF, \
"Expected _PARTITION_EOF, not {}".format(exc_info)
# Skipping the test for consumer protocol for now. Update the test to use
# IncrementalAlterConfigs Admin operation to update
# group.session.timeout.ms and enable the test again.
@pytest.mark.skipif(TestUtils.use_group_protocol_consumer(),
reason="session.timeout.ms is not supported on client side for "
"consumer protocol. Update this test to use IncrementalAlterConfigs "
"Admin operation to update group.session.timeout.ms and enable "
"the test again.")
def test_consume_error_commit(kafka_cluster):
"""
Tests to ensure that we handle messages with errors when commiting.
"""
topic = kafka_cluster.create_topic_and_wait_propogation("test_commit_transaction")
consumer_conf = {'group.id': 'pytest',
'session.timeout.ms': 100}
producer = kafka_cluster.producer()
producer.produce(topic=topic, value="a")
producer.flush()
consumer = kafka_cluster.cimpl_consumer(consumer_conf)
consumer.subscribe([topic])
try:
# Since the session timeout value is low, JoinGroupRequest will fail
# and we get error in a message while polling.
m = consumer.poll(2)
consumer.commit(m)
except KafkaException as e:
assert e.args[0].code() == KafkaError._INVALID_ARG, \
"Expected INVALID_ARG, not {}".format(e)
# Skipping the test for consumer protocol for now. Update the test to use
# IncrementalAlterConfigs Admin operation to update
# group.session.timeout.ms and enable the test again.
@pytest.mark.skipif(TestUtils.use_group_protocol_consumer(),
reason="session.timeout.ms is not supported on client side for "
"consumer protocol. Update this test to use IncrementalAlterConfigs "
"Admin operation to update group.session.timeout.ms and enable "
"the test again.")
def test_consume_error_store_offsets(kafka_cluster):
"""
Tests to ensure that we handle messages with errors when storing offsets.
"""
topic = kafka_cluster.create_topic_and_wait_propogation("test_commit_transaction")
consumer_conf = {'group.id': 'pytest',
'session.timeout.ms': 100,
'enable.auto.offset.store': True,
'enable.auto.commit': False}
producer = kafka_cluster.producer()
producer.produce(topic=topic, value="a")
producer.flush()
consumer = kafka_cluster.cimpl_consumer(consumer_conf)
consumer.subscribe([topic])
try:
# Since the session timeout value is low, JoinGroupRequest will fail
# and we get error in a message while polling.
m = consumer.poll(2)
consumer.store_offsets(m)
except KafkaException as e:
assert e.args[0].code() == KafkaError._INVALID_ARG, \
"Expected INVALID_ARG, not {}".format(e)
|