Search in sources :

Example 1 with StreamingPullRequest

use of com.google.pubsub.v1.StreamingPullRequest in project google-cloud-java by GoogleCloudPlatform.

the class StreamingSubscriberConnectionTest method testPartitionAckOperations.

@Test
public void testPartitionAckOperations() {
    List<StreamingPullRequest> requests;
    requests = StreamingSubscriberConnection.partitionAckOperations(Collections.<String>emptyList(), Collections.<PendingModifyAckDeadline>emptyList(), 3);
    Truth.assertThat(requests).isEmpty();
    requests = StreamingSubscriberConnection.partitionAckOperations(Arrays.asList("a", "b", "c"), Collections.<PendingModifyAckDeadline>emptyList(), 3);
    Truth.assertThat(requests).containsExactly(StreamingPullRequest.newBuilder().addAckIds("a").addAckIds("b").addAckIds("c").build()).inOrder();
    requests = StreamingSubscriberConnection.partitionAckOperations(Arrays.asList("a", "b", "c", "d"), Collections.<PendingModifyAckDeadline>emptyList(), 3);
    Truth.assertThat(requests).containsExactly(StreamingPullRequest.newBuilder().addAckIds("a").addAckIds("b").addAckIds("c").build(), StreamingPullRequest.newBuilder().addAckIds("d").build()).inOrder();
    requests = StreamingSubscriberConnection.partitionAckOperations(Arrays.asList("a", "b", "c", "d"), Arrays.asList(new PendingModifyAckDeadline(42, "w")), 3);
    Truth.assertThat(requests).containsExactly(StreamingPullRequest.newBuilder().addAckIds("a").addAckIds("b").addAckIds("c").addModifyDeadlineAckIds("w").addModifyDeadlineSeconds(42).build(), StreamingPullRequest.newBuilder().addAckIds("d").build()).inOrder();
    requests = StreamingSubscriberConnection.partitionAckOperations(Arrays.asList("a"), Arrays.asList(new PendingModifyAckDeadline(42, "w", "x")), 3);
    Truth.assertThat(requests).containsExactly(StreamingPullRequest.newBuilder().addAckIds("a").addModifyDeadlineAckIds("w").addModifyDeadlineSeconds(42).addModifyDeadlineAckIds("x").addModifyDeadlineSeconds(42).build()).inOrder();
    requests = StreamingSubscriberConnection.partitionAckOperations(Arrays.asList("a"), Arrays.asList(new PendingModifyAckDeadline(42, "w", "x"), new PendingModifyAckDeadline(43, "y", "z")), 3);
    Truth.assertThat(requests).containsExactly(StreamingPullRequest.newBuilder().addAckIds("a").addModifyDeadlineAckIds("w").addModifyDeadlineSeconds(42).addModifyDeadlineAckIds("x").addModifyDeadlineSeconds(42).addModifyDeadlineAckIds("y").addModifyDeadlineSeconds(43).build(), StreamingPullRequest.newBuilder().addModifyDeadlineAckIds("z").addModifyDeadlineSeconds(43).build()).inOrder();
}
Also used : StreamingPullRequest(com.google.pubsub.v1.StreamingPullRequest) PendingModifyAckDeadline(com.google.cloud.pubsub.spi.v1.MessageDispatcher.PendingModifyAckDeadline) Test(org.junit.Test)

Example 2 with StreamingPullRequest

use of com.google.pubsub.v1.StreamingPullRequest in project google-cloud-java by GoogleCloudPlatform.

the class StreamingSubscriberConnection method partitionAckOperations.

@VisibleForTesting
static List<StreamingPullRequest> partitionAckOperations(List<String> acksToSend, List<PendingModifyAckDeadline> ackDeadlineExtensions, int size) {
    int numExtensions = 0;
    for (PendingModifyAckDeadline modify : ackDeadlineExtensions) {
        numExtensions += modify.ackIds.size();
    }
    int numChanges = Math.max(numExtensions, acksToSend.size());
    int numRequests = numChanges / size + (numChanges % size == 0 ? 0 : 1);
    List<StreamingPullRequest.Builder> requests = new ArrayList<>(numRequests);
    for (int i = 0; i < numRequests; i++) {
        requests.add(StreamingPullRequest.newBuilder());
    }
    int reqCount = 0;
    for (List<String> acksChunk : Lists.partition(acksToSend, size)) {
        requests.get(reqCount).addAllAckIds(acksChunk);
        reqCount++;
    }
    reqCount = 0;
    int ackCount = 0;
    for (PendingModifyAckDeadline modify : ackDeadlineExtensions) {
        for (String ackId : modify.ackIds) {
            requests.get(reqCount).addModifyDeadlineSeconds(modify.deadlineExtensionSeconds).addModifyDeadlineAckIds(ackId);
            ackCount++;
            if (ackCount == size) {
                reqCount++;
                ackCount = 0;
            }
        }
    }
    List<StreamingPullRequest> ret = new ArrayList<>(requests.size());
    for (StreamingPullRequest.Builder builder : requests) {
        ret.add(builder.build());
    }
    return ret;
}
Also used : ArrayList(java.util.ArrayList) StreamingPullRequest(com.google.pubsub.v1.StreamingPullRequest) PendingModifyAckDeadline(com.google.cloud.pubsub.spi.v1.MessageDispatcher.PendingModifyAckDeadline) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with StreamingPullRequest

