public static void awaitTermination(Service service, long timeout) { long begin = System.currentTimeMillis(); while (true) { State state = service.state(); if (state == State.TERMINATED) { break; } if (state == State.FAILED) { throw new IllegalStateException("Service '" + service + "' execution unexpectedly failed"); } TimeUtils.sleepMillis(500); long now = System.currentTimeMillis(); long diff = now - begin; if (diff > timeout) { throw new RuntimeException( String.format("Waiting for service %s is failed. Timeout %d", service, diff)); } } }