Debian Codegraph @

tests/integration/admin/test_list_offsets.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Copyright 2023 Confluent Inc.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 from confluent_kafka import TopicPartition, IsolationLevel
17 from confluent_kafka.admin import ListOffsetsResultInfo, OffsetSpec
18
19
20 def test_list_offsets(kafka_cluster):
21 """
22 Test list offsets results when asking for the earliest offset (the first one),
23 the latest offset (last one), max timestamp (second one)
24 or after a specific timestamp (second one).
25 """
26
27 admin_client = kafka_cluster.admin()
28
29 # Create a topic with a single partition
30 topic = kafka_cluster.create_topic_and_wait_propogation("test-topic-verify-list-offsets",
31 {
32 "num_partitions": 1,
33 "replication_factor": 1,
34 })
35
36 # Create Producer instance
37 p = kafka_cluster.producer()
38 base_timestamp = 1000000000
39 p.produce(topic, "Message-1", timestamp=(base_timestamp + 100))
40 p.produce(topic, "Message-2", timestamp=(base_timestamp + 400))
41 p.produce(topic, "Message-3", timestamp=(base_timestamp + 200))
42 p.flush()
43
44 topic_partition = TopicPartition(topic, 0)
45
46 for kwargs in [{},
47 {"isolation_level": IsolationLevel.READ_UNCOMMITTED},
48 {"request_timeout": 30},
49 {"isolation_level": IsolationLevel.READ_COMMITTED, "request_timeout": 30}]:
50
51 requests = {topic_partition: OffsetSpec.earliest()}
52 futmap = admin_client.list_offsets(requests, **kwargs)
53 for _, fut in futmap.items():
54 result = fut.result()
55 assert isinstance(result, ListOffsetsResultInfo)
56 assert (result.offset == 0)
57
58 requests = {topic_partition: OffsetSpec.latest()}
59 futmap = admin_client.list_offsets(requests, **kwargs)
60 for _, fut in futmap.items():
61 result = fut.result()
62 assert isinstance(result, ListOffsetsResultInfo)
63 assert (result.offset == 3)
64
65 requests = {topic_partition: OffsetSpec.max_timestamp()}
66 futmap = admin_client.list_offsets(requests, **kwargs)
67 for _, fut in futmap.items():
68 result = fut.result()
69 assert isinstance(result, ListOffsetsResultInfo)
70 assert (result.offset == 1)
71
72 requests = {topic_partition: OffsetSpec.for_timestamp(base_timestamp + 150)}
73 futmap = admin_client.list_offsets(requests, **kwargs)
74 for _, fut in futmap.items():
75 result = fut.result()
76 assert isinstance(result, ListOffsetsResultInfo)
77 assert (result.offset == 1)
78
79 # Delete created topic
80 fs = admin_client.delete_topics([topic])
81 for topic, f in fs.items():
82 f.result()