use of org.apache.pulsar.common.lookup.GetTopicsResult in project pulsar by yahoo.
the class PatternMultiTopicsConsumerImplTest method testChangedUnfilteredResponse.
@Test
public void testChangedUnfilteredResponse() {
PatternMultiTopicsConsumerImpl.updateSubscriptions(Pattern.compile("tenant/my-ns/name-.*"), mockTopicsHashSetter, new GetTopicsResult(Arrays.asList("persistent://tenant/my-ns/name-1", "persistent://tenant/my-ns/name-2", "persistent://tenant/my-ns/non-matching"), null, false, true), mockListener, Collections.emptyList());
verify(mockListener).onTopicsAdded(Sets.newHashSet("persistent://tenant/my-ns/name-1", "persistent://tenant/my-ns/name-2"));
verify(mockListener).onTopicsRemoved(Collections.emptySet());
verify(mockTopicsHashSetter).accept(null);
}
use of org.apache.pulsar.common.lookup.GetTopicsResult in project pulsar by yahoo.
the class PatternMultiTopicsConsumerImplTest method testUnchangedResponse.
@Test
public void testUnchangedResponse() {
PatternMultiTopicsConsumerImpl.updateSubscriptions(Pattern.compile("tenant/my-ns/name-.*"), mockTopicsHashSetter, new GetTopicsResult(Arrays.asList("persistent://tenant/my-ns/name-0", "persistent://tenant/my-ns/name-1", "persistent://tenant/my-ns/name-2"), "TOPICS_HASH", true, false), mockListener, Arrays.asList("persistent://tenant/my-ns/name-0"));
verify(mockListener, never()).onTopicsAdded(any());
verify(mockListener, never()).onTopicsRemoved(any());
verify(mockTopicsHashSetter).accept("TOPICS_HASH");
}
use of org.apache.pulsar.common.lookup.GetTopicsResult in project pulsar by yahoo.
the class PatternMultiTopicsConsumerImplTest method testChangedFilteredResponse.
@Test
public void testChangedFilteredResponse() {
PatternMultiTopicsConsumerImpl.updateSubscriptions(Pattern.compile("tenant/my-ns/name-.*"), mockTopicsHashSetter, new GetTopicsResult(Arrays.asList("persistent://tenant/my-ns/name-0", "persistent://tenant/my-ns/name-1", "persistent://tenant/my-ns/name-2"), "TOPICS_HASH", true, true), mockListener, Arrays.asList("persistent://tenant/my-ns/name-0"));
verify(mockListener).onTopicsAdded(Sets.newHashSet("persistent://tenant/my-ns/name-1", "persistent://tenant/my-ns/name-2"));
verify(mockListener).onTopicsRemoved(Collections.emptySet());
verify(mockTopicsHashSetter).accept("TOPICS_HASH");
}
use of org.apache.pulsar.common.lookup.GetTopicsResult in project pulsar by yahoo.
the class PulsarClientImplTest method testConsumerIsClosed.
@Test
public void testConsumerIsClosed() throws Exception {
// mock client connection
LookupService lookup = mock(LookupService.class);
when(lookup.getTopicsUnderNamespace(any(NamespaceName.class), any(CommandGetTopicsOfNamespace.Mode.class), nullable(String.class), nullable(String.class))).thenReturn(CompletableFuture.completedFuture(new GetTopicsResult(Collections.emptyList(), null, false, true)));
when(lookup.getPartitionedTopicMetadata(any(TopicName.class))).thenReturn(CompletableFuture.completedFuture(new PartitionedTopicMetadata()));
when(lookup.getBroker(any(TopicName.class))).thenReturn(CompletableFuture.completedFuture(Pair.of(mock(InetSocketAddress.class), mock(InetSocketAddress.class))));
ConnectionPool pool = mock(ConnectionPool.class);
ClientCnx cnx = mock(ClientCnx.class);
ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
Channel channel = mock(Channel.class);
when(channel.remoteAddress()).thenReturn(mock(SocketAddress.class));
when(ctx.channel()).thenReturn(channel);
when(ctx.writeAndFlush(any(), any(ChannelPromise.class))).thenReturn(mock(ChannelFuture.class));
when(ctx.voidPromise()).thenReturn(mock(ChannelPromise.class));
when(cnx.channel()).thenReturn(channel);
when(cnx.ctx()).thenReturn(ctx);
when(cnx.sendRequestWithId(any(ByteBuf.class), anyLong())).thenReturn(CompletableFuture.completedFuture(mock(ProducerResponse.class)));
when(pool.getConnection(any(InetSocketAddress.class), any(InetSocketAddress.class))).thenReturn(CompletableFuture.completedFuture(cnx));
Whitebox.setInternalState(clientImpl, "cnxPool", pool);
Whitebox.setInternalState(clientImpl, "lookup", lookup);
List<ConsumerBase<byte[]>> consumers = new ArrayList<>();
/**
* {@link org.apache.pulsar.client.impl.PulsarClientImpl#patternTopicSubscribeAsync}
*/
ConsumerConfigurationData<byte[]> consumerConf0 = new ConsumerConfigurationData<>();
consumerConf0.setSubscriptionName("test-subscription0");
consumerConf0.setTopicsPattern(Pattern.compile("test-topic"));
consumers.add((ConsumerBase) clientImpl.subscribeAsync(consumerConf0).get());
/**
* {@link org.apache.pulsar.client.impl.PulsarClientImpl#singleTopicSubscribeAsync}
*/
ConsumerConfigurationData<byte[]> consumerConf1 = new ConsumerConfigurationData<>();
consumerConf1.setSubscriptionName("test-subscription1");
consumerConf1.setTopicNames(Collections.singleton("test-topic"));
consumers.add((ConsumerBase) clientImpl.subscribeAsync(consumerConf1).get());
/**
* {@link org.apache.pulsar.client.impl.PulsarClientImpl#multiTopicSubscribeAsync}
*/
ConsumerConfigurationData<byte[]> consumerConf2 = new ConsumerConfigurationData<>();
consumerConf2.setSubscriptionName("test-subscription2");
consumers.add((ConsumerBase) clientImpl.subscribeAsync(consumerConf2).get());
consumers.forEach(consumer -> assertNotSame(consumer.getState(), HandlerState.State.Closed));
clientImpl.close();
consumers.forEach(consumer -> assertSame(consumer.getState(), HandlerState.State.Closed));
}
use of org.apache.pulsar.common.lookup.GetTopicsResult in project pulsar by yahoo.
the class PatternMultiTopicsConsumerImpl method run.
// TimerTask to recheck topics change, and trigger subscribe/unsubscribe based on the change.
@Override
public void run(Timeout timeout) throws Exception {
if (timeout.isCancelled()) {
return;
}
client.getLookup().getTopicsUnderNamespace(namespaceName, subscriptionMode, topicsPattern.pattern(), topicsHash).thenCompose(getTopicsResult -> {
if (log.isDebugEnabled()) {
log.debug("Get topics under namespace {}, topics.size: {}, topicsHash: {}, filtered: {}", namespaceName, getTopicsResult.getTopics().size(), getTopicsResult.getTopicsHash(), getTopicsResult.isFiltered());
getTopicsResult.getTopics().forEach(topicName -> log.debug("Get topics under namespace {}, topic: {}", namespaceName, topicName));
}
final List<String> oldTopics = new ArrayList<>(getPartitionedTopics());
for (String partition : getPartitions()) {
TopicName topicName = TopicName.get(partition);
if (!topicName.isPartitioned() || !oldTopics.contains(topicName.getPartitionedTopicName())) {
oldTopics.add(partition);
}
}
return updateSubscriptions(topicsPattern, this::setTopicsHash, getTopicsResult, topicsChangeListener, oldTopics);
}).exceptionally(ex -> {
log.warn("[{}] Failed to recheck topics change: {}", topic, ex.getMessage());
return null;
}).thenAccept(__ -> {
// schedule the next re-check task
this.recheckPatternTimeout = client.timer().newTimeout(PatternMultiTopicsConsumerImpl.this, Math.max(1, conf.getPatternAutoDiscoveryPeriod()), TimeUnit.SECONDS);
});
}
Aggregations