use of com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList in project gax-java by googleapis.
the class BatchingTest method batching.
@Test
public void batching() throws Exception {
BatchingSettings batchingSettings = BatchingSettings.newBuilder().setDelayThreshold(Duration.ofSeconds(1)).setElementCountThreshold(2L).build();
BatchingCallSettings<LabeledIntList, List<Integer>> batchingCallSettings = BatchingCallSettings.newBuilder(SQUARER_BATCHING_DESC).setBatchingSettings(batchingSettings).build();
UnaryCallable<LabeledIntList, List<Integer>> callable = FakeCallableFactory.createBatchingCallable(callLabeledIntSquarer, batchingCallSettings, clientContext);
ApiFuture<List<Integer>> f1 = callable.futureCall(new LabeledIntList("one", 1, 2));
ApiFuture<List<Integer>> f2 = callable.futureCall(new LabeledIntList("one", 3, 4));
Truth.assertThat(f1.get()).isEqualTo(Arrays.asList(1, 4));
Truth.assertThat(f2.get()).isEqualTo(Arrays.asList(9, 16));
}
use of com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList in project gax-java by googleapis.
the class BatchingTest method batchingWithFlowControl.
@Test
public void batchingWithFlowControl() throws Exception {
BatchingSettings batchingSettings = BatchingSettings.newBuilder().setDelayThreshold(Duration.ofSeconds(1)).setElementCountThreshold(4L).setRequestByteThreshold(null).setFlowControlSettings(FlowControlSettings.newBuilder().setLimitExceededBehavior(LimitExceededBehavior.Block).setMaxOutstandingElementCount(10L).setMaxOutstandingRequestBytes(10L).build()).build();
TrackedFlowController trackedFlowController = new TrackedFlowController(batchingSettings.getFlowControlSettings());
Truth.assertThat(trackedFlowController.getElementsReserved()).isEqualTo(0);
Truth.assertThat(trackedFlowController.getElementsReleased()).isEqualTo(0);
Truth.assertThat(trackedFlowController.getBytesReserved()).isEqualTo(0);
Truth.assertThat(trackedFlowController.getBytesReleased()).isEqualTo(0);
Truth.assertThat(trackedFlowController.getCallsToReserve()).isEqualTo(0);
Truth.assertThat(trackedFlowController.getCallsToRelease()).isEqualTo(0);
LabeledIntList requestA = new LabeledIntList("one", 1, 2);
LabeledIntList requestB = new LabeledIntList("one", 3, 4);
BatchingCallSettings<LabeledIntList, List<Integer>> batchingCallSettings = BatchingCallSettings.newBuilder(SQUARER_BATCHING_DESC).setBatchingSettings(batchingSettings).setFlowController(trackedFlowController).build();
Callables.BatchingCreateResult<LabeledIntList, List<Integer>> batchingCreateResult = Callables.batchingImpl(callLabeledIntSquarer, batchingCallSettings, clientContext);
ApiFuture<List<Integer>> f1 = batchingCreateResult.getUnaryCallable().futureCall(requestA, FakeCallContext.createDefault());
ApiFuture<List<Integer>> f2 = batchingCreateResult.getUnaryCallable().futureCall(requestB, FakeCallContext.createDefault());
Truth.assertThat(f1.get()).isEqualTo(Arrays.asList(1, 4));
Truth.assertThat(f2.get()).isEqualTo(Arrays.asList(9, 16));
batchingCreateResult.getBatcherFactory().getPushingBatcher(SQUARER_BATCHING_DESC.getBatchPartitionKey(requestA)).pushCurrentBatch().get();
// Check that the number of bytes is correct even when requests are merged, and the merged
// request consumes fewer bytes.
Truth.assertThat(trackedFlowController.getElementsReserved()).isEqualTo(4);
Truth.assertThat(trackedFlowController.getElementsReleased()).isEqualTo(4);
Truth.assertThat(trackedFlowController.getBytesReserved()).isEqualTo(8);
Truth.assertThat(trackedFlowController.getBytesReleased()).isEqualTo(8);
Truth.assertThat(trackedFlowController.getCallsToReserve()).isEqualTo(2);
Truth.assertThat(trackedFlowController.getCallsToRelease()).isEqualTo(1);
}
use of com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList in project gax-java by googleapis.
the class BatchingTest method batchingDisabled.
@Test
public void batchingDisabled() throws Exception {
BatchingSettings batchingSettings = BatchingSettings.newBuilder().setIsEnabled(false).build();
BatchingCallSettings<LabeledIntList, List<Integer>> batchingCallSettings = BatchingCallSettings.newBuilder(SQUARER_BATCHING_DESC).setBatchingSettings(batchingSettings).build();
UnaryCallable<LabeledIntList, List<Integer>> callable = FakeCallableFactory.createBatchingCallable(callLabeledIntSquarer, batchingCallSettings, clientContext);
ApiFuture<List<Integer>> f1 = callable.futureCall(new LabeledIntList("one", 1, 2));
ApiFuture<List<Integer>> f2 = callable.futureCall(new LabeledIntList("one", 3, 4));
Truth.assertThat(f1.get()).isEqualTo(Arrays.asList(1, 4));
Truth.assertThat(f2.get()).isEqualTo(Arrays.asList(9, 16));
}
use of com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList in project gax-java by googleapis.
the class BatchingTest method batchingException.
@Test
public void batchingException() throws Exception {
BatchingSettings batchingSettings = BatchingSettings.newBuilder().setDelayThreshold(Duration.ofSeconds(1)).setElementCountThreshold(2L).build();
BatchingCallSettings<LabeledIntList, List<Integer>> batchingCallSettings = BatchingCallSettings.newBuilder(SQUARER_BATCHING_DESC).setBatchingSettings(batchingSettings).build();
UnaryCallable<LabeledIntList, List<Integer>> callable = FakeCallableFactory.createBatchingCallable(callLabeledIntExceptionThrower, batchingCallSettings, clientContext);
ApiFuture<List<Integer>> f1 = callable.futureCall(new LabeledIntList("one", 1, 2));
ApiFuture<List<Integer>> f2 = callable.futureCall(new LabeledIntList("one", 3, 4));
try {
f1.get();
Assert.fail("Expected exception from batching call");
} catch (ExecutionException e) {
// expected
}
try {
f2.get();
Assert.fail("Expected exception from batching call");
} catch (ExecutionException e) {
// expected
}
}
use of com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList in project gax-java by googleapis.
the class BatchingTest method batchingWithBlockingCallThreshold.
@Test
public void batchingWithBlockingCallThreshold() throws Exception {
BatchingSettings batchingSettings = BatchingSettings.newBuilder().setDelayThreshold(Duration.ofSeconds(1)).setElementCountThreshold(2L).build();
BatchingCallSettings<LabeledIntList, List<Integer>> batchingCallSettings = BatchingCallSettings.newBuilder(SQUARER_BATCHING_DESC).setBatchingSettings(batchingSettings).build();
UnaryCallable<LabeledIntList, List<Integer>> callable = FakeCallableFactory.createBatchingCallable(callLabeledIntSquarer, batchingCallSettings, clientContext);
ApiFuture<List<Integer>> f1 = callable.futureCall(new LabeledIntList("one", 1));
ApiFuture<List<Integer>> f2 = callable.futureCall(new LabeledIntList("one", 3));
Truth.assertThat(f1.get()).isEqualTo(Arrays.asList(1));
Truth.assertThat(f2.get()).isEqualTo(Arrays.asList(9));
}
Aggregations