| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 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 |
|
| 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() |