Debian Codegraph @

tests/integration/consumer/test_consumer_topicpartition_metadata.py

raw symbols SCIP
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright 2022 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 from confluent_kafka import TopicPartition
19
20
21 def commit_and_check(consumer, topic, metadata):
22 if metadata is None:
23 consumer.commit(offsets=[TopicPartition(topic, 0, 1)], asynchronous=False)
24 else:
25 consumer.commit(offsets=[TopicPartition(topic, 0, 1, metadata)], asynchronous=False)
26
27 offsets = consumer.committed([TopicPartition(topic, 0)], timeout=100)
28 assert len(offsets) == 1
29 assert offsets[0].metadata == metadata
30
31
32 def test_consumer_topicpartition_metadata(kafka_cluster):
33 topic = kafka_cluster.create_topic_and_wait_propogation("test_topicpartition")
34 consumer_conf = {'group.id': 'test_topicpartition'}
35
36 c = kafka_cluster.consumer(consumer_conf)
37
38 # Commit without any metadata.
39 metadata = None
40 commit_and_check(c, topic, metadata)
41
42 # Commit with only ASCII metadata.
43 metadata = 'hello world'
44 commit_and_check(c, topic, metadata)
45
46 # Commit with Unicode characters in metadata.
47 metadata = 'नमस्ते दुनिया'
48 commit_and_check(c, topic, metadata)
49
50 # Commit with empty string as metadata.
51 metadata = ''
52 commit_and_check(c, topic, metadata)
53
54 # Commit with invalid metadata (with null byte in the middle).
55 metadata = 'xyz\x00abc'
56 try:
57 commit_and_check(c, topic, metadata)
58 # We should never reach this point, since the prior statement should throw.
59 assert False
60 except ValueError as ve:
61 assert 'embedded null character' in str(ve)
62
63 c.close()