Search in sources :

Example 16 with PulsarClient

use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.

the class ClientErrorsTest method testConsumerReconnect.

@Test
public void testConsumerReconnect() throws Exception {
    AtomicInteger numOfConnections = new AtomicInteger();
    AtomicReference<ChannelHandlerContext> channelCtx = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    mockBrokerService.setHandleConnect((ctx, connect) -> {
        channelCtx.set(ctx);
        ctx.writeAndFlush(Commands.newConnected(connect));
        if (numOfConnections.incrementAndGet() == 2) {
            // close the cnx immediately when trying to conenct the 2nd time
            ctx.channel().close();
        }
        if (numOfConnections.get() == 3) {
            latch.countDown();
        }
    });
    mockBrokerService.setHandleSubscribe((ctx, subscribe) -> {
        ctx.writeAndFlush(Commands.newSuccess(subscribe.getRequestId()));
    });
    PulsarClient client = PulsarClient.create("http://127.0.0.1:" + WEB_SERVICE_PORT);
    client.subscribe("persistent://prop/use/ns/t1", "sub");
    // close the cnx after creating the producer
    channelCtx.get().channel().close();
    latch.await(5, TimeUnit.SECONDS);
    assertEquals(numOfConnections.get(), 3);
    mockBrokerService.resetHandleConnect();
    mockBrokerService.resetHandleSubscribe();
    client.close();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 17 with PulsarClient

use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.

the class ClientErrorsTest method testOneConsumerFailShouldCloseAllConsumersInPartitionedConsumer.

// if a consumer fails to subscribe while creating partitioned consumer, it should close all successful connections
// of other consumers and fail
@Test
public void testOneConsumerFailShouldCloseAllConsumersInPartitionedConsumer() throws Exception {
    PulsarClient client = PulsarClient.create("http://127.0.0.1:" + WEB_SERVICE_PORT);
    final AtomicInteger subscribeCounter = new AtomicInteger(0);
    final AtomicInteger closeCounter = new AtomicInteger(0);
    mockBrokerService.setHandleSubscribe((ctx, subscribe) -> {
        if (subscribeCounter.incrementAndGet() == 3) {
            ctx.writeAndFlush(Commands.newError(subscribe.getRequestId(), ServerError.AuthenticationError, "msg"));
            return;
        }
        ctx.writeAndFlush(Commands.newSuccess(subscribe.getRequestId()));
    });
    mockBrokerService.setHandleCloseConsumer((ctx, closeConsumer) -> {
        ctx.writeAndFlush(Commands.newSuccess(closeConsumer.getRequestId()));
        closeCounter.incrementAndGet();
    });
    try {
        Consumer consumer = client.subscribe("persistent://prop/use/ns/multi-part-t1", "my-sub");
        fail("Should have failed with an authentication error");
    } catch (Exception e) {
        assertTrue(e instanceof PulsarClientException.AuthenticationException);
        // should call close for 3 partitions
        assertEquals(closeCounter.get(), 3);
    }
    mockBrokerService.resetHandleSubscribe();
    mockBrokerService.resetHandleCloseConsumer();
    client.close();
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) LookupException(com.yahoo.pulsar.client.api.PulsarClientException.LookupException) ExecutionException(java.util.concurrent.ExecutionException) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) Test(org.testng.annotations.Test)

Example 18 with PulsarClient

use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.

the class ClientErrorsTest method subscribeFailWithoutRetry.

private void subscribeFailWithoutRetry(String topic) throws Exception {
    PulsarClient client = PulsarClient.create("http://127.0.0.1:" + WEB_SERVICE_PORT);
    final AtomicInteger counter = new AtomicInteger(0);
    mockBrokerService.setHandleSubscribe((ctx, subscribe) -> {
        if (counter.incrementAndGet() == 2) {
            // piggyback unknown error to relay assertion failure
            ctx.writeAndFlush(Commands.newError(subscribe.getRequestId(), ServerError.UnknownError, ASSERTION_ERROR));
            return;
        }
        ctx.writeAndFlush(Commands.newError(subscribe.getRequestId(), ServerError.PersistenceError, "msg"));
    });
    try {
        ConsumerConfiguration conf = new ConsumerConfiguration();
        conf.setSubscriptionType(SubscriptionType.Exclusive);
        Consumer consumer = client.subscribe(topic, "sub1", conf);
    } catch (Exception e) {
        if (e.getMessage().equals(ASSERTION_ERROR)) {
            fail("Subscribe should not retry on persistence error");
        }
        assertTrue(e instanceof PulsarClientException.BrokerPersistenceException);
    }
    mockBrokerService.resetHandleSubscribe();
    client.close();
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) LookupException(com.yahoo.pulsar.client.api.PulsarClientException.LookupException) ExecutionException(java.util.concurrent.ExecutionException) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException)

