use of com.yahoo.pulsar.client.api.ConsumerConfiguration in project pulsar by yahoo.
the class PersistentTopicE2ETest method testGracefulClose.
@Test(enabled = false)
public // TODO: enable this after java client supports graceful close
void testGracefulClose() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/topic4";
final String subName = "sub4";
Producer producer = pulsarClient.createProducer(topicName);
Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
assertNotNull(topicRef);
ExecutorService executor = Executors.newCachedThreadPool();
CountDownLatch latch = new CountDownLatch(1);
executor.submit(() -> {
for (int i = 0; i < 10; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
latch.countDown();
return null;
});
producer.close();
// 1. verify there are no pending publish acks once the producer close
// is completed on client
assertEquals(topicRef.getProducers().values().iterator().next().getPendingPublishAcks(), 0);
// safety latch in case of failure,
// wait for the spawned thread to complete
latch.await();
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
Consumer consumer = pulsarClient.subscribe(topicName, subName, conf);
PersistentSubscription subRef = topicRef.getPersistentSubscription(subName);
assertNotNull(subRef);
Message msg = null;
for (int i = 0; i < 10; i++) {
msg = consumer.receive();
}
// message acks
try {
consumer.close();
fail("should have failed");
} catch (IllegalStateException e) {
// Expected - messages not acked
}
consumer.acknowledgeCumulative(msg);
Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
// 3. verify consumer close succeeds once all messages are ack'ed
consumer.close();
Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT);
assertTrue(subRef.getDispatcher().isConsumerConnected());
}
use of com.yahoo.pulsar.client.api.ConsumerConfiguration in project pulsar by yahoo.
the class PersistentTopicE2ETest method testSubscriptionTypeTransitions.
@Test
public void testSubscriptionTypeTransitions() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/shared-topic2";
final String subName = "sub2";
ConsumerConfiguration conf1 = new ConsumerConfiguration();
conf1.setSubscriptionType(SubscriptionType.Exclusive);
ConsumerConfiguration conf2 = new ConsumerConfiguration();
conf2.setSubscriptionType(SubscriptionType.Shared);
ConsumerConfiguration conf3 = new ConsumerConfiguration();
conf3.setSubscriptionType(SubscriptionType.Failover);
Consumer consumer1 = pulsarClient.subscribe(topicName, subName, conf1);
Consumer consumer2 = null;
Consumer consumer3 = null;
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
PersistentSubscription subRef = topicRef.getPersistentSubscription(subName);
// 1. shared consumer on an exclusive sub fails
try {
consumer2 = pulsarClient.subscribe(topicName, subName, conf2);
fail("should have failed");
} catch (PulsarClientException e) {
assertTrue(e.getMessage().contains("Subscription is of different type"));
}
// 2. failover consumer on an exclusive sub fails
try {
consumer3 = pulsarClient.subscribe(topicName, subName, conf3);
fail("should have failed");
} catch (PulsarClientException e) {
assertTrue(e.getMessage().contains("Subscription is of different type"));
}
// 3. disconnected sub can be converted in shared
consumer1.close();
try {
consumer2 = pulsarClient.subscribe(topicName, subName, conf2);
assertEquals(subRef.getDispatcher().getType(), SubType.Shared);
} catch (PulsarClientException e) {
fail("should not fail");
}
// 4. exclusive fails on shared sub
try {
consumer1 = pulsarClient.subscribe(topicName, subName, conf1);
fail("should have failed");
} catch (PulsarClientException e) {
assertTrue(e.getMessage().contains("Subscription is of different type"));
}
// 5. disconnected sub can be converted in failover
consumer2.close();
try {
consumer3 = pulsarClient.subscribe(topicName, subName, conf3);
assertEquals(subRef.getDispatcher().getType(), SubType.Failover);
} catch (PulsarClientException e) {
fail("should not fail");
}
// 5. exclusive consumer can connect after failover disconnects
consumer3.close();
try {
consumer1 = pulsarClient.subscribe(topicName, subName, conf1);
assertEquals(subRef.getDispatcher().getType(), SubType.Exclusive);
} catch (PulsarClientException e) {
fail("should not fail");
}
consumer1.close();
admin.persistentTopics().delete(topicName);
}
use of com.yahoo.pulsar.client.api.ConsumerConfiguration in project pulsar by yahoo.
the class PersistentTopicE2ETest method testMessageExpiry.
@Test
public void testMessageExpiry() throws Exception {
int messageTTLSecs = 1;
String namespaceName = "prop/use/expiry-check";
admin.namespaces().createNamespace(namespaceName);
admin.namespaces().setNamespaceMessageTTL(namespaceName, messageTTLSecs);
final String topicName = "persistent://prop/use/expiry-check/topic1";
final String subName = "sub1";
final int numMsgs = 10;
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
Consumer consumer = pulsarClient.subscribe(topicName, subName, conf);
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
PersistentSubscription subRef = topicRef.getPersistentSubscription(subName);
consumer.close();
assertFalse(subRef.getDispatcher().isConsumerConnected());
Producer producer = pulsarClient.createProducer(topicName);
for (int i = 0; i < numMsgs; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
rolloverPerIntervalStats();
assertEquals(subRef.getNumberOfEntriesInBacklog(), numMsgs);
Thread.sleep(TimeUnit.SECONDS.toMillis(messageTTLSecs));
runMessageExpiryCheck();
// 1. check all messages expired for this unconnected subscription
assertEquals(subRef.getNumberOfEntriesInBacklog(), 0);
// clean-up
producer.close();
consumer.close();
admin.persistentTopics().deleteSubscription(topicName, subName);
admin.persistentTopics().delete(topicName);
admin.namespaces().deleteNamespace(namespaceName);
}
use of com.yahoo.pulsar.client.api.ConsumerConfiguration in project pulsar by yahoo.
the class PersistentTopicE2ETest method testReceiveWithTimeout.
@Test
public void testReceiveWithTimeout() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/topic-receive-timeout";
final String subName = "sub";
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
conf.setReceiverQueueSize(1000);
ConsumerImpl consumer = (ConsumerImpl) pulsarClient.subscribe(topicName, subName, conf);
Producer producer = pulsarClient.createProducer(topicName);
assertEquals(consumer.getAvailablePermits(), 0);
Message msg = consumer.receive(10, TimeUnit.MILLISECONDS);
assertNull(msg);
assertEquals(consumer.getAvailablePermits(), 0);
producer.send("test".getBytes());
Thread.sleep(100);
assertEquals(consumer.getAvailablePermits(), 0);
msg = consumer.receive(10, TimeUnit.MILLISECONDS);
assertNotNull(msg);
assertEquals(consumer.getAvailablePermits(), 1);
msg = consumer.receive(10, TimeUnit.MILLISECONDS);
assertNull(msg);
assertEquals(consumer.getAvailablePermits(), 1);
}
use of com.yahoo.pulsar.client.api.ConsumerConfiguration in project pulsar by yahoo.
the class PersistentTopicE2ETest method testTopicDeleteWithDisconnectedSubscription.
@Test
public void testTopicDeleteWithDisconnectedSubscription() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/topic8";
final String subName = "sub1";
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
// 1. client connect
Consumer consumer = pulsarClient.subscribe(topicName, subName, conf);
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
PersistentSubscription subRef = topicRef.getPersistentSubscription(subName);
assertNotNull(topicRef);
assertNotNull(subRef);
assertTrue(subRef.getDispatcher().isConsumerConnected());
// 2. client disconnect
consumer.close();
assertFalse(subRef.getDispatcher().isConsumerConnected());
// 3. delete topic
admin.persistentTopics().delete(topicName);
try {
admin.persistentTopics().getStats(topicName);
} catch (PulsarAdminException e) {
// ok
}
}
Aggregations