Debian Codegraph @

tests/integration/consumer/test_incremental_assign.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 import pytest
19 from uuid import uuid1
20
21 from confluent_kafka import TopicPartition, OFFSET_BEGINNING, KafkaException, KafkaError
22
23
24 def test_incremental_assign(kafka_cluster):
25 """
26 Test incremental_assign and incremental_unassign
27 """
28
29 consumer_conf = {'group.id': str(uuid1()),
30 'enable.auto.commit': 'false',
31 'auto.offset.reset': 'error'}
32
33 topic1 = kafka_cluster.create_topic_and_wait_propogation("topic1")
34 topic2 = kafka_cluster.create_topic_and_wait_propogation("topic2")
35
36 kafka_cluster.seed_topic(topic1, value_source=[b'a'])
37 kafka_cluster.seed_topic(topic2, value_source=[b'b'])
38
39 consumer = kafka_cluster.consumer(consumer_conf)
40
41 consumer.incremental_assign([TopicPartition(topic1, 0, OFFSET_BEGINNING)])
42 msg1 = consumer.poll(10)
43 assert msg1 is not None
44 assert 0 == msg1.offset()
45 assert topic1 == msg1.topic()
46 assert 0 == msg1.partition()
47 consumer.commit(msg1)
48
49 # should not be possible to incrementally assign to the same partition twice.
50 with pytest.raises(KafkaException) as exc_info:
51 consumer.incremental_assign([TopicPartition(topic1, 0, OFFSET_BEGINNING)])
52 assert exc_info.value.args[0].code() == KafkaError._CONFLICT, \
53 "Expected _CONFLICT, not {}".format(exc_info)
54
55 consumer.incremental_assign([TopicPartition(topic2, 0, OFFSET_BEGINNING)])
56 msg2 = consumer.poll(10)
57 assert msg2 is not None
58 assert 0 == msg2.offset()
59 assert topic2 == msg2.topic()
60 assert 0 == msg2.partition()
61
62 consumer.incremental_unassign([TopicPartition(topic1, 0)])
63 kafka_cluster.seed_topic(topic1, value_source=[b'aa'])
64 msg3 = consumer.poll(2)
65 assert msg3 is None
66
67 kafka_cluster.seed_topic(topic2, value_source=[b'aaa'])
68 msg4 = consumer.poll(10)
69 assert msg4 is not None
70 assert 1 == msg4.offset()
71 assert topic2 == msg4.topic()
72 assert 0 == msg4.partition()
73 assert msg4.value() == b'aaa'
74
75 consumer.incremental_assign([TopicPartition(topic1, 0)])
76 msg5 = consumer.poll(10)
77 assert msg5 is not None
78 assert 1 == msg5.offset()
79 assert topic1 == msg5.topic()
80 assert 0 == msg5.partition()
81
82 msg6 = consumer.poll(1)
83 assert msg6 is None
84
85 consumer.incremental_unassign([TopicPartition(topic1, 0)])
86
87 # should not be possible to incrementally unassign from a partition not in the current assignment
88 with pytest.raises(KafkaException) as exc_info:
89 consumer.incremental_unassign([TopicPartition(topic1, 0)])
90 assert exc_info.value.args[0].code() == KafkaError._INVALID_ARG, \
91 "Expected _INVALID_ARG, not {}".format(exc_info)
92
93 consumer.unassign()