Scheduler { void schedule(TriggerTask task, ZonedDateTime time) { ZonedDateTime now = ZonedDateTime.now(clock); Duration delay = Duration.between(now, time); scheduler.schedule(() -> executeTask(task, time), delay.toNanos(), TimeUnit.NANOSECONDS); } Scheduler(LogManager logManager);   Scheduler(LogManager logManager, ScheduledExecutorService scheduler, ExecutorService jobExecutor); void start(); void shutdown(); void awaitTermination(long timeoutInMs); void addFixedRateTask(String name, Job job, Duration rate); void addTriggerTask(String name, Job job, Trigger trigger); void triggerNow(String name); final Map<String, Task> tasks; public Clock clock; }
@Test void scheduleTriggerTask() { ZonedDateTime now = ZonedDateTime.now(); scheduler.clock = Clock.fixed(now.toInstant(), ZoneId.systemDefault()); var task = new TriggerTask("trigger-job", null, previous -> previous.plusHours(1), ZoneId.systemDefault()); scheduler.schedule(task, now); ArgumentCaptor<Runnable> scheduledTask = ArgumentCaptor.forClass(Runnable.class); verify(schedulerExecutor).schedule(scheduledTask.capture(), eq(0L), eq(TimeUnit.NANOSECONDS)); scheduledTask.getValue().run(); verify(schedulerExecutor).schedule(scheduledTask.capture(), eq(Duration.ofHours(1).toNanos()), eq(TimeUnit.NANOSECONDS)); verify(jobExecutor).submit((Callable<?>) any(Callable.class)); }
@Test void scheduleFixedRateTask() { Duration rate = Duration.ofHours(1); scheduler.schedule(new FixedRateTask("hourly-job", null, rate)); ArgumentCaptor<Runnable> scheduledTask = ArgumentCaptor.forClass(Runnable.class); verify(schedulerExecutor).scheduleAtFixedRate(scheduledTask.capture(), anyLong(), eq(rate.toNanos()), eq(TimeUnit.NANOSECONDS)); scheduledTask.getValue().run(); verify(jobExecutor).submit((Callable<?>) any(Callable.class)); }