Search in sources :

Example 16 with LabeledIntList

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

Example 17 with LabeledIntList

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

Example 18 with LabeledIntList

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

Example 19 with LabeledIntList

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

Example 20 with LabeledIntList

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