Search in sources :

Example 11 with LabeledIntList

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.");
}
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 12 with LabeledIntList

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");
}
Also used : UnaryCallable(com.google.api.gax.rpc.UnaryCallable) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) TimeoutException(java.util.concurrent.TimeoutException) FlowControlRuntimeException(com.google.api.gax.batching.FlowController.FlowControlRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) Test(org.junit.Test)

Example 13 with LabeledIntList

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);
}
Also used : RetrySettings(com.google.api.gax.retrying.RetrySettings) LabeledIntList(com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList) List(java.util.List) LabeledIntList(com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList) StatusCode(com.google.api.gax.rpc.StatusCode) Test(org.junit.Test)

Example 14 with LabeledIntList

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);
}
Also used : FlowControlSettings(com.google.api.gax.batching.FlowControlSettings) SquarerBatchingDescriptor(com.google.api.gax.rpc.testing.FakeBatchableApi.SquarerBatchingDescriptor) LabeledIntList(com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList) List(java.util.List) LabeledIntList(com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList) FlowController(com.google.api.gax.batching.FlowController) BatchingSettings(com.google.api.gax.batching.BatchingSettings) Test(org.junit.Test)

Example 15 with LabeledIntList

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);
}
Also used : FlowControlSettings(com.google.api.gax.batching.FlowControlSettings) SquarerBatchingDescriptor(com.google.api.gax.rpc.testing.FakeBatchableApi.SquarerBatchingDescriptor) LabeledIntList(com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList) List(java.util.List) LabeledIntList(com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList) FlowController(com.google.api.gax.batching.FlowController) BatchingSettings(com.google.api.gax.batching.BatchingSettings) Test(org.junit.Test)

Aggregations

LabeledIntList (com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList)27 List (java.util.List)27 Test (org.junit.Test)27 ArrayList (java.util.ArrayList)16 ImmutableList (com.google.common.collect.ImmutableList)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 BatchingSettings (com.google.api.gax.batching.BatchingSettings)8 FlowControlRuntimeException (com.google.api.gax.batching.FlowController.FlowControlRuntimeException)6 ExecutionException (java.util.concurrent.ExecutionException)6 TimeoutException (java.util.concurrent.TimeoutException)6 ApiCallContext (com.google.api.gax.rpc.ApiCallContext)5 UnaryCallable (com.google.api.gax.rpc.UnaryCallable)4 SquarerBatchingDescriptorV2 (com.google.api.gax.rpc.testing.FakeBatchableApi.SquarerBatchingDescriptorV2)4 FlowControlSettings (com.google.api.gax.batching.FlowControlSettings)3 FlowController (com.google.api.gax.batching.FlowController)3 RetrySettings (com.google.api.gax.retrying.RetrySettings)3 SquarerBatchingDescriptor (com.google.api.gax.rpc.testing.FakeBatchableApi.SquarerBatchingDescriptor)3 StatusCode (com.google.api.gax.rpc.StatusCode)2 LabeledIntSquarerCallable (com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntSquarerCallable)2 ExecutorService (java.util.concurrent.ExecutorService)2