Search in sources :

Example 1 with SquarerBatchingDescriptorV2

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);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SquarerBatchingDescriptorV2(com.google.api.gax.rpc.testing.FakeBatchableApi.SquarerBatchingDescriptorV2) FlowControlRuntimeException(com.google.api.gax.batching.FlowController.FlowControlRuntimeException) LabeledIntList(com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LabeledIntList(com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) FlowControlRuntimeException(com.google.api.gax.batching.FlowController.FlowControlRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 2 with SquarerBatchingDescriptorV2

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.");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SquarerBatchingDescriptorV2(com.google.api.gax.rpc.testing.FakeBatchableApi.SquarerBatchingDescriptorV2) FlowControlRuntimeException(com.google.api.gax.batching.FlowController.FlowControlRuntimeException) LabeledIntList(com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LabeledIntList(com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) FlowControlRuntimeException(com.google.api.gax.batching.FlowController.FlowControlRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 3 with SquarerBatchingDescriptorV2

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.");
}
Also used : SquarerBatchingDescriptorV2(com.google.api.gax.rpc.testing.FakeBatchableApi.SquarerBatchingDescriptorV2) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FlowControlRuntimeException(com.google.api.gax.batching.FlowController.FlowControlRuntimeException) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LabeledIntList(com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList) TimeoutException(java.util.concurrent.TimeoutException) FlowControlRuntimeException(com.google.api.gax.batching.FlowController.FlowControlRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 4 with SquarerBatchingDescriptorV2

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.");
}
Also used : SquarerBatchingDescriptorV2(com.google.api.gax.rpc.testing.FakeBatchableApi.SquarerBatchingDescriptorV2) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LabeledIntList(com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList) TimeoutException(java.util.concurrent.TimeoutException) FlowControlRuntimeException(com.google.api.gax.batching.FlowController.FlowControlRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

FlowControlRuntimeException (com.google.api.gax.batching.FlowController.FlowControlRuntimeException)4 LabeledIntList (com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList)4 SquarerBatchingDescriptorV2 (com.google.api.gax.rpc.testing.FakeBatchableApi.SquarerBatchingDescriptorV2)4 ImmutableList (com.google.common.collect.ImmutableList)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ExecutionException (java.util.concurrent.ExecutionException)4 TimeoutException (java.util.concurrent.TimeoutException)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Test (org.junit.Test)4