/** * Alternate method of committing, for use with {@code try-with-resources}. */ @Override public void close() { commit(); }
@Override public boolean commit() { boolean result = super.commit(); ExecutorCallback.class.cast(getOut()).close(); return result; }
@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()); }
@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()); }
batcher.commit(); fail(); } catch (BatchingCallbackExecutionException bcee) {