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);
}
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);
}
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);
}
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());
}
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());
}
Aggregations