Search in sources :

Example 26 with PersistentTopic

use of org.apache.pulsar.broker.service.persistent.PersistentTopic in project incubator-pulsar by apache.

the class ResendRequestTest method testExclusiveCumulativeAckedNormalTopic.

@Test(timeOut = testTimeout)
public void testExclusiveCumulativeAckedNormalTopic() throws Exception {
    String key = "testExclusiveCumulativeAckedNormalTopic";
    final String topicName = "persistent://prop/use/ns-abc/topic-" + key;
    final String subscriptionName = "my-ex-subscription-" + key;
    final String messagePredicate = "my-message-" + key + "-";
    final int totalMessages = 10;
    // 1. producer connect
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
    assertNotNull(topicRef);
    assertEquals(topicRef.getProducers().size(), 1);
    // 2. Create consumer
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).receiverQueueSize(7).subscribe();
    // 3. producer publish messages
    for (int i = 0; i < totalMessages; i++) {
        String message = messagePredicate + i;
        producer.send(message.getBytes());
    }
    // 4. Receive messages
    Message<byte[]> message = consumer.receive();
    log.info("Message received " + new String(message.getData()));
    for (int i = 0; i < 7; i++) {
        printIncomingMessageQueue(consumer);
        message = consumer.receive();
        log.info("Message received " + new String(message.getData()));
    }
    consumer.redeliverUnacknowledgedMessages();
    Thread.sleep(1000);
    consumer.acknowledgeCumulative(message);
    do {
        message = consumer.receive(1000, TimeUnit.MILLISECONDS);
    } while (message != null);
    log.info("Consumer Requests Messages");
    consumer.redeliverUnacknowledgedMessages();
    int numOfReceives = 0;
    message = consumer.receive();
    do {
        numOfReceives += 1;
        log.info("Message received " + new String(message.getData()));
        message = consumer.receive(1000, TimeUnit.MILLISECONDS);
    } while (message != null);
    assertEquals(numOfReceives, 2);
}
Also used : PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) Test(org.testng.annotations.Test)

Example 27 with PersistentTopic

use of org.apache.pulsar.broker.service.persistent.PersistentTopic in project incubator-pulsar by apache.

the class ServerCnxTest method testProducerCommand.

@Test(timeOut = 30000)
public void testProducerCommand() throws Exception {
    resetChannel();
    setChannelConnected();
    // test PRODUCER success case
    ByteBuf clientCommand = Commands.newProducer(successTopicName, 1, /* producer id */
    1, /* request id */
    "prod-name", Collections.emptyMap());
    channel.writeInbound(clientCommand);
    assertTrue(getResponse() instanceof CommandProducerSuccess);
    PersistentTopic topicRef = (PersistentTopic) brokerService.getTopicReference(successTopicName);
    assertNotNull(topicRef);
    assertEquals(topicRef.getProducers().size(), 1);
    // test PRODUCER error case
    clientCommand = Commands.newProducer(failTopicName, 2, 2, "prod-name-2", Collections.emptyMap());
    channel.writeInbound(clientCommand);
    assertTrue(getResponse() instanceof CommandError);
    assertNull(brokerService.getTopicReference(failTopicName));
    channel.finish();
    assertEquals(topicRef.getProducers().size(), 0);
}
Also used : PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) CommandProducerSuccess(org.apache.pulsar.common.api.proto.PulsarApi.CommandProducerSuccess) CommandError(org.apache.pulsar.common.api.proto.PulsarApi.CommandError) ByteBuf(io.netty.buffer.ByteBuf) Test(org.testng.annotations.Test)

Example 28 with PersistentTopic

use of org.apache.pulsar.broker.service.persistent.PersistentTopic in project incubator-pulsar by apache.

the class ServerCnxTest method testSubscribeCommand.

@Test(timeOut = 30000)
public void testSubscribeCommand() throws Exception {
    final String failSubName = "failSub";
    resetChannel();
    setChannelConnected();
    doReturn(false).when(brokerService).isAuthenticationEnabled();
    doReturn(false).when(brokerService).isAuthorizationEnabled();
    // test SUBSCRIBE on topic and cursor creation success
    ByteBuf clientCommand = // 
    Commands.newSubscribe(// 
    successTopicName, successSubName, 1, /* consumer id */
    1, /* request id */
    SubType.Exclusive, 0, "test");
    channel.writeInbound(clientCommand);
    assertTrue(getResponse() instanceof CommandSuccess);
    PersistentTopic topicRef = (PersistentTopic) brokerService.getTopicReference(successTopicName);
    assertNotNull(topicRef);
    assertTrue(topicRef.getSubscriptions().containsKey(successSubName));
    assertTrue(topicRef.getSubscription(successSubName).getDispatcher().isConsumerConnected());
    // test SUBSCRIBE on topic creation success and cursor failure
    clientCommand = Commands.newSubscribe(successTopicName, failSubName, 2, 2, SubType.Exclusive, 0, "test");
    channel.writeInbound(clientCommand);
    assertTrue(getResponse() instanceof CommandError);
    // test SUBSCRIBE on topic creation failure
    clientCommand = Commands.newSubscribe(failTopicName, successSubName, 3, 3, SubType.Exclusive, 0, "test");
    channel.writeInbound(clientCommand);
    assertEquals(getResponse().getClass(), CommandError.class);
    // Server will not close the connection
    assertTrue(channel.isOpen());
    channel.finish();
}
Also used : CommandSuccess(org.apache.pulsar.common.api.proto.PulsarApi.CommandSuccess) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) ByteString(com.google.protobuf.ByteString) CommandError(org.apache.pulsar.common.api.proto.PulsarApi.CommandError) ByteBuf(io.netty.buffer.ByteBuf) Test(org.testng.annotations.Test)

Example 29 with PersistentTopic

use of org.apache.pulsar.broker.service.persistent.PersistentTopic in project incubator-pulsar by apache.

the class ServerCnxTest method testProducerCommandWithAuthorizationPositive.

@Test(timeOut = 30000)
public void testProducerCommandWithAuthorizationPositive() throws Exception {
    AuthorizationService authorizationService = mock(AuthorizationService.class);
    doReturn(CompletableFuture.completedFuture(true)).when(authorizationService).canProduceAsync(Mockito.any(), Mockito.any(), Mockito.any());
    doReturn(authorizationService).when(brokerService).getAuthorizationService();
    doReturn(true).when(brokerService).isAuthenticationEnabled();
    resetChannel();
    setChannelConnected();
    // test PRODUCER success case
    ByteBuf clientCommand = Commands.newProducer(successTopicName, 1, /* producer id */
    1, /* request id */
    "prod-name", Collections.emptyMap());
    channel.writeInbound(clientCommand);
    assertEquals(getResponse().getClass(), CommandProducerSuccess.class);
    PersistentTopic topicRef = (PersistentTopic) brokerService.getTopicReference(successTopicName);
    assertNotNull(topicRef);
    assertEquals(topicRef.getProducers().size(), 1);
    channel.finish();
    assertEquals(topicRef.getProducers().size(), 0);
}
Also used : AuthorizationService(org.apache.pulsar.broker.authorization.AuthorizationService) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) ByteBuf(io.netty.buffer.ByteBuf) Test(org.testng.annotations.Test)

Example 30 with PersistentTopic

use of org.apache.pulsar.broker.service.persistent.PersistentTopic in project incubator-pulsar by apache.

the class ServerCnxTest method testUnsupportedBatchMsgSubscribeCommand.

@Test(timeOut = 30000)
public void testUnsupportedBatchMsgSubscribeCommand() throws Exception {
    final String failSubName = "failSub";
    resetChannel();
    setChannelConnected();
    setConnectionVersion(ProtocolVersion.v3.getNumber());
    doReturn(false).when(brokerService).isAuthenticationEnabled();
    doReturn(false).when(brokerService).isAuthorizationEnabled();
    // test SUBSCRIBE on topic and cursor creation success
    ByteBuf clientCommand = // 
    Commands.newSubscribe(// 
    successTopicName, successSubName, 1, /* consumer id */
    1, /* request id */
    SubType.Exclusive, 0, /* priority */
    "test");
    channel.writeInbound(clientCommand);
    assertTrue(getResponse() instanceof CommandSuccess);
    PersistentTopic topicRef = (PersistentTopic) brokerService.getTopicReference(successTopicName);
    topicRef.markBatchMessagePublished();
    // test SUBSCRIBE on topic and cursor creation success
    clientCommand = Commands.newSubscribe(successTopicName, failSubName, 2, 2, SubType.Exclusive, 0, /* priority */
    "test");
    channel.writeInbound(clientCommand);
    Object response = getResponse();
    assertTrue(response instanceof CommandError);
    assertTrue(((CommandError) response).getError().equals(ServerError.UnsupportedVersionError));
    // Server will not close the connection
    assertTrue(channel.isOpen());
    channel.finish();
}
Also used : CommandSuccess(org.apache.pulsar.common.api.proto.PulsarApi.CommandSuccess) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) Matchers.anyObject(org.mockito.Matchers.anyObject) ByteString(com.google.protobuf.ByteString) CommandError(org.apache.pulsar.common.api.proto.PulsarApi.CommandError) ByteBuf(io.netty.buffer.ByteBuf) Test(org.testng.annotations.Test)

Aggregations

PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)126 Test (org.testng.annotations.Test)100 PersistentSubscription (org.apache.pulsar.broker.service.persistent.PersistentSubscription)34 Field (java.lang.reflect.Field)23 CompletableFuture (java.util.concurrent.CompletableFuture)22 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)22 CountDownLatch (java.util.concurrent.CountDownLatch)20 PersistentDispatcherSingleActiveConsumer (org.apache.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer)20 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)19 ManagedLedgerImpl (org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl)17 ExecutionException (java.util.concurrent.ExecutionException)16 NotAllowedException (org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException)13 KeeperException (org.apache.zookeeper.KeeperException)13 IOException (java.io.IOException)12 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)12 PersistentReplicator (org.apache.pulsar.broker.service.persistent.PersistentReplicator)11 TopicName (org.apache.pulsar.common.naming.TopicName)11 DispatchRate (org.apache.pulsar.common.policies.data.DispatchRate)11 ByteBuf (io.netty.buffer.ByteBuf)10 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)10