use of com.google.api.gax.rpc.testing.FakeBatchableApi.SquarerBatchingDescriptorV2 in project gax-java by googleapis.
the class BatcherImplTest method testExceptionInDescriptorErrorHandling.
/**
* Resolves future results when {@link BatchingDescriptor#splitException} throws exception
*/
@Test
public void testExceptionInDescriptorErrorHandling() throws InterruptedException {
final RuntimeException fakeError = new RuntimeException("internal exception");
BatchingDescriptor<Integer, Integer, LabeledIntList, List<Integer>> descriptor = new SquarerBatchingDescriptorV2() {
@Override
public void splitResponse(List<Integer> batchResponse, List<BatchEntry<Integer, Integer>> batch) {
throw fakeError;
}
@Override
public void splitException(Throwable throwable, List<BatchEntry<Integer, Integer>> batch) {
throw fakeError;
}
};
underTest = new BatcherImpl<>(descriptor, callLabeledIntSquarer, labeledIntList, batchingSettings, EXECUTOR);
Future<Integer> result = underTest.add(2);
underTest.flush();
Throwable actualError = null;
try {
result.get();
} catch (ExecutionException ex) {
actualError = ex;
}
assertThat(actualError).hasCauseThat().isSameInstanceAs(fakeError);
try {
underTest.close();
} catch (Exception ex) {
actualError = ex;
}
assertThat(actualError).isInstanceOf(BatchingException.class);
}
use of com.google.api.gax.rpc.testing.FakeBatchableApi.SquarerBatchingDescriptorV2 in project gax-java by googleapis.
the class BatcherImplTest method testExceptionInDescriptor.
/**
* Resolves future results when {@link BatchingDescriptor#splitResponse} throws exception.
*/
@Test
public void testExceptionInDescriptor() throws InterruptedException {
final RuntimeException fakeError = new RuntimeException("internal exception");
BatchingDescriptor<Integer, Integer, LabeledIntList, List<Integer>> descriptor = new SquarerBatchingDescriptorV2() {
@Override
public void splitResponse(List<Integer> batchResponse, List<BatchEntry<Integer, Integer>> batch) {
throw fakeError;
}
};
underTest = new BatcherImpl<>(descriptor, callLabeledIntSquarer, labeledIntList, batchingSettings, EXECUTOR);
Future<Integer> result = underTest.add(2);
underTest.flush();
Throwable actualError = null;
try {
result.get();
} catch (ExecutionException ex) {
actualError = ex;
}
assertThat(actualError).hasCauseThat().isSameInstanceAs(fakeError);
try {
underTest.close();
} catch (Exception batchingEx) {
actualError = batchingEx;
}
assertThat(actualError).isInstanceOf(BatchingException.class);
assertThat(actualError).hasMessageThat().contains("Batching finished with 1 batches failed to apply due to: 1 RuntimeException and 0 " + "partial failures.");
}
use of com.google.api.gax.rpc.testing.FakeBatchableApi.SquarerBatchingDescriptorV2 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.SquarerBatchingDescriptorV2 in project gax-java by googleapis.
the class BatcherImplTest method testPartialFailureWithSplitResponse.
/**
* To confirm the partial failures in Batching does not mark whole batch failed
*/
@Test
public void testPartialFailureWithSplitResponse() throws Exception {
SquarerBatchingDescriptorV2 descriptor = new SquarerBatchingDescriptorV2() {
@Override
public void splitResponse(List<Integer> batchResponse, List<BatchEntry<Integer, Integer>> batch) {
for (int i = 0; i < batchResponse.size(); i++) {
if (batchResponse.get(i) > 10_000) {
batch.get(i).getResultFuture().setException(new ArithmeticException());
} else {
batch.get(i).getResultFuture().set(batchResponse.get(i));
}
}
}
};
underTest = new BatcherImpl<>(descriptor, callLabeledIntSquarer, labeledIntList, batchingSettings, EXECUTOR);
underTest.add(10);
// This will cause partial failure
underTest.add(200);
underTest.flush();
underTest.add(40);
underTest.add(50);
underTest.flush();
// These will cause partial failure
underTest.add(500);
underTest.add(600);
Exception actualError = null;
try {
underTest.close();
} catch (Exception e) {
actualError = e;
}
assertThat(actualError).isInstanceOf(BatchingException.class);
assertThat(actualError).hasMessageThat().contains("Batching finished with 2 partial failures. The 2 partial failures contained " + "3 entries that failed with: 3 ArithmeticException.");
}
Aggregations