| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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', |
| 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 |
|
| 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 |
|
| 78 |
|
| 79 |
assert 0 == reb.revoke_count |
| 80 |
consumer.poll(1) |
| 81 |
assert 1 == reb.revoke_count |
| 82 |
|
| 83 |
consumer.close() |