CronSchedulerImpl implements CronScheduler { @Override public ScheduledCompletableFuture<@Nullable Void> schedule(SchedulerRunnable runnable, String cronExpression) { return schedule(d -> runnable.run(), Collections.emptyMap(), cronExpression); } @Activate  CronSchedulerImpl(final @Reference Scheduler scheduler); @Override ScheduledCompletableFuture<@Nullable Void> schedule(SchedulerRunnable runnable, String cronExpression); @Override ScheduledCompletableFuture<@Nullable Void> schedule(CronJob job, Map<String, Object> config,
            String cronExpression);  }
@Test @Timeout(value = 1, unit = TimeUnit.SECONDS) public void testCronReboot() throws Exception { long now = System.currentTimeMillis(); Semaphore s = new Semaphore(0); ScheduledCompletableFuture<Void> future = cronScheduler.schedule(() -> { }, "@reboot"); future.getPromise().thenAccept(x -> s.release()); s.acquire(1); long diff = System.currentTimeMillis() - now; assertTrue(diff < 200, "Time difference should be less 200 but was: " + diff); assertTrue(future.isDone(), "Scheduler should be done once reboot call done."); }
@Test @Timeout(value = 6, unit = TimeUnit.SECONDS) public void testCronScheduling() throws Exception { long now = System.currentTimeMillis(); AtomicReference<Object> ref = new AtomicReference<>(); Semaphore s = new Semaphore(0); cronScheduler.schedule(foo -> { s.release(); ref.set(foo.get("foo")); }, Map.of("foo", "bar"), "#\n" + "\n" + " foo = bar \n" + "# bla bla foo=foo\n" + "0/2 * * * * *"); s.acquire(2); long diff = (System.currentTimeMillis() - now + 50) / 1000; assertTrue(diff >= 3 && diff <= 4, "Difference calculation should be between 3 and 4 but was: " + diff); assertEquals("bar", ref.get(), "Environment variable 'foo' should be correctly set"); }