Search in sources :

Example 6 with PulsarClient

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();
}
Also used : ProducerImpl(com.yahoo.pulsar.client.impl.ProducerImpl) ProducerConfiguration(com.yahoo.pulsar.client.api.ProducerConfiguration) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Example 7 with PulsarClient

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();
    }
}
Also used : ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Example 8 with PulsarClient

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();
    }
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) ClientConfiguration(com.yahoo.pulsar.client.api.ClientConfiguration) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 9 with PulsarClient

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();
    }
}
Also used : AuthenticationTls(com.yahoo.pulsar.client.impl.auth.AuthenticationTls) Consumer(com.yahoo.pulsar.client.api.Consumer) HashMap(java.util.HashMap) Authentication(com.yahoo.pulsar.client.api.Authentication) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) ClientConfiguration(com.yahoo.pulsar.client.api.ClientConfiguration) IOException(java.io.IOException) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 10 with PulsarClient

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();
    }
}
Also used : AuthenticationTls(com.yahoo.pulsar.client.impl.auth.AuthenticationTls) Consumer(com.yahoo.pulsar.client.api.Consumer) HashMap(java.util.HashMap) Authentication(com.yahoo.pulsar.client.api.Authentication) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) ClientConfiguration(com.yahoo.pulsar.client.api.ClientConfiguration) IOException(java.io.IOException) HashSet(java.util.HashSet) 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