PeriodicSchedulerImpl implements PeriodicScheduler { @Override public <T> ScheduledCompletableFuture<T> schedule(SchedulerRunnable runnable, Duration... delays) { return scheduler.schedule(runnable, new PeriodicAdjuster(delays)); } @Activate  PeriodicSchedulerImpl(final @Reference Scheduler scheduler); @Override ScheduledCompletableFuture<T> schedule(SchedulerRunnable runnable, Duration... delays);  }
@Test @Timeout(value = 5, unit = TimeUnit.SECONDS) public void testSchedule() throws InterruptedException, IOException { Queue<Long> times = new ArrayDeque<>(); Semaphore semaphore = new Semaphore(0); Duration[] delays = { Duration.ofMillis(100), Duration.ofMillis(200), Duration.ofMillis(300) }; final long now = System.currentTimeMillis(); ScheduledCompletableFuture<Object> future = periodicScheduler.schedule(() -> { times.add((System.currentTimeMillis() - now) / 100); semaphore.release(); }, delays); semaphore.acquire(6); future.cancel(true); long offset = times.poll(); long[] expectedResults = { 2, 5, 8, 11, 14 }; for (long expectedResult : expectedResults) { assertEquals(offset + expectedResult, times.poll().longValue(), "Expected periodic time"); } assertFalse(semaphore.tryAcquire(1, TimeUnit.SECONDS), "No more jobs should have been scheduled"); }