Debian Codegraph @

tests/integration/consumer/test_consumer_error.py

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