Search in sources :

Example 6 with OutgoingMessage

use of org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage in project beam by apache.

the class PubsubUnboundedSinkTest method sendOneMessageWithoutAttributes.

@Test
public void sendOneMessageWithoutAttributes() throws IOException {
    List<OutgoingMessage> outgoing = ImmutableList.of(new OutgoingMessage(DATA.getBytes(), null, /* attributes */
    TIMESTAMP, getRecordId(DATA)));
    try (PubsubTestClientFactory factory = PubsubTestClient.createFactoryForPublish(TOPIC, outgoing, ImmutableList.<OutgoingMessage>of())) {
        PubsubUnboundedSink sink = new PubsubUnboundedSink(factory, StaticValueProvider.of(TOPIC), TIMESTAMP_ATTRIBUTE, ID_ATTRIBUTE, NUM_SHARDS, 1, /* batchSize */
        1, /* batchBytes */
        Duration.standardSeconds(2), RecordIdMethod.DETERMINISTIC);
        p.apply(Create.of(ImmutableList.of(DATA))).apply(ParDo.of(new Stamp(null))).apply(sink);
        p.run();
    }
// The PubsubTestClientFactory will assert fail on close if the actual published
// message does not match the expected publish message.
}
Also used : OutgoingMessage(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage) PubsubTestClientFactory(org.apache.beam.sdk.io.gcp.pubsub.PubsubTestClient.PubsubTestClientFactory) Test(org.junit.Test)

Example 7 with OutgoingMessage

use of org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage in project beam by apache.

the class PubsubGrpcClientTest method publishOneMessage.

@Test
public void publishOneMessage() throws IOException {
    String expectedTopic = TOPIC.getPath();
    PubsubMessage expectedPubsubMessage = PubsubMessage.newBuilder().setData(ByteString.copyFrom(DATA.getBytes())).putAllAttributes(ATTRIBUTES).putAllAttributes(ImmutableMap.of(TIMESTAMP_ATTRIBUTE, String.valueOf(MESSAGE_TIME), ID_ATTRIBUTE, RECORD_ID)).build();
    final PublishRequest expectedRequest = PublishRequest.newBuilder().setTopic(expectedTopic).addAllMessages(ImmutableList.of(expectedPubsubMessage)).build();
    final PublishResponse response = PublishResponse.newBuilder().addAllMessageIds(ImmutableList.of(MESSAGE_ID)).build();
    final List<PublishRequest> requestsReceived = new ArrayList<>();
    PublisherImplBase publisherImplBase = new PublisherImplBase() {

        @Override
        public void publish(PublishRequest request, StreamObserver<PublishResponse> responseObserver) {
            requestsReceived.add(request);
            responseObserver.onNext(response);
            responseObserver.onCompleted();
        }
    };
    Server server = InProcessServerBuilder.forName(channelName).addService(publisherImplBase).build().start();
    try {
        OutgoingMessage actualMessage = new OutgoingMessage(DATA.getBytes(), ATTRIBUTES, MESSAGE_TIME, RECORD_ID);
        int n = client.publish(TOPIC, ImmutableList.of(actualMessage));
        assertEquals(1, n);
        assertEquals(expectedRequest, Iterables.getOnlyElement(requestsReceived));
    } finally {
        server.shutdownNow();
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) PublishResponse(com.google.pubsub.v1.PublishResponse) OutgoingMessage(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage) Server(io.grpc.Server) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) PublishRequest(com.google.pubsub.v1.PublishRequest) PubsubMessage(com.google.pubsub.v1.PubsubMessage) PublisherImplBase(com.google.pubsub.v1.PublisherGrpc.PublisherImplBase) Test(org.junit.Test)

Aggregations

OutgoingMessage (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage)7 Test (org.junit.Test)7 PubsubTestClientFactory (org.apache.beam.sdk.io.gcp.pubsub.PubsubTestClient.PubsubTestClientFactory)4 ArrayList (java.util.ArrayList)3 PublishRequest (com.google.api.services.pubsub.model.PublishRequest)1 PublishResponse (com.google.api.services.pubsub.model.PublishResponse)1 PubsubMessage (com.google.api.services.pubsub.model.PubsubMessage)1 ByteString (com.google.protobuf.ByteString)1 PublishRequest (com.google.pubsub.v1.PublishRequest)1 PublishResponse (com.google.pubsub.v1.PublishResponse)1 PublisherImplBase (com.google.pubsub.v1.PublisherGrpc.PublisherImplBase)1 PubsubMessage (com.google.pubsub.v1.PubsubMessage)1 Server (io.grpc.Server)1 StreamObserver (io.grpc.stub.StreamObserver)1 HashMap (java.util.HashMap)1