ReScheduleSupport { static long getInitialDelay(long stepSize, Clock clock) { long now = clock.wallTime(); long stepStart = now / stepSize * stepSize; long offset = stepSize / 10; long delay = now - stepStart; if (delay < offset) { return delay + offset; } else if (delay > stepSize - offset) { return now - stepStart - offset; } else { return delay; } } ReScheduleSupport(ScheduledService scheduler, LookoutConfig config, Clock clock); synchronized void reschedulePoll(Function<MetricFilter, Object> function); TaskResult schedulePoll(final PRIORITY priority, final long stepMillis,
                                   final MetricFilter metricFilter,
                                   final Function<MetricFilter, Object> function);  }
@Test public void testInitDelay() { int stepSize = 60000; long now = 1522675416000l; long min = now / stepSize * stepSize; long val = ReScheduleSupport.getInitialDelay(60000, new ManualClock(now, 1l)); Assert.assertEquals(now - min, val); System.out.println("=============================too small==================="); now = 1522675385000l; min = now / stepSize * stepSize; System.out.println(min + "," + (min + stepSize)); val = ReScheduleSupport.getInitialDelay(60000, new ManualClock(now, 1l)); Assert.assertEquals(now - min + stepSize / 10, val); System.out.println("=============================too big==================="); now = 1522675440000l - 3000l; min = now / stepSize * stepSize; long max = min + stepSize; System.out.println(min + "," + max); val = ReScheduleSupport.getInitialDelay(60000, new ManualClock(now, 1l)); Assert.assertEquals(stepSize - (max - now + stepSize / 10), val); }