Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ public boolean topicExist(String topic) {
try {
topicRouteData = this.getTopicRouteDataDirectlyFromNameServer(topic);
topicExist = topicRouteData != null;
} catch (Throwable e) {
topicExist = false;
} catch (Exception e) {
if (TopicRouteHelper.isTopicNotExistError(e)) {
topicExist = false;
} else {
throw new IllegalStateException("get topic route for topic='" + topic + "' failed", e);
}
}

return topicExist;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.rocketmq.remoting.protocol.route.TopicRouteData;
import org.apache.rocketmq.client.impl.mqclient.MQClientAPIExt;
import org.apache.rocketmq.client.impl.mqclient.MQClientAPIFactory;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -35,6 +36,7 @@
import org.mockito.junit.MockitoJUnitRunner;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
Expand Down Expand Up @@ -87,6 +89,27 @@ public void testCreateTopic() throws Exception {
assertEquals(8, topicConfigArgumentCaptor.getValue().getReadQueueNums());
}

@Test
public void testTopicExistReturnsFalseForNotFound() throws Exception {
when(mqClientAPIExt.getTopicRouteInfoFromNameServer(eq("missingTopic"), anyLong()))
.thenThrow(new MQClientException(ResponseCode.TOPIC_NOT_EXIST, "topic not exist"));

assertFalse(defaultAdminService.topicExist("missingTopic"));
}

@Test
public void testTopicExistThrowsForUnexpectedRouteLookupFailure() throws Exception {
MQClientException cause = new MQClientException(ResponseCode.SYSTEM_ERROR, "namesrv unavailable");
when(mqClientAPIExt.getTopicRouteInfoFromNameServer(eq("brokenTopic"), anyLong()))
.thenThrow(cause);

IllegalStateException exception = Assert.assertThrows(IllegalStateException.class,
() -> defaultAdminService.topicExist("brokenTopic"));

assertEquals("get topic route for topic='brokenTopic' failed", exception.getMessage());
assertEquals(cause, exception.getCause());
}

private TopicRouteData createTopicRouteData(int brokerNum) {
TopicRouteData topicRouteData = new TopicRouteData();
for (int i = 0; i < brokerNum; i++) {
Expand All @@ -100,4 +123,4 @@ private TopicRouteData createTopicRouteData(int brokerNum) {
}
return topicRouteData;
}
}
}
Loading