Debian Codegraph @

tests/integration/consumer/test_cooperative_rebalance_2.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 import time
20 from uuid import uuid1
21
22 from confluent_kafka import KafkaError
23 from confluent_kafka.error import ConsumeError
24
25
26 def test_cooperative_rebalance_2(kafka_cluster):
27 """
28 Test that lost partitions events are directed to the
29 revoked handler if the lost partitions handler isn't
30 specified.
31 """
32
33 consumer_conf = {'group.id': str(uuid1()),
34 'partition.assignment.strategy': 'cooperative-sticky',
35 'enable.auto.commit': 'false',
36 'auto.offset.reset': 'earliest',
37 'heartbeat.interval.ms': '2000',
38 'session.timeout.ms': '6000', # minimum allowed by broker
39 'max.poll.interval.ms': '6500'}
40
41 class RebalanceState:
42 def __init__(self):
43 self.assign_count = 0
44 self.revoke_count = 0
45
46 def on_assign(self, consumer, partitions):
47 self.assign_count += 1
48 assert 1 == len(partitions)
49
50 def on_revoke(self, consumer, partitions):
51 self.revoke_count += 1
52
53 reb = RebalanceState()
54
55 topic1 = kafka_cluster.create_topic_and_wait_propogation("topic1")
56
57 consumer = kafka_cluster.consumer(consumer_conf)
58
59 kafka_cluster.seed_topic(topic1, value_source=[b'a'])
60
61 consumer.subscribe([topic1],
62 on_assign=reb.on_assign,
63 on_revoke=reb.on_revoke)
64 msg = consumer.poll(10)
65 assert msg is not None
66 assert msg.value() == b'a'
67 assert reb.assign_count == 1
68
69 # Exceed MaxPollIntervalMs => lost partitions.
70 time.sleep(8)
71
72 with pytest.raises(ConsumeError) as exc_info:
73 consumer.poll(1)
74 assert exc_info.value.args[0].code() == KafkaError._MAX_POLL_EXCEEDED, \
75 "Expected _MAX_POLL_EXCEEDED, not {}".format(exc_info)
76
77 # The second call should trigger the revoked handler (because a lost
78 # handler has not been specified).
79 assert 0 == reb.revoke_count
80 consumer.poll(1)
81 assert 1 == reb.revoke_count
82
83 consumer.close()