use of com.google.pubsub.v1.StreamingPullRequest in project google-cloud-java by GoogleCloudPlatform.

the class SubscriptionAdminClientTest method streamingPullExceptionTest.

@Test
@SuppressWarnings("all")
public void streamingPullExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockSubscriber.addException(exception);
    SubscriptionName subscription = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]");
    int streamAckDeadlineSeconds = 1875467245;
    StreamingPullRequest request = StreamingPullRequest.newBuilder().setSubscriptionWithSubscriptionName(subscription).setStreamAckDeadlineSeconds(streamAckDeadlineSeconds).build();
    MockStreamObserver<StreamingPullResponse> responseObserver = new MockStreamObserver<>();
    StreamingCallable<StreamingPullRequest, StreamingPullResponse> callable = client.streamingPullCallable();
    ApiStreamObserver<StreamingPullRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
    requestObserver.onNext(request);
    try {
        List<StreamingPullResponse> actualResponses = responseObserver.future().get();
        Assert.fail("No exception thrown");
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof StatusRuntimeException);
        StatusRuntimeException statusException = (StatusRuntimeException) e.getCause();
        Assert.assertEquals(Status.INVALID_ARGUMENT, statusException.getStatus());
    }
}
Also used : StreamingPullResponse(com.google.pubsub.v1.StreamingPullResponse) StatusRuntimeException(io.grpc.StatusRuntimeException) SubscriptionName(com.google.pubsub.v1.SubscriptionName) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) ExecutionException(java.util.concurrent.ExecutionException) StreamingPullRequest(com.google.pubsub.v1.StreamingPullRequest) Test(org.junit.Test)

Example 4 with StreamingPullRequest

use of com.google.pubsub.v1.StreamingPullRequest in project google-cloud-java by GoogleCloudPlatform.

the class SubscriptionAdminClientTest method streamingPullTest.

@Test
@SuppressWarnings("all")
public void streamingPullTest() throws Exception {
    StreamingPullResponse expectedResponse = StreamingPullResponse.newBuilder().build();
    mockSubscriber.addResponse(expectedResponse);
    SubscriptionName subscription = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]");
    int streamAckDeadlineSeconds = 1875467245;
    StreamingPullRequest request = StreamingPullRequest.newBuilder().setSubscriptionWithSubscriptionName(subscription).setStreamAckDeadlineSeconds(streamAckDeadlineSeconds).build();
    MockStreamObserver<StreamingPullResponse> responseObserver = new MockStreamObserver<>();
    StreamingCallable<StreamingPullRequest, StreamingPullResponse> callable = client.streamingPullCallable();
    ApiStreamObserver<StreamingPullRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
    requestObserver.onNext(request);
    requestObserver.onCompleted();
    List<StreamingPullResponse> actualResponses = responseObserver.future().get();
    Assert.assertEquals(1, actualResponses.size());
    Assert.assertEquals(expectedResponse, actualResponses.get(0));
}
Also used : StreamingPullResponse(com.google.pubsub.v1.StreamingPullResponse) SubscriptionName(com.google.pubsub.v1.SubscriptionName) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) StreamingPullRequest(com.google.pubsub.v1.StreamingPullRequest) Test(org.junit.Test)

Aggregations

StreamingPullRequest (com.google.pubsub.v1.StreamingPullRequest)4 Test (org.junit.Test)3 MockStreamObserver (com.google.api.gax.grpc.testing.MockStreamObserver)2 PendingModifyAckDeadline (com.google.cloud.pubsub.spi.v1.MessageDispatcher.PendingModifyAckDeadline)2 StreamingPullResponse (com.google.pubsub.v1.StreamingPullResponse)2 SubscriptionName (com.google.pubsub.v1.SubscriptionName)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1