@Test public void handlesScheduledAtLowerNumbersThanCurrentTime() throws Exception { final ScheduledExecutorTester mock = new ScheduledExecutorTester(); final Instant midnight = Instant.parse("1970-01-01T14:00:00Z"); BackgroundRunner<String> instance = new BackgroundRunner<>(2, Clock.fixed(midnight, ZoneId.of("Z")), mock); instance.start(() -> ""); assertThat(mock.initialDelay, is(12L)); }
public void start(Producer<E> task) { previousResult = task.call(); final int currentHour = LocalDateTime.ofInstant(clock.instant(), clock.getZone()).getHour(); final int initialDelay; if (currentHour > hourToRunAt) { initialDelay = 24 - currentHour + hourToRunAt; } else { initialDelay = hourToRunAt - currentHour; } healthchecksExecutor.scheduleAtFixedRate(() -> previousResult = task.call(), initialDelay, 24, TimeUnit.HOURS); }
BackgroundRunner { public void start(Producer<E> task) { previousResult = task.call(); final int currentHour = LocalDateTime.ofInstant(clock.instant(), clock.getZone()).getHour(); final int initialDelay; if (currentHour > hourToRunAt) { initialDelay = 24 - currentHour + hourToRunAt; } else { initialDelay = hourToRunAt - currentHour; } healthchecksExecutor.scheduleAtFixedRate(() -> previousResult = task.call(), initialDelay, 24, TimeUnit.HOURS); } }
BackgroundRunner { public void start(Producer<E> task) { previousResult = task.call(); final int currentHour = LocalDateTime.ofInstant(clock.instant(), clock.getZone()).getHour(); final int initialDelay; if (currentHour > hourToRunAt) { initialDelay = 24 - currentHour + hourToRunAt; } else { initialDelay = hourToRunAt - currentHour; } healthchecksExecutor.scheduleAtFixedRate(() -> previousResult = task.call(), initialDelay, 24, TimeUnit.HOURS); } BackgroundRunner(int hourToRunAt, Clock clock, ScheduledExecutorService healthchecksExecutor); }
BackgroundRunner { public void start(Producer<E> task) { previousResult = task.call(); final int currentHour = LocalDateTime.ofInstant(clock.instant(), clock.getZone()).getHour(); final int initialDelay; if (currentHour > hourToRunAt) { initialDelay = 24 - currentHour + hourToRunAt; } else { initialDelay = hourToRunAt - currentHour; } healthchecksExecutor.scheduleAtFixedRate(() -> previousResult = task.call(), initialDelay, 24, TimeUnit.HOURS); } BackgroundRunner(int hourToRunAt, Clock clock, ScheduledExecutorService healthchecksExecutor); void start(Producer<E> task); Optional<E> getMostRecentResult(); }
BackgroundRunner { public void start(Producer<E> task) { previousResult = task.call(); final int currentHour = LocalDateTime.ofInstant(clock.instant(), clock.getZone()).getHour(); final int initialDelay; if (currentHour > hourToRunAt) { initialDelay = 24 - currentHour + hourToRunAt; } else { initialDelay = hourToRunAt - currentHour; } healthchecksExecutor.scheduleAtFixedRate(() -> previousResult = task.call(), initialDelay, 24, TimeUnit.HOURS); } BackgroundRunner(int hourToRunAt, Clock clock, ScheduledExecutorService healthchecksExecutor); void start(Producer<E> task); Optional<E> getMostRecentResult();  }