use of com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList in project gax-java by googleapis.
the class BatcherImplTest method testPartialFailureInResultProcessing.
@Test
public void testPartialFailureInResultProcessing() throws Exception {
final Queue<RuntimeException> queue = Queues.newArrayBlockingQueue(3);
queue.add(new NullPointerException());
queue.add(new RuntimeException());
queue.add(new ArithmeticException());
SquarerBatchingDescriptorV2 descriptor = new SquarerBatchingDescriptorV2() {
@Override
public void splitResponse(List<Integer> batchResponse, List<BatchEntry<Integer, Integer>> batch) {
throw queue.poll();
}
};
underTest = new BatcherImpl<>(descriptor, callLabeledIntSquarer, labeledIntList, batchingSettings, EXECUTOR);
// This batch should fail with NullPointerException
underTest.add(10);
underTest.flush();
// This batch should fail with RuntimeException
underTest.add(20);
underTest.add(30);
underTest.flush();
// This batch should fail with ArithmeticException
underTest.add(40);
underTest.add(50);
underTest.add(60);
Exception actualError = null;
try {
underTest.close();
} catch (Exception e) {
actualError = e;
}
assertThat(actualError).isInstanceOf(BatchingException.class);
assertThat(actualError).hasMessageThat().contains("Batching finished with 3 batches failed to apply due to:");
assertThat(actualError).hasMessageThat().contains("1 NullPointerException");
assertThat(actualError).hasMessageThat().contains("1 RuntimeException");
assertThat(actualError).hasMessageThat().contains("1 ArithmeticException");
assertThat(actualError).hasMessageThat().contains(" and 0 partial failures.");
}
use of com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList in project gax-java by googleapis.
the class BatcherImplTest method testResultFailureAfterRPCFailure.
/**
* Verifies exception occurred at RPC is propagated to element results
*/
@Test
public void testResultFailureAfterRPCFailure() throws Exception {
final Exception fakeError = new RuntimeException();
UnaryCallable<LabeledIntList, List<Integer>> unaryCallable = new UnaryCallable<LabeledIntList, List<Integer>>() {
@Override
public ApiFuture<List<Integer>> futureCall(LabeledIntList request, ApiCallContext context) {
return ApiFutures.immediateFailedFuture(fakeError);
}
};
underTest = new BatcherImpl<>(SQUARER_BATCHING_DESC_V2, unaryCallable, labeledIntList, batchingSettings, EXECUTOR);
Future<Integer> failedResult = underTest.add(5);
underTest.add(6);
underTest.add(7);
underTest.flush();
assertThat(failedResult.isDone()).isTrue();
Throwable actualError = null;
try {
failedResult.get();
} catch (InterruptedException | ExecutionException ex) {
actualError = ex;
}
assertThat(actualError).hasCauseThat().isSameInstanceAs(fakeError);
try {
underTest.close();
} catch (RuntimeException e) {
actualError = e;
}
assertThat(actualError).isNotNull();
assertThat(actualError).isInstanceOf(BatchingException.class);
assertThat(actualError).hasMessageThat().contains("1 batches failed to apply due to: 1 RuntimeException");
}
use of com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList in project gax-java by googleapis.
the class BatchingCallSettingsTest method testToString.
@Test
public void testToString() {
RetrySettings retrySettings = RetrySettings.newBuilder().build();
Set<StatusCode.Code> retryCodes = ImmutableSet.of(StatusCode.Code.UNAUTHENTICATED);
BatchingCallSettings<Integer, Integer, LabeledIntList, List<Integer>> batchingCallSettings = BatchingCallSettings.newBuilder(SQUARER_BATCHING_DESC_V2).setRetryableCodes(retryCodes).setRetrySettings(retrySettings).setBatchingSettings(BATCHING_SETTINGS).build();
assertThat(batchingCallSettings.toString()).contains("retryableCodes=" + retryCodes);
assertThat(batchingCallSettings.toString()).contains("retrySettings=" + retrySettings);
assertThat(batchingCallSettings.toString()).contains("batchingSettings=" + BATCHING_SETTINGS);
}
use of com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList in project gax-java by googleapis.
the class BatchingCallableTest method testUnbatchedCall.
@Test
public void testUnbatchedCall() throws Exception {
BatchingSettings batchingSettings = BatchingSettings.newBuilder().setIsEnabled(false).build();
FlowControlSettings flowControlSettings = FlowControlSettings.newBuilder().setLimitExceededBehavior(LimitExceededBehavior.Ignore).build();
FlowController flowController = new FlowController(flowControlSettings);
BatcherFactory<LabeledIntList, List<Integer>> batcherFactory = new BatcherFactory<>(new SquarerBatchingDescriptor(), batchingSettings, batchingExecutor, flowController);
BatchingCallable<LabeledIntList, List<Integer>> batchingCallable = new BatchingCallable<>(FakeBatchableApi.callLabeledIntSquarer, FakeBatchableApi.SQUARER_BATCHING_DESC, batcherFactory);
LabeledIntList request1 = new LabeledIntList("label", 2);
ApiFuture<List<Integer>> future1 = batchingCallable.futureCall(request1, FakeCallContext.createDefault());
List<Integer> response1 = future1.get();
Truth.assertThat(response1.size()).isEqualTo(1);
Truth.assertThat(response1.get(0)).isEqualTo(2 * 2);
}
use of com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList in project gax-java by googleapis.
the class BatchingCallableTest method testBatchedCall.
@Test
public void testBatchedCall() throws Exception {
BatchingSettings batchingSettings = BatchingSettings.newBuilder().setDelayThreshold(Duration.ofSeconds(10)).setElementCountThreshold(2L).setRequestByteThreshold(1000L).build();
FlowControlSettings flowControlSettings = FlowControlSettings.newBuilder().setLimitExceededBehavior(LimitExceededBehavior.Ignore).build();
FlowController flowController = new FlowController(flowControlSettings);
BatcherFactory<LabeledIntList, List<Integer>> batcherFactory = new BatcherFactory<>(new SquarerBatchingDescriptor(), batchingSettings, batchingExecutor, flowController);
BatchingCallable<LabeledIntList, List<Integer>> batchingCallable = new BatchingCallable<>(FakeBatchableApi.callLabeledIntSquarer, FakeBatchableApi.SQUARER_BATCHING_DESC, batcherFactory);
LabeledIntList request1 = new LabeledIntList("label", 2);
ApiFuture<List<Integer>> future1 = batchingCallable.futureCall(request1, FakeCallContext.createDefault());
// Assume it won't take 10 seconds (the batching delay threshold) to check the first future
Truth.assertThat(future1.isDone()).isFalse();
LabeledIntList request2 = new LabeledIntList("label", 3);
ApiFuture<List<Integer>> future2 = batchingCallable.futureCall(request2, FakeCallContext.createDefault());
List<Integer> response1 = future1.get();
List<Integer> response2 = future2.get();
Truth.assertThat(response1.size()).isEqualTo(1);
Truth.assertThat(response1.get(0)).isEqualTo(2 * 2);
Truth.assertThat(response2.size()).isEqualTo(1);
Truth.assertThat(response2.get(0)).isEqualTo(3 * 3);
}
Aggregations