@Override public void run() { pushCurrentBatch(); } };
@Override public void run() { pushCurrentBatch(); } };
@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 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); }
@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); }
} catch (FlowControlException e) { batcher.pushCurrentBatch().get(); assertThat(receiver.getBatches()).hasSize(1); batcher.add(SimpleBatch.fromInteger(11)); batcher.add(SimpleBatch.fromInteger(13)); batcher.pushCurrentBatch().get();
batcher.pushCurrentBatch().get(); Assert.fail("expected exception"); } catch (Exception e) {
assertThat(receiver.getBatches()).hasSize(2); batcher.pushCurrentBatch().get();
.getBatcherFactory() .getPushingBatcher(SQUARER_BATCHING_DESC.getBatchPartitionKey(requestA)) .pushCurrentBatch() .get();