Search in sources :

Example 6 with TypedMessageBuilderImpl

use of org.apache.pulsar.client.impl.TypedMessageBuilderImpl in project pulsar by yahoo.

the class V1_ProducerConsumerTest method testInvalidSequence.

@Test
public void testInvalidSequence() throws Exception {
    log.info("-- Starting {} test --", methodName);
    PulsarClient client1 = PulsarClient.builder().serviceUrl(pulsar.getWebServiceAddress()).build();
    client1.close();
    try {
        client1.newConsumer().topic("persistent://my-property/use/my-ns/my-topic6").subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Exclusive).subscribe();
        Assert.fail("Should fail");
    } catch (PulsarClientException e) {
        Assert.assertTrue(e instanceof PulsarClientException.AlreadyClosedException);
    }
    try {
        client1.newProducer().topic("persistent://my-property/use/my-ns/my-topic6").create();
        Assert.fail("Should fail");
    } catch (PulsarClientException e) {
        Assert.assertTrue(e instanceof PulsarClientException.AlreadyClosedException);
    }
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic("persistent://my-property/use/my-ns/my-topic6").subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Exclusive).subscribe();
    Producer<byte[]> producer = pulsarClient.newProducer().topic("persistent://my-property/use/my-ns/my-topic6").create();
    try {
        TypedMessageBuilder<byte[]> builder = producer.newMessage().value("InvalidMessage".getBytes());
        Message<byte[]> msg = ((TypedMessageBuilderImpl<byte[]>) builder).getMessage();
        consumer.acknowledge(msg);
    } catch (PulsarClientException.InvalidMessageException e) {
    // ok
    }
    consumer.close();
    try {
        consumer.receive();
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
    try {
        consumer.unsubscribe();
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
    producer.close();
    try {
        producer.send("message".getBytes());
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
}
Also used : TypedMessageBuilderImpl(org.apache.pulsar.client.impl.TypedMessageBuilderImpl) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Example 7 with TypedMessageBuilderImpl

use of org.apache.pulsar.client.impl.TypedMessageBuilderImpl in project pulsar by yahoo.

the class PartitionedProducerConsumerTest method testInvalidSequence.

@Test(timeOut = 30000)
public void testInvalidSequence() throws Exception {
    log.info("-- Starting {} test --", methodName);
    int numPartitions = 4;
    TopicName topicName = TopicName.get("persistent://my-property/my-ns/my-partitionedtopic4-" + System.currentTimeMillis());
    admin.topics().createPartitionedTopic(topicName.toString(), numPartitions);
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName.toString()).subscriptionName("my-subscriber-name").subscribe();
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName.toString()).enableBatching(false).messageRoutingMode(MessageRoutingMode.SinglePartition).create();
    try {
        TypedMessageBuilderImpl<byte[]> mb = (TypedMessageBuilderImpl<byte[]>) producer.newMessage().value("InvalidMessage".getBytes());
        consumer.acknowledge(mb.getMessage());
    } catch (PulsarClientException.InvalidMessageException e) {
    // ok
    }
    consumer.close();
    try {
        consumer.receive();
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
    try {
        consumer.unsubscribe();
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
    producer.close();
    try {
        producer.send("message".getBytes());
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
    admin.topics().deletePartitionedTopic(topicName.toString());
}
Also used : TypedMessageBuilderImpl(org.apache.pulsar.client.impl.TypedMessageBuilderImpl) TopicName(org.apache.pulsar.common.naming.TopicName) Test(org.testng.annotations.Test)

Example 8 with TypedMessageBuilderImpl

use of org.apache.pulsar.client.impl.TypedMessageBuilderImpl in project incubator-pulsar by apache.

the class ContextImplTest method setup.

@BeforeMethod(alwaysRun = true)
public void setup() throws PulsarClientException {
    config = new InstanceConfig();
    config.setExposePulsarAdminClientEnabled(true);
    FunctionDetails functionDetails = FunctionDetails.newBuilder().setUserConfig("").build();
    config.setFunctionDetails(functionDetails);
    logger = mock(Logger.class);
    pulsarAdmin = mock(PulsarAdmin.class);
    producer = mock(Producer.class);
    client = mock(PulsarClientImpl.class);
    when(client.newProducer()).thenReturn(new ProducerBuilderImpl(client, Schema.BYTES));
    when(client.createProducerAsync(any(ProducerConfigurationData.class), any(), any())).thenReturn(CompletableFuture.completedFuture(producer));
    when(client.getSchema(anyString())).thenReturn(CompletableFuture.completedFuture(Optional.empty()));
    when(producer.sendAsync(anyString())).thenReturn(CompletableFuture.completedFuture(null));
    clientBuilder = mock(ClientBuilder.class);
    when(clientBuilder.build()).thenReturn(client);
    TypedMessageBuilder messageBuilder = spy(new TypedMessageBuilderImpl(mock(ProducerBase.class), Schema.STRING));
    doReturn(new CompletableFuture<>()).when(messageBuilder).sendAsync();
    when(producer.newMessage()).thenReturn(messageBuilder);
    context = new ContextImpl(config, logger, client, new EnvironmentBasedSecretsProvider(), FunctionCollectorRegistry.getDefaultImplementation(), new String[0], FunctionDetails.ComponentType.FUNCTION, null, new InstanceStateManager(), pulsarAdmin, clientBuilder);
    context.setCurrentMessageContext((Record<String>) () -> null);
}
Also used : PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Logger(org.slf4j.Logger) ProducerConfigurationData(org.apache.pulsar.client.impl.conf.ProducerConfigurationData) FunctionDetails(org.apache.pulsar.functions.proto.Function.FunctionDetails) TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) TypedMessageBuilderImpl(org.apache.pulsar.client.impl.TypedMessageBuilderImpl) ProducerBuilderImpl(org.apache.pulsar.client.impl.ProducerBuilderImpl) Producer(org.apache.pulsar.client.api.Producer) InstanceStateManager(org.apache.pulsar.functions.instance.state.InstanceStateManager) EnvironmentBasedSecretsProvider(org.apache.pulsar.functions.secretsprovider.EnvironmentBasedSecretsProvider) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) ClientBuilder(org.apache.pulsar.client.api.ClientBuilder) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 9 with TypedMessageBuilderImpl

use of org.apache.pulsar.client.impl.TypedMessageBuilderImpl in project incubator-pulsar by apache.

the class PartitionedProducerConsumerTest method testInvalidSequence.

@Test(timeOut = 30000)
public void testInvalidSequence() throws Exception {
    log.info("-- Starting {} test --", methodName);
    int numPartitions = 4;
    TopicName topicName = TopicName.get("persistent://my-property/my-ns/my-partitionedtopic4-" + System.currentTimeMillis());
    admin.topics().createPartitionedTopic(topicName.toString(), numPartitions);
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName.toString()).subscriptionName("my-subscriber-name").subscribe();
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName.toString()).enableBatching(false).messageRoutingMode(MessageRoutingMode.SinglePartition).create();
    try {
        TypedMessageBuilderImpl<byte[]> mb = (TypedMessageBuilderImpl<byte[]>) producer.newMessage().value("InvalidMessage".getBytes());
        consumer.acknowledge(mb.getMessage());
    } catch (PulsarClientException.InvalidMessageException e) {
    // ok
    }
    consumer.close();
    try {
        consumer.receive();
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
    try {
        consumer.unsubscribe();
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
    producer.close();
    try {
        producer.send("message".getBytes());
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
    admin.topics().deletePartitionedTopic(topicName.toString());
}
Also used : TypedMessageBuilderImpl(org.apache.pulsar.client.impl.TypedMessageBuilderImpl) TopicName(org.apache.pulsar.common.naming.TopicName) Test(org.testng.annotations.Test)

Example 10 with TypedMessageBuilderImpl

use of org.apache.pulsar.client.impl.TypedMessageBuilderImpl in project incubator-pulsar by apache.

the class V1_ProducerConsumerTest method testInvalidSequence.

@Test
public void testInvalidSequence() throws Exception {
    log.info("-- Starting {} test --", methodName);
    PulsarClient client1 = PulsarClient.builder().serviceUrl(pulsar.getWebServiceAddress()).build();
    client1.close();
    try {
        client1.newConsumer().topic("persistent://my-property/use/my-ns/my-topic6").subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Exclusive).subscribe();
        Assert.fail("Should fail");
    } catch (PulsarClientException e) {
        Assert.assertTrue(e instanceof PulsarClientException.AlreadyClosedException);
    }
    try {
        client1.newProducer().topic("persistent://my-property/use/my-ns/my-topic6").create();
        Assert.fail("Should fail");
    } catch (PulsarClientException e) {
        Assert.assertTrue(e instanceof PulsarClientException.AlreadyClosedException);
    }
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic("persistent://my-property/use/my-ns/my-topic6").subscriptionName("my-subscriber-name").subscriptionType(SubscriptionType.Exclusive).subscribe();
    Producer<byte[]> producer = pulsarClient.newProducer().topic("persistent://my-property/use/my-ns/my-topic6").create();
    try {
        TypedMessageBuilder<byte[]> builder = producer.newMessage().value("InvalidMessage".getBytes());
        Message<byte[]> msg = ((TypedMessageBuilderImpl<byte[]>) builder).getMessage();
        consumer.acknowledge(msg);
    } catch (PulsarClientException.InvalidMessageException e) {
    // ok
    }
    consumer.close();
    try {
        consumer.receive();
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
    try {
        consumer.unsubscribe();
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
    producer.close();
    try {
        producer.send("message".getBytes());
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
}
Also used : TypedMessageBuilderImpl(org.apache.pulsar.client.impl.TypedMessageBuilderImpl) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Aggregations

TypedMessageBuilderImpl (org.apache.pulsar.client.impl.TypedMessageBuilderImpl)15 Test (org.testng.annotations.Test)9 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)3 ClientBuilder (org.apache.pulsar.client.api.ClientBuilder)3 PulsarClient (org.apache.pulsar.client.api.PulsarClient)3 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)3 TypedMessageBuilder (org.apache.pulsar.client.api.TypedMessageBuilder)3 ProducerBuilderImpl (org.apache.pulsar.client.impl.ProducerBuilderImpl)3 PulsarClientImpl (org.apache.pulsar.client.impl.PulsarClientImpl)3 ProducerConfigurationData (org.apache.pulsar.client.impl.conf.ProducerConfigurationData)3 TopicName (org.apache.pulsar.common.naming.TopicName)3 InstanceStateManager (org.apache.pulsar.functions.instance.state.InstanceStateManager)3 FunctionDetails (org.apache.pulsar.functions.proto.Function.FunctionDetails)3 EnvironmentBasedSecretsProvider (org.apache.pulsar.functions.secretsprovider.EnvironmentBasedSecretsProvider)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 Logger (org.slf4j.Logger)3 BeforeMethod (org.testng.annotations.BeforeMethod)3 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 Message (org.apache.pulsar.client.api.Message)2