Search in sources :

Example 16 with SequencedMessage

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

the class PslMicroBatchInputPartitionReaderTest method testPartitionReaderNewMessageExceedsRange.

@Test
public void testPartitionReaderNewMessageExceedsRange() throws Exception {
    long endOffset = 14L;
    createReader(endOffset);
    SequencedMessage message1 = newMessage(10L);
    SequencedMessage message2 = newMessage(endOffset + 1);
    // Multiple get w/o next will return same msg.
    when(subscriber.onData()).thenReturn(ApiFutures.immediateFuture(null));
    when(subscriber.messageIfAvailable()).thenReturn(Optional.of(message1));
    assertThat(reader.next()).isTrue();
    verifyInternalRow(reader.get(), 10L);
    verifyInternalRow(reader.get(), 10L);
    // Next will advance to next msg, and recognize it's out of the batch range.
    when(subscriber.onData()).thenReturn(ApiFutures.immediateFuture(null));
    when(subscriber.messageIfAvailable()).thenReturn(Optional.of(message2));
    assertThat(reader.next()).isFalse();
}
Also used : SequencedMessage(com.google.cloud.pubsublite.SequencedMessage) Test(org.junit.Test)

Example 17 with SequencedMessage

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

the class RecordTransformsTest method subscribeTransform.

@Test
public void subscribeTransform() {
    SequencedMessage sequencedMessage = SequencedMessage.of(MESSAGE, Timestamp.newBuilder().setNanos(12345).build(), example(Offset.class), 123L);
    ConsumerRecord<byte[], byte[]> record = RecordTransforms.fromMessage(sequencedMessage, example(TopicPath.class), example(Partition.class));
    assertThat(record.key()).isEqualTo("abc".getBytes());
    assertThat(record.value()).isEqualTo("def".getBytes());
    assertThat(record.timestampType()).isEqualTo(TimestampType.CREATE_TIME);
    assertThat(record.timestamp()).isEqualTo(1001L);
    ImmutableListMultimap.Builder<String, ByteString> headers = ImmutableListMultimap.builder();
    record.headers().forEach(header -> headers.put(header.key(), ByteString.copyFrom(header.value())));
    assertThat(headers.build()).isEqualTo(MESSAGE.attributes());
    assertThat(record.offset()).isEqualTo(example(Offset.class).value());
    assertThat(record.topic()).isEqualTo(example(TopicPath.class).toString());
    assertThat(record.partition()).isEqualTo(example(Partition.class).value());
}
Also used : Partition(com.google.cloud.pubsublite.Partition) TopicPath(com.google.cloud.pubsublite.TopicPath) ByteString(com.google.protobuf.ByteString) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) SequencedMessage(com.google.cloud.pubsublite.SequencedMessage) ByteString(com.google.protobuf.ByteString) Offset(com.google.cloud.pubsublite.Offset) Test(org.junit.Test)

Example 18 with SequencedMessage

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

the class MessageTransformsTest method wrappedSubscribeTransformerSetsIdFailure.

@Test
public void wrappedSubscribeTransformerSetsIdFailure() throws ApiException {
    MessageTransformer<SequencedMessage, PubsubMessage> mockTransformer = mock(MessageTransformer.class);
    MessageTransformer<SequencedMessage, PubsubMessage> wrapped = MessageTransforms.addIdCpsSubscribeTransformer(example(Partition.class), mockTransformer);
    when(mockTransformer.transform(any())).thenReturn(PubsubMessage.newBuilder().setMessageId("3").build());
    ApiException e = assertThrows(ApiException.class, () -> wrapped.transform(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)));
    assertThat(e.getStatusCode().getCode()).isEqualTo(Code.INVALID_ARGUMENT);
}
Also used : Partition(com.google.cloud.pubsublite.Partition) SequencedMessage(com.google.cloud.pubsublite.SequencedMessage) PubsubMessage(com.google.pubsub.v1.PubsubMessage) CheckedApiException(com.google.cloud.pubsublite.internal.CheckedApiException) ApiException(com.google.api.gax.rpc.ApiException) Test(org.junit.Test)

Example 19 with SequencedMessage

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

the class SinglePartitionSubscriberTest method multiMessageAck.

@Test
public void multiMessageAck() throws CheckedApiException {
    Runnable ack1 = mock(Runnable.class);
    Runnable ack2 = mock(Runnable.class);
    long bytes2 = 111;
    SequencedMessage message2 = SequencedMessage.fromProto(MESSAGE.toProto().toBuilder().setSizeBytes(bytes2).setPublishTime(Timestamps.fromMillis(System.currentTimeMillis())).setCursor(Cursor.newBuilder().setOffset(OFFSET.value() + 1)).build());
    when(ackSetTracker.track(MESSAGE)).thenReturn(ack1);
    when(ackSetTracker.track(message2)).thenReturn(ack2);
    subscriber.onMessages(ImmutableList.of(MESSAGE, message2));
    verify(ackSetTracker).track(MESSAGE);
    verify(ackSetTracker).track(message2);
    verify(receiver).receiveMessage(eq(transformer.transform(MESSAGE)), ackConsumerCaptor.capture());
    verify(receiver).receiveMessage(eq(transformer.transform(message2)), ackConsumerCaptor.capture());
    ackConsumerCaptor.getAllValues().get(1).ack();
    verify(ack2).run();
    verify(wireSubscriber).allowFlow(FlowControlRequest.newBuilder().setAllowedMessages(1).setAllowedBytes(bytes2).build());
    ackConsumerCaptor.getAllValues().get(0).ack();
    verify(ack1).run();
    verify(wireSubscriber).allowFlow(FlowControlRequest.newBuilder().setAllowedMessages(1).setAllowedBytes(BYTE_SIZE).build());
}
Also used : SequencedMessage(com.google.cloud.pubsublite.SequencedMessage) Test(org.junit.Test)

Example 20 with SequencedMessage

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

the class FlowControlBatcherTest method onMessagesDecrementsClientTokens.

@Test
public void onMessagesDecrementsClientTokens() throws CheckedApiException {
    FlowControlRequest clientFlowRequest = FlowControlRequest.newBuilder().setAllowedBytes(500).setAllowedMessages(10).build();
    batcher.onClientFlowRequest(clientFlowRequest);
    ImmutableList<SequencedMessage> messages = ImmutableList.of(SequencedMessage.of(Message.builder().build(), Timestamps.EPOCH, Offset.of(0), 100), SequencedMessage.of(Message.builder().build(), Timestamps.EPOCH, Offset.of(1), 150));
    batcher.onMessages(messages);
    assertThat(batcher.releasePendingRequest().get()).isEqualTo(clientFlowRequest);
    FlowControlRequest expectedRequestForRestart = FlowControlRequest.newBuilder().setAllowedBytes(250).setAllowedMessages(8).build();
    assertThat(batcher.requestForRestart().get()).isEqualTo(expectedRequestForRestart);
}
Also used : FlowControlRequest(com.google.cloud.pubsublite.proto.FlowControlRequest) 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