Skip to content

Commit 2b04e54

Browse files
committed
fix message test
1 parent 999e317 commit 2b04e54

File tree

1 file changed

+58
-25
lines changed

1 file changed

+58
-25
lines changed

clients/src/test/java/org/apache/kafka/common/message/MessageTest.java

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -184,39 +184,72 @@ public void testJoinGroupRequestVersions() throws Exception {
184184

185185
@Test
186186
public void testListOffsetsRequestVersions() throws Exception {
187-
List<ListOffsetsTopic> v = Collections.singletonList(new ListOffsetsTopic()
188-
.setName("topic")
189-
.setPartitions(Collections.singletonList(new ListOffsetsPartition()
190-
.setPartitionIndex(0)
191-
.setTimestamp(123L))));
192-
Supplier<ListOffsetsRequestData> newRequest = () -> new ListOffsetsRequestData()
193-
.setTopics(v)
194-
.setReplicaId(0);
195-
testAllMessageRoundTrips(newRequest.get());
196-
testAllMessageRoundTripsFromVersion((short) 2, newRequest.get().setIsolationLevel(IsolationLevel.READ_COMMITTED.id()));
187+
// Test each version with appropriate fields
188+
for (short version = ApiKeys.LIST_OFFSETS.oldestVersion(); version <= ApiKeys.LIST_OFFSETS.latestVersion(); ++version) {
189+
ListOffsetsRequestData request = new ListOffsetsRequestData()
190+
.setReplicaId(0);
191+
192+
if (version >= 2) {
193+
request.setIsolationLevel(IsolationLevel.READ_COMMITTED.id());
194+
}
195+
196+
if (version >= 12) {
197+
// Version 12+: use topic IDs only
198+
Uuid topicId = Uuid.randomUuid();
199+
List<ListOffsetsTopic> topicsWithIds = Collections.singletonList(new ListOffsetsTopic()
200+
.setTopicId(topicId)
201+
.setPartitions(Collections.singletonList(new ListOffsetsPartition()
202+
.setPartitionIndex(0)
203+
.setTimestamp(123L))));
204+
request.setTopics(topicsWithIds);
205+
} else {
206+
// Version < 12: use topic names only
207+
List<ListOffsetsTopic> topicsWithNames = Collections.singletonList(new ListOffsetsTopic()
208+
.setName("topic")
209+
.setPartitions(Collections.singletonList(new ListOffsetsPartition()
210+
.setPartitionIndex(0)
211+
.setTimestamp(123L))));
212+
request.setTopics(topicsWithNames);
213+
}
214+
215+
testEquivalentMessageRoundTrip(version, request);
216+
}
197217
}
198218

199219
@Test
200220
public void testListOffsetsResponseVersions() throws Exception {
201-
ListOffsetsPartitionResponse partition = new ListOffsetsPartitionResponse()
202-
.setErrorCode(Errors.NONE.code())
203-
.setPartitionIndex(0);
204-
List<ListOffsetsTopicResponse> topics = Collections.singletonList(new ListOffsetsTopicResponse()
205-
.setName("topic")
206-
.setPartitions(Collections.singletonList(partition)));
207-
Supplier<ListOffsetsResponseData> response = () -> new ListOffsetsResponseData()
208-
.setTopics(topics);
209221
for (short version = ApiKeys.LIST_OFFSETS.oldestVersion(); version <= ApiKeys.LIST_OFFSETS.latestVersion(); ++version) {
210-
ListOffsetsResponseData responseData = response.get();
211-
responseData.topics().get(0).partitions().get(0)
212-
.setOffset(456L)
213-
.setTimestamp(123L);
214-
if (version > 1) {
215-
responseData.setThrottleTimeMs(1000);
216-
}
222+
ListOffsetsPartitionResponse partition = new ListOffsetsPartitionResponse()
223+
.setErrorCode(Errors.NONE.code())
224+
.setPartitionIndex(0)
225+
.setOffset(456L)
226+
.setTimestamp(123L);
227+
217228
if (version > 3) {
218229
partition.setLeaderEpoch(1);
219230
}
231+
232+
List<ListOffsetsTopicResponse> topics;
233+
if (version >= 12) {
234+
// Version 12+: use topic IDs only
235+
Uuid topicId = Uuid.randomUuid();
236+
topics = Collections.singletonList(new ListOffsetsTopicResponse()
237+
.setTopicId(topicId)
238+
.setPartitions(Collections.singletonList(partition)));
239+
} else {
240+
// Version < 12: use topic names only
241+
topics = Collections.singletonList(new ListOffsetsTopicResponse()
242+
.setName("topic")
243+
.setPartitions(Collections.singletonList(partition)));
244+
}
245+
246+
ListOffsetsResponseData responseData = new ListOffsetsResponseData()
247+
.setTopics(topics);
248+
249+
if (version > 1) {
250+
responseData.setThrottleTimeMs(1000);
251+
}
252+
220253
testEquivalentMessageRoundTrip(version, responseData);
221254
}
222255
}

0 commit comments

Comments
 (0)