|
19 | 19 | package org.apache.pulsar.broker.namespace; |
20 | 20 |
|
21 | 21 | import static org.apache.pulsar.broker.resources.LoadBalanceResources.BUNDLE_DATA_BASE_PATH; |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
22 | 23 | import static org.mockito.ArgumentMatchers.any; |
23 | 24 | import static org.mockito.Mockito.doAnswer; |
24 | 25 | import static org.mockito.Mockito.doReturn; |
|
86 | 87 | import org.apache.pulsar.common.policies.data.Policies; |
87 | 88 | import org.apache.pulsar.common.policies.data.TenantInfo; |
88 | 89 | import org.apache.pulsar.common.policies.data.TenantInfoImpl; |
| 90 | +import org.apache.pulsar.common.policies.data.TopicType; |
89 | 91 | import org.apache.pulsar.common.util.ObjectMapperFactory; |
90 | 92 | import org.apache.pulsar.metadata.api.GetResult; |
91 | 93 | import org.apache.pulsar.metadata.api.MetadataCache; |
@@ -816,23 +818,6 @@ public Object[] topicDomain() { |
816 | 818 | }; |
817 | 819 | } |
818 | 820 |
|
819 | | - @Test(dataProvider = "topicDomain") |
820 | | - public void testCheckTopicExists(String topicDomain) throws Exception { |
821 | | - String topic = topicDomain + "://prop/ns-abc/" + UUID.randomUUID(); |
822 | | - admin.topics().createNonPartitionedTopic(topic); |
823 | | - Awaitility.await().untilAsserted(() -> { |
824 | | - assertTrue(pulsar.getNamespaceService().checkTopicExists(TopicName.get(topic)).get().isExists()); |
825 | | - }); |
826 | | - |
827 | | - String partitionedTopic = topicDomain + "://prop/ns-abc/" + UUID.randomUUID(); |
828 | | - admin.topics().createPartitionedTopic(partitionedTopic, 5); |
829 | | - Awaitility.await().untilAsserted(() -> { |
830 | | - assertTrue(pulsar.getNamespaceService().checkTopicExists(TopicName.get(partitionedTopic)).get().isExists()); |
831 | | - assertTrue(pulsar.getNamespaceService() |
832 | | - .checkTopicExists(TopicName.get(partitionedTopic + "-partition-2")).get().isExists()); |
833 | | - }); |
834 | | - } |
835 | | - |
836 | 821 | @Test |
837 | 822 | public void testAllowedClustersAtNamespaceLevelShouldBeIncludedInAllowedClustersAtTenantLevel() throws Exception { |
838 | 823 | // 1. Setup |
@@ -954,6 +939,94 @@ public void testNewAllowedClusterAdminAPIAndItsImpactOnReplicationClusterAPI() t |
954 | 939 | pulsar.getConfiguration().setForceDeleteTenantAllowed(false); |
955 | 940 | } |
956 | 941 |
|
| 942 | + |
| 943 | + @Test(dataProvider = "topicDomain") |
| 944 | + public void checkTopicExistsForNonPartitionedTopic(String topicDomain) throws Exception { |
| 945 | + TopicName topicName = TopicName.get(topicDomain, "prop", "ns-abc", "topic-" + UUID.randomUUID()); |
| 946 | + admin.topics().createNonPartitionedTopic(topicName.toString()); |
| 947 | + CompletableFuture<TopicExistsInfo> result = pulsar.getNamespaceService().checkTopicExists(topicName); |
| 948 | + assertThat(result) |
| 949 | + .succeedsWithin(3, TimeUnit.SECONDS) |
| 950 | + .satisfies(n -> { |
| 951 | + assertTrue(n.isExists()); |
| 952 | + assertEquals(n.getPartitions(), 0); |
| 953 | + assertEquals(n.getTopicType(), TopicType.NON_PARTITIONED); |
| 954 | + n.recycle(); |
| 955 | + }); |
| 956 | + } |
| 957 | + |
| 958 | + @Test(dataProvider = "topicDomain") |
| 959 | + public void checkTopicExistsForPartitionedTopic(String topicDomain) throws Exception { |
| 960 | + TopicName topicName = TopicName.get(topicDomain, "prop", "ns-abc", "topic-" + UUID.randomUUID()); |
| 961 | + admin.topics().createPartitionedTopic(topicName.toString(), 3); |
| 962 | + |
| 963 | + // Check the topic exists by the partitions. |
| 964 | + CompletableFuture<TopicExistsInfo> result = pulsar.getNamespaceService().checkTopicExists(topicName); |
| 965 | + assertThat(result) |
| 966 | + .succeedsWithin(3, TimeUnit.SECONDS) |
| 967 | + .satisfies(n -> { |
| 968 | + assertTrue(n.isExists()); |
| 969 | + assertEquals(n.getPartitions(), 3); |
| 970 | + assertEquals(n.getTopicType(), TopicType.PARTITIONED); |
| 971 | + n.recycle(); |
| 972 | + }); |
| 973 | + |
| 974 | + // Check the specific partition. |
| 975 | + result = pulsar.getNamespaceService().checkTopicExists(topicName.getPartition(2)); |
| 976 | + assertThat(result) |
| 977 | + .succeedsWithin(3, TimeUnit.SECONDS) |
| 978 | + .satisfies(n -> { |
| 979 | + assertTrue(n.isExists()); |
| 980 | + assertEquals(n.getPartitions(), 0); |
| 981 | + assertEquals(n.getTopicType(), TopicType.NON_PARTITIONED); |
| 982 | + n.recycle(); |
| 983 | + }); |
| 984 | + |
| 985 | + // Partition index is out of range. |
| 986 | + result = pulsar.getNamespaceService().checkTopicExists(topicName.getPartition(10)); |
| 987 | + assertThat(result) |
| 988 | + .succeedsWithin(3, TimeUnit.SECONDS) |
| 989 | + .satisfies(n -> { |
| 990 | + assertFalse(n.isExists()); |
| 991 | + assertEquals(n.getPartitions(), 0); |
| 992 | + assertEquals(n.getTopicType(), TopicType.NON_PARTITIONED); |
| 993 | + n.recycle(); |
| 994 | + }); |
| 995 | + } |
| 996 | + |
| 997 | + @Test(dataProvider = "topicDomain") |
| 998 | + public void checkTopicExistsForNonExistentNonPartitionedTopic(String topicDomain) { |
| 999 | + TopicName topicName = TopicName.get(topicDomain, "prop", "ns-abc", "topic-" + UUID.randomUUID()); |
| 1000 | + CompletableFuture<TopicExistsInfo> result = pulsar.getNamespaceService().checkTopicExists(topicName); |
| 1001 | + assertThat(result) |
| 1002 | + .succeedsWithin(3, TimeUnit.SECONDS) |
| 1003 | + .satisfies(n -> { |
| 1004 | + // when using the pulsar client to check non_persistent topic, always return true, so ignore to |
| 1005 | + // check that. |
| 1006 | + if (topicDomain.equals(TopicDomain.persistent)) { |
| 1007 | + assertFalse(n.isExists()); |
| 1008 | + } |
| 1009 | + n.recycle(); |
| 1010 | + }); |
| 1011 | + } |
| 1012 | + |
| 1013 | + @Test(dataProvider = "topicDomain") |
| 1014 | + public void checkTopicExistsForNonExistentPartitionTopic(String topicDomain) { |
| 1015 | + TopicName topicName = |
| 1016 | + TopicName.get(topicDomain, "prop", "ns-abc", "topic-" + UUID.randomUUID() + "-partition-10"); |
| 1017 | + CompletableFuture<TopicExistsInfo> result = pulsar.getNamespaceService().checkTopicExists(topicName); |
| 1018 | + assertThat(result) |
| 1019 | + .succeedsWithin(3, TimeUnit.SECONDS) |
| 1020 | + .satisfies(n -> { |
| 1021 | + // when using the pulsar client to check non_persistent topic, always return true, so ignore to |
| 1022 | + // check that. |
| 1023 | + if (topicDomain.equals(TopicDomain.persistent)) { |
| 1024 | + assertFalse(n.isExists()); |
| 1025 | + } |
| 1026 | + n.recycle(); |
| 1027 | + }); |
| 1028 | + } |
| 1029 | + |
957 | 1030 | /** |
958 | 1031 | * 1. Manually trigger "LoadReportUpdaterTask" |
959 | 1032 | * 2. Registry another new zk-node-listener "waitForBrokerChangeNotice". |
|
0 commit comments