Search in sources :

Example 1 with SequencedMessage

use of com.google.cloud.pubsublite.SequencedMessage in project java-pubsublite by googleapis.

the class MessageTransformsTest method subscribeTransformCorrect.

@Test
public void subscribeTransformCorrect() throws ApiException {
    SequencedMessage message = SequencedMessage.of(Message.builder().setAttributes(ImmutableListMultimap.<String, ByteString>builder().put("abc", ByteString.EMPTY).put("def", ByteString.copyFromUtf8("hij")).build()).setData(ByteString.copyFrom(notUtf8Array)).setEventTime(Timestamps.fromNanos(10)).setKey(ByteString.copyFromUtf8("some_key")).build(), Timestamps.fromSeconds(5), Offset.of(7), 2);
    PubsubMessage result = PubsubMessage.newBuilder().setData(message.message().data()).setOrderingKey(// The key field
    "some_key").setPublishTime(message.publishTime()).putAttributes("abc", "").putAttributes("def", "hij").putAttributes(MessageTransforms.PUBSUB_LITE_EVENT_TIME_TIMESTAMP_PROTO, MessageTransforms.encodeAttributeEventTime(message.message().eventTime().get())).build();
    assertThat(subscribeTransformer.transform(message)).isEqualTo(result);
}
Also used : SequencedMessage(com.google.cloud.pubsublite.SequencedMessage) PubsubMessage(com.google.pubsub.v1.PubsubMessage) Test(org.junit.Test)

Example 2 with SequencedMessage

use of com.google.cloud.pubsublite.SequencedMessage in project java-pubsublite by googleapis.

the class MessageTransformsTest method wrappedSubscribeTransformerMetadataId.

@Test
public void wrappedSubscribeTransformerMetadataId() throws ApiException {
    MessageTransformer<SequencedMessage, PubsubMessage> wrapped = MessageTransforms.addIdCpsSubscribeTransformer(example(Partition.class), subscribeTransformer);
    SequencedMessage message = SequencedMessage.of(Message.builder().setAttributes(ImmutableListMultimap.<String, ByteString>builder().put("abc", ByteString.EMPTY).put("def", ByteString.copyFromUtf8("hij")).build()).setData(ByteString.copyFrom(notUtf8Array)).setEventTime(Timestamps.fromNanos(10)).setKey(ByteString.copyFromUtf8("some_key")).build(), Timestamps.fromSeconds(5), example(Offset.class), 2);
    PubsubMessage result = PubsubMessage.newBuilder().setData(message.message().data()).setMessageId(MessageMetadata.of(example(Partition.class), example(Offset.class)).encode()).setOrderingKey(// The key field
    "some_key").setPublishTime(message.publishTime()).putAttributes("abc", "").putAttributes("def", "hij").putAttributes(MessageTransforms.PUBSUB_LITE_EVENT_TIME_TIMESTAMP_PROTO, MessageTransforms.encodeAttributeEventTime(message.message().eventTime().get())).build();
    assertThat(wrapped.transform(message)).isEqualTo(result);
}
Also used : Partition(com.google.cloud.pubsublite.Partition) SequencedMessage(com.google.cloud.pubsublite.SequencedMessage) PubsubMessage(com.google.pubsub.v1.PubsubMessage) Offset(com.google.cloud.pubsublite.Offset) Test(org.junit.Test)

Example 3 with SequencedMessage

use of com.google.cloud.pubsublite.SequencedMessage in project java-pubsublite by googleapis.

the class BlockingPullSubscriberImplTest method pullMessagePrioritizeErrorOverExistingMessage.

@Test
public void pullMessagePrioritizeErrorOverExistingMessage() {
    CheckedApiException expected = new CheckedApiException(StatusCode.Code.INTERNAL);
    errorListener.failed(null, expected);
    SequencedMessage message = SequencedMessage.of(Message.builder().build(), Timestamps.EPOCH, Offset.of(12), 30);
    messageConsumer.accept(ImmutableList.of(message));
    CheckedApiException e = assertThrows(CheckedApiException.class, () -> subscriber.messageIfAvailable());
    assertThat(expected).isEqualTo(e);
}
Also used : SequencedMessage(com.google.cloud.pubsublite.SequencedMessage) Test(org.junit.Test)

