@Test public void testCallbackRefused() throws Exception { final List<Collection<String>> items = Lists.newArrayList(); Callback<Collection<String>> callback = new Callback<Collection<String>>() { @Override public void call(Collection<String> item) throws Exception { if (items.size() > 1) { throw new CallbackRefusedException(); } items.add(item); } }; try (BatchingCallback<String> batcher = BatchingCallback.<String>batchInto(2, callback)) { Callbacks.stream(batcher, "a", "b", "c", "d", "e", "f", "g"); } assertEquals(of( of("a", "b"), of("c", "d") ), items); } }
@Test(expected=BatchingCallbackExecutionException.class) public void testSingleExceptionFails() throws Exception { ExecutorService executor = Executors.newSingleThreadExecutor(); try { Callback<List<String>> out = new Callback<List<String>>() { @Override public void call(List<String> item) throws Exception { Thread.sleep(10); throw new IllegalStateException("boom!"); } }; BatchingCallback<String> batcher = BatchingCallback.batchInto(2, executor, out, true); batcher.call("a"); batcher.close(); } finally { executor.shutdownNow(); } } }
@Test public void testGood() throws Exception { ExecutorService executor = MoreExecutors.sameThreadExecutor(); CallbackCollector<List<String>> out = new CallbackCollector<>(); BatchingCallback<String> batcher = BatchingCallback.batchInto(2, executor, out, false); batcher.call("a"); batcher.call("b"); batcher.call("c"); batcher.call("d"); batcher.call("e"); batcher.commit(); assertEquals(of(of("a", "b"), of("c", "d"), of("e")), out.getItems()); }
@Test public void testBatchingCallback() throws Exception { CallbackCollector<List<String>> collector = new CallbackCollector<>(); try (BatchingCallback<String> batcher = BatchingCallback.batchInto(2, collector)) { batcher.call("a"); batcher.call("b"); batcher.call("c"); batcher.commit(); batcher.call("d"); batcher.call("e"); batcher.call("f"); } assertEquals(of( of("a", "b"), of("c"), of("d", "e"), of("f") ), collector.getItems()); }
BatchingCallback<String> batcher = BatchingCallback.batchInto(2, executor, out, true);
BatchingCallback<String> batcher = BatchingCallback.batchInto(2, executor, out, false);