### Question:
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();  }### Answer:
@Test public void schedulesTheFirstExecutionAtTheRequestedHour() throws Exception { final ScheduledExecutorTester mock = new ScheduledExecutorTester(); final Instant two_oclock = Instant.parse("1970-01-01T14:00:00Z"); BackgroundRunner<String> instance = new BackgroundRunner<>(16, Clock.fixed(two_oclock, ZoneId.of("Z")), mock); instance.start(() -> ""); assertThat(mock.initialDelay, is(2L)); assertThat(mock.unit, is(TimeUnit.HOURS)); }
@Test public void schedulesTheFirstExecutionAtTheRequestedHourForNonUtc() throws Exception { final ScheduledExecutorTester mock = new ScheduledExecutorTester(); final Instant two_oclock_at_amsterdam = Instant.parse("1970-01-01T12:00:00Z"); BackgroundRunner<String> instance = new BackgroundRunner<>( 16, Clock.fixed(two_oclock_at_amsterdam, ZoneId.of("+0200")), mock ); instance.start(() -> ""); assertThat(mock.initialDelay, is(2L)); assertThat(mock.unit, is(TimeUnit.HOURS)); }
@Test public void skipsTheFirstExecutionWhenStartedInTheRequestedHour() throws Exception { final ScheduledExecutorTester mock = new ScheduledExecutorTester(); final Instant midnight = Instant.parse("1970-01-01T00:00:00Z"); BackgroundRunner<String> instance = new BackgroundRunner<>(24, Clock.fixed(midnight, ZoneId.of("Z")), mock); instance.start(() -> ""); assertThat(mock.initialDelay, is(24L)); }
@Test public void schedulesTheFirstExecutionAroundMidnightWhenRequestedJustBeforeMidnight() throws Exception { final ScheduledExecutorTester mock = new ScheduledExecutorTester(); final Instant midnight = Instant.parse("1970-01-01T23:59:59Z"); BackgroundRunner<String> instance = new BackgroundRunner<>(24, Clock.fixed(midnight, ZoneId.of("Z")), mock); instance.start(() -> ""); assertThat(mock.initialDelay, is(1L)); }
@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)); }
@Test public void schedulesSubsequentExecutions24HoursLater() throws Exception { final ScheduledExecutorTester mock = new ScheduledExecutorTester(); BackgroundRunner<String> instance = new BackgroundRunner<>( 16, Clock.fixed(Instant.ofEpochMilli(0), ZoneId.systemDefault()), mock ); instance.start(() -> ""); assertThat(mock.period, is(24L)); assertThat(mock.unit, is(TimeUnit.HOURS)); }