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