batcherFactory); LabeledIntList request1 = new LabeledIntList("label", 2); ApiFuture<List<Integer>> future1 = batchingCallable.futureCall(request1, FakeCallContext.createDefault()); LabeledIntList request2 = new LabeledIntList("label", 3); ApiFuture<List<Integer>> future2 = batchingCallable.futureCall(request2, FakeCallContext.createDefault());
Truth.assertThat(trackedFlowController.getCallsToRelease()).isEqualTo(0); LabeledIntList requestA = new LabeledIntList("one", 1, 2); LabeledIntList requestB = new LabeledIntList("one", 3, 4);
@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 } } }
@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); } }
@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)); }
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)); }
@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)); }
@Test public void testBatchMergerImpl() { LabeledIntList request1 = new LabeledIntList("lable", 1); Batch<LabeledIntList, List<Integer>> batch1 = createBatch(request1); LabeledIntList request2 = new LabeledIntList("lable", 2); Batch<LabeledIntList, List<Integer>> batch2 = createBatch(request2); BatchMergerImpl<LabeledIntList, List<Integer>> batchMerger = new BatchMergerImpl<>(); batchMerger.merge(batch1, batch2); Truth.assertThat(batch1.getByteCount()).isEqualTo(3); }
@Test public void testBatchByteCounter() { ElementCounter<Batch<LabeledIntList, List<Integer>>> counter = new BatchByteCounter<>(); LabeledIntList request = new LabeledIntList("lable", 3); Batch<LabeledIntList, List<Integer>> batch = createBatch(request); Truth.assertThat(counter.count(batch)).isEqualTo(3); }
@Test public void testMerge() { LabeledIntList request1 = new LabeledIntList("lable", 1); Batch<LabeledIntList, List<Integer>> batch1 = createBatch(request1); LabeledIntList request2 = new LabeledIntList("lable", 2); Batch<LabeledIntList, List<Integer>> batch2 = createBatch(request2); batch1.merge(batch2); Truth.assertThat(batch1.getByteCount()).isEqualTo(3); }
@Test public void testConstructor() { LabeledIntList request = new LabeledIntList("lable", 2); Batch<LabeledIntList, List<Integer>> batch = createBatch(request); Truth.assertThat(batch.getRequest()).isEqualTo(request); Truth.assertThat(batch.getCallable()).isSameAs(batch.getCallable()); Truth.assertThat(batch.getRequestIssuerList().size()).isEqualTo(1); Truth.assertThat(batch.getByteCount()).isEqualTo(2); }
@Test public void testBatchElementCounter() { ElementCounter<Batch<LabeledIntList, List<Integer>>> counter = new BatchElementCounter<>(SQUARER_BATCHING_DESC); LabeledIntList request = new LabeledIntList("lable", 3); Batch<LabeledIntList, List<Integer>> batch = createBatch(request); Truth.assertThat(counter.count(batch)).isEqualTo(1); }
@Test public void testMergeStartEmpty() { LabeledIntList request1 = new LabeledIntList("lable", new ArrayList<Integer>()); Batch<LabeledIntList, List<Integer>> batch1 = createBatch(request1, null); Truth.assertThat(batch1.getCallable()).isNull(); LabeledIntList request2 = new LabeledIntList("lable", 2); Batch<LabeledIntList, List<Integer>> batch2 = createBatch(request2); batch1.merge(batch2); Truth.assertThat(batch1.getByteCount()).isEqualTo(2); Truth.assertThat(batch1.getCallable()).isNotNull(); Truth.assertThat(batch1.getCallable()).isSameAs(batch2.getCallable()); }