BatcherImpl implements Batcher<ElementT, ElementResultT> { @Override public ApiFuture<ElementResultT> add(ElementT element) { Preconditions.checkState(!isClosed, "Cannot add elements on a closed batcher"); SettableApiFuture<ElementResultT> result = SettableApiFuture.create(); synchronized (elementLock) { currentOpenBatch.add(element, result); } if (currentOpenBatch.hasAnyThresholdReached()) { sendOutstanding(); } return result; } BatcherImpl(
      BatchingDescriptor<ElementT, ElementResultT, RequestT, ResponseT> batchingDescriptor,
      UnaryCallable<RequestT, ResponseT> unaryCallable,
      RequestT prototype,
      BatchingSettings batchingSettings,
      ScheduledExecutorService executor); @Override ApiFuture<ElementResultT> add(ElementT element); @Override void flush(); @Override void sendOutstanding(); @Override void close();  }
@Test public void testWhenBatcherIsClose() throws Exception { Future<Integer> result; try (Batcher<Integer, Integer> batcher = new BatcherImpl<>( SQUARER_BATCHING_DESC_V2, callLabeledIntSquarer, labeledIntList, batchingSettings, EXECUTOR)) { result = batcher.add(5); } assertThat(result.isDone()).isTrue(); assertThat(result.get()).isEqualTo(25); }
@Test public void testWhenThresholdIsDisabled() throws Exception { BatchingSettings settings = BatchingSettings.newBuilder() .setElementCountThreshold(null) .setRequestByteThreshold(null) .setDelayThreshold(null) .build(); underTest = new BatcherImpl<>( SQUARER_BATCHING_DESC_V2, callLabeledIntSquarer, labeledIntList, settings, EXECUTOR); Future<Integer> result = underTest.add(2); assertThat(result.isDone()).isTrue(); assertThat(result.get()).isEqualTo(4); }
@Test public void testWhenDelayThresholdExceeds() throws Exception { BatchingSettings settings = batchingSettings.toBuilder().setDelayThreshold(Duration.ofMillis(100)).build(); underTest = new BatcherImpl<>( SQUARER_BATCHING_DESC_V2, callLabeledIntSquarer, labeledIntList, settings, EXECUTOR); Future<Integer> result = underTest.add(6); assertThat(result.isDone()).isFalse(); assertThat(result.get()).isEqualTo(36); }