Example 4 with SequencedMessage

use of com.google.cloud.pubsublite.SequencedMessage in project java-pubsublite by googleapis.

the class BlockingPullSubscriberImplTest method onlyOneMessageDeliveredWhenMultiCalls.

// Not guaranteed to fail if subscriber is not thread safe, investigate if this becomes
// flaky.
@Test
public void onlyOneMessageDeliveredWhenMultiCalls() throws Exception {
    SequencedMessage message = SequencedMessage.of(Message.builder().build(), Timestamps.EPOCH, Offset.of(12), 30);
    messageConsumer.accept(ImmutableList.of(message));
    AtomicInteger count = new AtomicInteger(0);
    CountDownLatch latch = new CountDownLatch(1);
    List<Future<?>> futures = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        futures.add(executorService.submit(() -> {
            try {
                latch.await();
                if (subscriber.messageIfAvailable().isPresent()) {
                    count.incrementAndGet();
                }
            } catch (Exception e) {
                throw new IllegalStateException(e);
            }
        }));
    }
    latch.countDown();
    for (Future<?> f : futures) {
        f.get();
    }
    assertThat(1).isEqualTo(count.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) SequencedMessage(com.google.cloud.pubsublite.SequencedMessage) Test(org.junit.Test)

Example 5 with SequencedMessage

use of com.google.cloud.pubsublite.SequencedMessage in project java-pubsublite by googleapis.

the class BlockingPullSubscriberImplTest method pullMessage.

@Test
public void pullMessage() throws Exception {
    int byteSize = 30;
    SequencedMessage message = SequencedMessage.of(Message.builder().build(), Timestamps.EPOCH, Offset.of(12), byteSize);
    messageConsumer.accept(ImmutableList.of(message));
    assertThat(Optional.of(message)).isEqualTo(subscriber.messageIfAvailable());
    verify(underlying).allowFlow(FlowControlRequest.newBuilder().setAllowedBytes(byteSize).setAllowedMessages(1).build());
}
Also used : SequencedMessage(com.google.cloud.pubsublite.SequencedMessage) Test(org.junit.Test)

Aggregations

SequencedMessage (com.google.cloud.pubsublite.SequencedMessage)24 Test (org.junit.Test)20 Partition (com.google.cloud.pubsublite.Partition)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Offset (com.google.cloud.pubsublite.Offset)4 PubsubMessage (com.google.pubsub.v1.PubsubMessage)4 TopicPath (com.google.cloud.pubsublite.TopicPath)3 FlowControlRequest (com.google.cloud.pubsublite.proto.FlowControlRequest)3 ArrayList (java.util.ArrayList)3 ApiFuture (com.google.api.core.ApiFuture)2 ApiFutureCallback (com.google.api.core.ApiFutureCallback)2 ApiFutures (com.google.api.core.ApiFutures)2 SettableApiFuture (com.google.api.core.SettableApiFuture)2 CheckedApiException (com.google.cloud.pubsublite.internal.CheckedApiException)2 CloseableMonitor (com.google.cloud.pubsublite.internal.CloseableMonitor)2 ExtractStatus (com.google.cloud.pubsublite.internal.ExtractStatus)2 ApiServiceUtils.blockingShutdown (com.google.cloud.pubsublite.internal.wire.ApiServiceUtils.blockingShutdown)2 KafkaExceptionUtils.toKafka (com.google.cloud.pubsublite.kafka.KafkaExceptionUtils.toKafka)2 InitialSubscribeRequest (com.google.cloud.pubsublite.proto.InitialSubscribeRequest)2 SeekRequest (com.google.cloud.pubsublite.proto.SeekRequest)2