@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);
    // Because starting scheduler takes some time and we don't know how long
    // the first time set is the offset on which we check the next values.
    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");
}