@Test public void updateNextFixedDelay() { ManualClock clock = new ManualClock(); Registry registry = new DefaultRegistry(clock); Counter skipped = registry.counter("skipped"); Scheduler.Options options = new Scheduler.Options() .withFrequency(Scheduler.Policy.FIXED_DELAY, Duration.ofSeconds(10)); clock.setWallTime(5437L); Scheduler.DelayedTask task = new Scheduler.DelayedTask(clock, options, () -> {}); Assertions.assertEquals(5437L, task.getNextExecutionTime()); Assertions.assertEquals(0L, skipped.count()); clock.setWallTime(12123L); task.updateNextExecutionTime(skipped); Assertions.assertEquals(22123L, task.getNextExecutionTime()); Assertions.assertEquals(0L, skipped.count()); clock.setWallTime(27000L); task.updateNextExecutionTime(skipped); Assertions.assertEquals(37000L, task.getNextExecutionTime()); Assertions.assertEquals(0L, skipped.count()); }
@Test public void updateNextFixedRateSkip() { ManualClock clock = new ManualClock(); Registry registry = new DefaultRegistry(clock); Counter skipped = registry.counter("skipped"); Scheduler.Options options = new Scheduler.Options() .withFrequency(Scheduler.Policy.FIXED_RATE_SKIP_IF_LONG, Duration.ofSeconds(10)); clock.setWallTime(5437L); Scheduler.DelayedTask task = new Scheduler.DelayedTask(clock, options, () -> {}); Assertions.assertEquals(5437L, task.getNextExecutionTime()); Assertions.assertEquals(0L, skipped.count()); clock.setWallTime(12123L); task.updateNextExecutionTime(skipped); Assertions.assertEquals(15437L, task.getNextExecutionTime()); Assertions.assertEquals(0L, skipped.count()); clock.setWallTime(27000L); task.updateNextExecutionTime(skipped); Assertions.assertEquals(35437L, task.getNextExecutionTime()); Assertions.assertEquals(1L, skipped.count()); clock.setWallTime(57000L); task.updateNextExecutionTime(skipped); Assertions.assertEquals(65437L, task.getNextExecutionTime()); Assertions.assertEquals(3L, skipped.count()); }
/** * Schedule a repetitive task. * * @param options * Options for controlling the execution of the task. See {@link Options} * for more information. * @param task * Task to execute. * @return * Future that can be used for cancelling the current and future executions of * the task. There is no value associated with the task so the future is just for * checking if it is still running to stopping it from running in the future. */ public ScheduledFuture<?> schedule(Options options, Runnable task) { if (!started) { startThreads(); } DelayedTask t = new DelayedTask(clock, options, task); queue.put(t); return t; }
/** * Schedule a repetitive task. * * @param options * Options for controlling the execution of the task. See {@link Options} * for more information. * @param task * Task to execute. * @return * Future that can be used for cancelling the current and future executions of * the task. There is no value associated with the task so the future is just for * checking if it is still running to stopping it from running in the future. */ public ScheduledFuture<?> schedule(Options options, Runnable task) { if (!started) { startThreads(); } DelayedTask t = new DelayedTask(clock, options, task); queue.put(t); return t; }