use of com.google.cloud.pubsub.spi.v1.MessageDispatcher.PendingModifyAckDeadline 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.cloud.pubsub.spi.v1.MessageDispatcher.PendingModifyAckDeadline 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;
}
Aggregations