WorkerCounter implements Executor { public synchronized void afterPreviousWorkersFinished(ActorRef<WorkerListener> onFinished) { if (this.onFinished != null) { throw new IllegalStateException("a callback already exists; wait for the workers to finish before setting a new callback"); } this.onFinished = onFinished; if (activeWorkers.get() == 0) { fireAllWorkersFinished(); } } WorkerCounter(Executor realExecutor); @Override void execute(Runnable command); synchronized void afterPreviousWorkersFinished(ActorRef<WorkerListener> onFinished);  }
@Test(timeout = TIMEOUT) public void the_callback_is_fired_immediately_if_there_were_no_commands() throws InterruptedException { WorkerCounter counter = new WorkerCounter(realExecutor); counter.afterPreviousWorkersFinished(log("callback")); events.await(1, TIMEOUT); events.assertContains("callback"); }
@Test(timeout = TIMEOUT) public void a_new_callback_can_be_set_after_workers_are_finished() { WorkerCounter counter = new WorkerCounter(realExecutor); counter.afterPreviousWorkersFinished(log("callback 1")); counter.afterPreviousWorkersFinished(log("callback 2")); events.assertContains("callback 1", "callback 2"); }