From 36642b70aa2e1664784f99357e6f7cd012408823 Mon Sep 17 00:00:00 2001 From: Zike Yang Date: Sat, 11 May 2024 17:58:40 +0800 Subject: [PATCH] [fix][client] Fix MessageIdUtils cannot handle TopicMessageId --- .../pulsar/client/util/MessageIdUtils.java | 3 +- .../client/util/MessageIdUtilsTest.java | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 pulsar-client/src/test/java/org/apache/pulsar/client/util/MessageIdUtilsTest.java diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/util/MessageIdUtils.java b/pulsar-client/src/main/java/org/apache/pulsar/client/util/MessageIdUtils.java index 60cdad8e77200..c4275b8b2fd88 100644 --- a/pulsar-client/src/main/java/org/apache/pulsar/client/util/MessageIdUtils.java +++ b/pulsar-client/src/main/java/org/apache/pulsar/client/util/MessageIdUtils.java @@ -19,11 +19,12 @@ package org.apache.pulsar.client.util; import org.apache.pulsar.client.api.MessageId; +import org.apache.pulsar.client.api.MessageIdAdv; import org.apache.pulsar.client.impl.MessageIdImpl; public class MessageIdUtils { public static final long getOffset(MessageId messageId) { - MessageIdImpl msgId = (MessageIdImpl) messageId; + MessageIdAdv msgId = (MessageIdAdv) messageId; long ledgerId = msgId.getLedgerId(); long entryId = msgId.getEntryId(); diff --git a/pulsar-client/src/test/java/org/apache/pulsar/client/util/MessageIdUtilsTest.java b/pulsar-client/src/test/java/org/apache/pulsar/client/util/MessageIdUtilsTest.java new file mode 100644 index 0000000000000..08659c4025f40 --- /dev/null +++ b/pulsar-client/src/test/java/org/apache/pulsar/client/util/MessageIdUtilsTest.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pulsar.client.util; + +import static org.testng.Assert.assertEquals; +import org.apache.pulsar.client.impl.MessageIdImpl; +import org.apache.pulsar.client.impl.TopicMessageIdImpl; +import org.testng.annotations.Test; + +public class MessageIdUtilsTest { + @Test + public void testTopicMessageIdGetOffset() { + MessageIdImpl msgId = new MessageIdImpl(1, 2, 3); + TopicMessageIdImpl topicMsgId = new TopicMessageIdImpl("topic", msgId); + long offset = MessageIdUtils.getOffset(topicMsgId); + assertEquals(offset, 268435458L); + } +}