use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.
the class PersistentTopicE2ETest method testProducerQueueFullBlocking.
@Test
public void testProducerQueueFullBlocking() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/topic-xyzx";
final int messages = 10;
PulsarClient client = PulsarClient.create(brokerUrl.toString());
// 1. Producer connect
ProducerConfiguration producerConfiguration = new ProducerConfiguration().setMaxPendingMessages(messages).setBlockIfQueueFull(true).setSendTimeout(1, TimeUnit.SECONDS);
ProducerImpl producer = (ProducerImpl) client.createProducer(topicName, producerConfiguration);
// 2. Stop broker
cleanup();
// 2. producer publish messages
long startTime = System.nanoTime();
for (int i = 0; i < messages; i++) {
// Should never block
producer.sendAsync("msg".getBytes());
}
// Verify thread was not blocked
long delayNs = System.nanoTime() - startTime;
assertTrue(delayNs < TimeUnit.SECONDS.toNanos(1));
assertEquals(producer.getPendingQueueSize(), messages);
// Next send operation must block, until all the messages in the queue expire
startTime = System.nanoTime();
producer.sendAsync("msg".getBytes());
delayNs = System.nanoTime() - startTime;
assertTrue(delayNs > TimeUnit.MILLISECONDS.toNanos(500));
assertTrue(delayNs < TimeUnit.MILLISECONDS.toNanos(1500));
assertEquals(producer.getPendingQueueSize(), 1);
// 4. producer disconnect
producer.close();
client.close();
// 5. Restart broker
setup();
}
use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.
the class PersistentTopicE2ETest method testMultipleClientsMultipleSubscriptions.
@Test
public void testMultipleClientsMultipleSubscriptions() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/topic7";
final String subName = "sub7";
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
PulsarClient client1 = PulsarClient.create(brokerUrl.toString());
PulsarClient client2 = PulsarClient.create(brokerUrl.toString());
try {
client1.subscribe(topicName, subName, conf);
client1.createProducer(topicName);
client2.createProducer(topicName);
client2.subscribe(topicName, subName, conf);
fail("Should have thrown an exception since one consumer is already connected");
} catch (PulsarClientException cce) {
Assert.assertTrue(cce.getMessage().contains("Exclusive consumer is already connected"));
} finally {
client2.shutdown();
client1.shutdown();
}
}
use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.
the class BrokerServiceTest method testTlsDisabled.
@Test
public void testTlsDisabled() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/newTopic";
final String subName = "newSub";
ClientConfiguration clientConfig;
ConsumerConfiguration consumerConfig;
Consumer consumer;
PulsarClient pulsarClient = null;
conf.setAuthenticationEnabled(false);
conf.setTlsEnabled(false);
restartBroker();
// Case 1: Access without TLS
try {
clientConfig = new ClientConfiguration();
clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
pulsarClient = PulsarClient.create(brokerUrl.toString(), clientConfig);
consumerConfig = new ConsumerConfiguration();
consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
consumer.close();
} catch (Exception e) {
fail("should not fail");
} finally {
pulsarClient.close();
}
// Case 2: Access with TLS
try {
clientConfig = new ClientConfiguration();
clientConfig.setUseTls(true);
clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
consumerConfig = new ConsumerConfiguration();
consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
consumer.close();
fail("TLS connection should fail");
} catch (Exception e) {
assertTrue(e.getMessage().contains("ConnectException"));
} finally {
pulsarClient.close();
}
}
use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.
the class BrokerServiceTest method testTlsAuthDisallowInsecure.
@Test
public void testTlsAuthDisallowInsecure() throws Exception {
final String topicName = "persistent://prop/usw/my-ns/newTopic";
final String subName = "newSub";
ClientConfiguration clientConfig;
ConsumerConfiguration consumerConfig;
Consumer consumer;
Authentication auth;
Set<String> providers = new HashSet<>();
providers.add("com.yahoo.pulsar.broker.authentication.AuthenticationProviderTls");
conf.setAuthenticationEnabled(true);
conf.setAuthenticationProviders(providers);
conf.setTlsEnabled(true);
conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
conf.setTlsAllowInsecureConnection(false);
restartBroker();
Map<String, String> authParams = new HashMap<>();
authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
PulsarClient pulsarClient = null;
// Case 1: Access without client certificate
try {
clientConfig = new ClientConfiguration();
clientConfig.setUseTls(true);
clientConfig.setTlsAllowInsecureConnection(true);
clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
consumerConfig = new ConsumerConfiguration();
consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
consumer.close();
fail("should fail");
} catch (Exception e) {
assertTrue(e.getMessage().contains("Authentication required"));
} finally {
pulsarClient.close();
}
// Case 2: Access with client certificate
try {
auth = new AuthenticationTls();
auth.configure(authParams);
clientConfig = new ClientConfiguration();
clientConfig.setAuthentication(auth);
clientConfig.setUseTls(true);
clientConfig.setTlsAllowInsecureConnection(true);
clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
consumerConfig = new ConsumerConfiguration();
consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
consumer.close();
fail("should fail");
} catch (Exception e) {
assertTrue(e.getMessage().contains("Authentication required"));
} finally {
pulsarClient.close();
}
}
use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.
the class BrokerServiceTest method testTlsAuthAllowInsecure.
@Test
public void testTlsAuthAllowInsecure() throws Exception {
final String topicName = "persistent://prop/usw/my-ns/newTopic";
final String subName = "newSub";
ClientConfiguration clientConfig;
ConsumerConfiguration consumerConfig;
Consumer consumer;
Authentication auth;
Set<String> providers = new HashSet<>();
providers.add("com.yahoo.pulsar.broker.authentication.AuthenticationProviderTls");
conf.setAuthenticationEnabled(true);
conf.setAuthenticationProviders(providers);
conf.setTlsEnabled(true);
conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
conf.setTlsAllowInsecureConnection(true);
restartBroker();
Map<String, String> authParams = new HashMap<>();
authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
PulsarClient pulsarClient = null;
// Case 1: Access without client certificate
try {
clientConfig = new ClientConfiguration();
clientConfig.setUseTls(true);
clientConfig.setTlsAllowInsecureConnection(true);
clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
consumerConfig = new ConsumerConfiguration();
consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
consumer.close();
fail("should fail");
} catch (Exception e) {
assertTrue(e.getMessage().contains("Authentication required"));
} finally {
pulsarClient.close();
}
// Case 2: Access with client certificate
try {
auth = new AuthenticationTls();
auth.configure(authParams);
clientConfig = new ClientConfiguration();
clientConfig.setAuthentication(auth);
clientConfig.setUseTls(true);
clientConfig.setTlsAllowInsecureConnection(true);
clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
consumerConfig = new ConsumerConfiguration();
consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
consumer.close();
} catch (Exception e) {
fail("should not fail");
} finally {
pulsarClient.close();
}
}
Aggregations