Example 19 with PulsarClient

use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.

the class ClientErrorsTest method producerCreateSuccessAfterRetry.

private void producerCreateSuccessAfterRetry(String topic) throws Exception {
    PulsarClient client = PulsarClient.create("http://127.0.0.1:" + WEB_SERVICE_PORT);
    final AtomicInteger counter = new AtomicInteger(0);
    mockBrokerService.setHandleProducer((ctx, producer) -> {
        if (counter.incrementAndGet() == 2) {
            ctx.writeAndFlush(Commands.newProducerSuccess(producer.getRequestId(), "default-producer"));
            return;
        }
        ctx.writeAndFlush(Commands.newError(producer.getRequestId(), ServerError.ServiceNotReady, "msg"));
    });
    try {
        Producer producer = client.createProducer(topic);
    } catch (Exception e) {
        fail("Should not fail");
    }
    mockBrokerService.resetHandleProducer();
    client.close();
}
Also used : Producer(com.yahoo.pulsar.client.api.Producer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) LookupException(com.yahoo.pulsar.client.api.PulsarClientException.LookupException) ExecutionException(java.util.concurrent.ExecutionException) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException)

Example 20 with PulsarClient

use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.

the class ClientErrorsTest method testOneProducerFailShouldCloseAllProducersInPartitionedProducer.

// if a producer fails to connect while creating partitioned producer, it should close all successful connections of
// other producers and fail
@Test
public void testOneProducerFailShouldCloseAllProducersInPartitionedProducer() throws Exception {
    PulsarClient client = PulsarClient.create("http://127.0.0.1:" + WEB_SERVICE_PORT);
    final AtomicInteger producerCounter = new AtomicInteger(0);
    final AtomicInteger closeCounter = new AtomicInteger(0);
    mockBrokerService.setHandleProducer((ctx, producer) -> {
        if (producerCounter.incrementAndGet() == 3) {
            ctx.writeAndFlush(Commands.newError(producer.getRequestId(), ServerError.AuthorizationError, "msg"));
            return;
        }
        ctx.writeAndFlush(Commands.newProducerSuccess(producer.getRequestId(), "default-producer"));
    });
    mockBrokerService.setHandleCloseProducer((ctx, closeProducer) -> {
        ctx.writeAndFlush(Commands.newSuccess(closeProducer.getRequestId()));
        closeCounter.incrementAndGet();
    });
    try {
        Producer producer = client.createProducer("persistent://prop/use/ns/multi-part-t1");
        fail("Should have failed with an authorization error");
    } catch (Exception e) {
        assertTrue(e instanceof PulsarClientException.AuthorizationException);
        // should call close for 3 partitions
        assertEquals(closeCounter.get(), 3);
    }
    mockBrokerService.resetHandleProducer();
    mockBrokerService.resetHandleCloseProducer();
    client.close();
}
Also used : Producer(com.yahoo.pulsar.client.api.Producer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) LookupException(com.yahoo.pulsar.client.api.PulsarClientException.LookupException) ExecutionException(java.util.concurrent.ExecutionException) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) Test(org.testng.annotations.Test)

Aggregations

PulsarClient (com.yahoo.pulsar.client.api.PulsarClient)60 Test (org.testng.annotations.Test)39 Consumer (com.yahoo.pulsar.client.api.Consumer)32 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)32 ClientConfiguration (com.yahoo.pulsar.client.api.ClientConfiguration)28 Producer (com.yahoo.pulsar.client.api.Producer)26 ConsumerConfiguration (com.yahoo.pulsar.client.api.ConsumerConfiguration)24 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 LookupException (com.yahoo.pulsar.client.api.PulsarClientException.LookupException)13 ExecutionException (java.util.concurrent.ExecutionException)13 ProducerConfiguration (com.yahoo.pulsar.client.api.ProducerConfiguration)10 PersistentTopicStats (com.yahoo.pulsar.common.policies.data.PersistentTopicStats)10 Message (com.yahoo.pulsar.client.api.Message)9 BacklogQuota (com.yahoo.pulsar.common.policies.data.BacklogQuota)9 IOException (java.io.IOException)8 URI (java.net.URI)6 URL (java.net.URL)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 MockedPulsarServiceBaseTest (com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)5