ThresholdBatcher<SimpleBatch> batcher = createSimpleBatcherBuidler(receiver) .setThresholds(BatchingThresholds.<SimpleBatch>create(4)) .setFlowController( getTrackedIntegerBatchingFlowController( 3L, null, LimitExceededBehavior.ThrowException)) .build();
@Test public void testBatchingWithDelay() throws Exception { AccumulatingBatchReceiver<SimpleBatch> receiver = new AccumulatingBatchReceiver<>(ApiFutures.<Void>immediateFuture(null)); ThresholdBatcher<SimpleBatch> batcher = createSimpleBatcherBuidler(receiver).setMaxDelay(Duration.ofMillis(100)).build(); batcher.add(SimpleBatch.fromInteger(3)); batcher.add(SimpleBatch.fromInteger(5)); // Give time for the delay to trigger and push the batch Thread.sleep(500); assertThat(receiver.getBatches()).hasSize(1); batcher.add(SimpleBatch.fromInteger(11)); batcher.pushCurrentBatch().get(); List<List<Integer>> expected = Arrays.asList(Arrays.asList(3, 5), Arrays.asList(11)); List<List<Integer>> actual = new ArrayList<>(); for (SimpleBatch batch : receiver.getBatches()) { actual.add(batch.getIntegers()); } assertThat(actual).isEqualTo(expected); }
ThresholdBatcher<SimpleBatch> batcher = createSimpleBatcherBuidler(receiver) .setThresholds(BatchingThresholds.<SimpleBatch>create(4)) .setFlowController( getTrackedIntegerBatchingFlowController( 3L, null, LimitExceededBehavior.ThrowException)) .build();
ThresholdBatcher<SimpleBatch> batcher = createSimpleBatcherBuidler(receiver) .setThresholds(BatchingThresholds.<SimpleBatch>create(2)) .setFlowController( getTrackedIntegerBatchingFlowController(2L, null, LimitExceededBehavior.Block)) .build();
@Test public void testBatching() throws Exception { AccumulatingBatchReceiver<SimpleBatch> receiver = new AccumulatingBatchReceiver<>(ApiFutures.<Void>immediateFuture(null)); ThresholdBatcher<SimpleBatch> batcher = createSimpleBatcherBuidler(receiver) .setThresholds(BatchingThresholds.<SimpleBatch>create(2)) .build(); batcher.add(SimpleBatch.fromInteger(3)); batcher.add(SimpleBatch.fromInteger(5)); // Give time for the executor to push the batch Thread.sleep(100); assertThat(receiver.getBatches()).hasSize(1); batcher.add(SimpleBatch.fromInteger(7)); batcher.add(SimpleBatch.fromInteger(9)); // Give time for the executor to push the batch Thread.sleep(100); assertThat(receiver.getBatches()).hasSize(2); batcher.add(SimpleBatch.fromInteger(11)); batcher.pushCurrentBatch().get(); List<List<Integer>> expected = Arrays.asList(Arrays.asList(3, 5), Arrays.asList(7, 9), Arrays.asList(11)); List<List<Integer>> actual = new ArrayList<>(); for (SimpleBatch batch : receiver.getBatches()) { actual.add(batch.getIntegers()); } assertThat(actual).isEqualTo(expected); }
private ThresholdBatcher<Batch<RequestT, ResponseT>> createBatcher(PartitionKey partitionKey) { BatchExecutor<RequestT, ResponseT> processor = new BatchExecutor<>(batchingDescriptor, partitionKey); BatchingFlowController<Batch<RequestT, ResponseT>> batchingFlowController = new BatchingFlowController<>( flowController, new BatchElementCounter<>(batchingDescriptor), new BatchByteCounter<RequestT, ResponseT>()); return ThresholdBatcher.<Batch<RequestT, ResponseT>>newBuilder() .setThresholds(getThresholds(batchingSettings)) .setExecutor(executor) .setMaxDelay(batchingSettings.getDelayThreshold()) .setReceiver(processor) .setFlowController(batchingFlowController) .setBatchMerger(new BatchMergerImpl<RequestT, ResponseT>()) .build(); }
/** Get a new builder for a ThresholdBatcher. */ public static <E> Builder<E> newBuilder() { return new Builder<>(); }
private ThresholdBatcher<Batch<RequestT, ResponseT>> createBatcher(PartitionKey partitionKey) { BatchExecutor<RequestT, ResponseT> processor = new BatchExecutor<>(batchingDescriptor, partitionKey); BatchingFlowController<Batch<RequestT, ResponseT>> batchingFlowController = new BatchingFlowController<>( flowController, new BatchElementCounter<>(batchingDescriptor), new BatchByteCounter<RequestT, ResponseT>()); return ThresholdBatcher.<Batch<RequestT, ResponseT>>newBuilder() .setThresholds(getThresholds(batchingSettings)) .setExecutor(executor) .setMaxDelay(batchingSettings.getDelayThreshold()) .setReceiver(processor) .setFlowController(batchingFlowController) .setBatchMerger(new BatchMergerImpl<RequestT, ResponseT>()) .build(); }
/** Get a new builder for a ThresholdBatcher. */ public static <E> Builder<E> newBuilder() { return new Builder<>(); }
@Test public void testAdd() throws Exception { AccumulatingBatchReceiver<SimpleBatch> receiver = new AccumulatingBatchReceiver<>(ApiFutures.<Void>immediateFuture(null)); ThresholdBatcher<SimpleBatch> batcher = createSimpleBatcherBuidler(receiver).build(); batcher.add(SimpleBatch.fromInteger(14)); assertThat(batcher.isEmpty()).isFalse(); assertThat(receiver.getBatches()).hasSize(0); batcher.pushCurrentBatch().get(); assertThat(batcher.isEmpty()).isTrue(); assertThat(receiver.getBatches()).hasSize(1); assertThat(receiver.getBatches().get(0).getIntegers()).isEqualTo(Arrays.asList(14)); }
@Test public void testExceptionWithNullFlowController() { thrown.expect(NullPointerException.class); ThresholdBatcher.<SimpleBatch>newBuilder() .setThresholds(BatchingThresholds.<SimpleBatch>create(100)) .setExecutor(EXECUTOR) .setMaxDelay(Duration.ofMillis(10000)) .setReceiver( new AccumulatingBatchReceiver<SimpleBatch>(ApiFutures.<Void>immediateFuture(null))) .setBatchMerger(new SimpleBatchMerger()) .build(); }
private static ThresholdBatcher.Builder<SimpleBatch> createSimpleBatcherBuidler( AccumulatingBatchReceiver<SimpleBatch> receiver) { return ThresholdBatcher.<SimpleBatch>newBuilder() .setThresholds(BatchingThresholds.<SimpleBatch>create(100)) .setExecutor(EXECUTOR) .setMaxDelay(Duration.ofMillis(10000)) .setReceiver(receiver) .setFlowController(ThresholdBatcherTest.<SimpleBatch>getDisabledBatchingFlowController()) .setBatchMerger(new SimpleBatchMerger()); }