SystemTimer { public void schedule(SystemTimerTask task, long delay) { checkCancelled(); timer.schedule(task, delay); } SystemTimer(DistributedSystem distributedSystem); @Override String toString(); static int distributedSystemCount(); static void cancelTimers(DistributedSystem system); int timerPurge(); void schedule(SystemTimerTask task, long delay); void schedule(SystemTimerTask task, Date time); void scheduleAtFixedRate(SystemTimerTask task, long delay, long period); void schedule(SystemTimerTask task, long delay, long period); void cancel(); boolean isCancelled();  }
@Test public void scheduleNow() { AtomicBoolean hasRun = new AtomicBoolean(false); SystemTimer.SystemTimerTask task = new SystemTimer.SystemTimerTask() { @Override public void run2() { hasRun.set(true); } }; systemTimer.schedule(task, 0); await().until(() -> hasRun.get()); }
@Test public void scheduleWithDelay() { AtomicBoolean hasRun = new AtomicBoolean(false); SystemTimer.SystemTimerTask task = new SystemTimer.SystemTimerTask() { @Override public void run2() { hasRun.set(true); } }; final long millis = System.currentTimeMillis(); final int delay = 1000; systemTimer.schedule(task, delay); await().until(() -> hasRun.get()); assertThat(System.currentTimeMillis()).isGreaterThanOrEqualTo(millis + delay); }
@Test public void scheduleWithDate() { AtomicBoolean hasRun = new AtomicBoolean(false); SystemTimer.SystemTimerTask task = new SystemTimer.SystemTimerTask() { @Override public void run2() { hasRun.set(true); } }; final long millis = System.currentTimeMillis(); final long delay = 1000; final Date scheduleTime = new Date(System.currentTimeMillis() + delay); systemTimer.schedule(task, scheduleTime); await().until(() -> hasRun.get()); assertThat(System.currentTimeMillis()).isGreaterThanOrEqualTo(millis + delay); }
@Test public void scheduleRepeatedWithDelay() { AtomicInteger invocations = new AtomicInteger(0); SystemTimer.SystemTimerTask task = new SystemTimer.SystemTimerTask() { @Override public void run2() { invocations.incrementAndGet(); } }; final long millis = System.currentTimeMillis(); final int delay = 1000; final int period = 500; systemTimer.schedule(task, delay, period); await().untilAsserted(() -> assertThat(invocations.get()).isGreaterThanOrEqualTo(2)); assertThat(System.currentTimeMillis()).isGreaterThanOrEqualTo(millis + delay + period); }