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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations