SummarizerJob implements TenantJob { @Override public Duration startIn() { final LocalDateTime date = DateUtil.localNow() .with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)) .withHour(6) .truncatedTo(ChronoUnit.HOURS); final Duration untilSunday6am = Duration.between(DateUtil.localNow(), date); final Duration untilNextSunday6am = untilSunday6am.isNegative() ? untilSunday6am.plusDays(7) : untilSunday6am; final int loadBalancingRandomMinutesOffset = RandomUtil.getNextInt(30) - 15; return untilNextSunday6am.plusMinutes(loadBalancingRandomMinutesOffset); }  @Override TenantPayload payload(); @Override Duration startIn(); @Override Duration killIn(); @Override Duration repeatEvery();  }
@Test void beforeSunday6am() { final Instant sundayEarlyMorning = LocalDateTime.of(2020, 1, 5, 5, 0) .atZone(Defaults.ZONE_ID) .toInstant(); setClock(Clock.fixed(sundayEarlyMorning, Defaults.ZONE_ID)); final Duration untilSundayAround6am = summarizer.startIn(); assertThat(untilSundayAround6am).isBetween(Duration.ofMinutes(-45), Duration.ofMinutes(75)); }
@Test void afterSunday6am() { final Instant sundayLaterMorning = LocalDateTime.of(2020, 1, 5, 7, 0) .atZone(Defaults.ZONE_ID) .toInstant(); setClock(Clock.fixed(sundayLaterMorning, Defaults.ZONE_ID)); final Duration untilNextSundayAround6am = summarizer.startIn(); assertThat(untilNextSundayAround6am) .isBetween(Duration.ofDays(7) .minusMinutes(75), Duration.ofDays(7) .minusMinutes(45)); }