@Test public void refreshableIsRepopulatedWithNewSupplierValuesAfterTheRefreshIntervalPasses() { AtomicLong atomicLong = new AtomicLong(); PollingRefreshable<Long> pollingRefreshable = createPollingRefreshableWithTestScheduler( atomicLong::incrementAndGet); Refreshable<Long> refreshable = pollingRefreshable.getRefreshable(); assertRefreshableContainsAndClear(refreshable, 1L); scheduler.tick(REFRESH_INTERVAL.toMillis() + 1, TimeUnit.MILLISECONDS); assertRefreshableContainsAndClear(refreshable, 2L); scheduler.tick(REFRESH_INTERVAL.toMillis() + 1, TimeUnit.MILLISECONDS); assertRefreshableContainsAndClear(refreshable, 3L); }
@Test public void refreshableIsPopulatedWithTheFreshestValueAfterTheRefreshIntervalPasses() { AtomicLong atomicLong = new AtomicLong(); PollingRefreshable<Long> pollingRefreshable = createPollingRefreshableWithTestScheduler(atomicLong::get); Refreshable<Long> refreshable = pollingRefreshable.getRefreshable(); assertRefreshableContainsAndClear(refreshable, 0L); atomicLong.set(FORTY_TWO + FORTY_TWO); scheduler.tick(1, TimeUnit.MILLISECONDS); atomicLong.set(FORTY_TWO); scheduler.tick(REFRESH_INTERVAL.toMillis() + 1, TimeUnit.MILLISECONDS); assertRefreshableContainsAndClear(refreshable, FORTY_TWO); }
@Test public void canRecoverFromSupplierThrowingExceptionsLater() { Refreshable<Long> refreshable = getIncrementingLongRefreshableThrowingOnSpecificValue(2L); assertRefreshableContainsAndClear(refreshable, 1L); // This execution will throw a RuntimeException. scheduler.tick(REFRESH_INTERVAL.toMillis() + 1, TimeUnit.MILLISECONDS); assertThat(refreshable.getAndClear()).isEmpty(); scheduler.tick(REFRESH_INTERVAL.toMillis() + 1, TimeUnit.MILLISECONDS); assertRefreshableContainsAndClear(refreshable, 3L); }
@Test public void refreshableIsNotRepopulatedWithNewSupplierValuesBeforeTheRefreshIntervalPasses() { AtomicLong atomicLong = new AtomicLong(); PollingRefreshable<Long> pollingRefreshable = createPollingRefreshableWithTestScheduler( atomicLong::incrementAndGet); Refreshable<Long> refreshable = pollingRefreshable.getRefreshable(); assertRefreshableContainsAndClear(refreshable, 1L); scheduler.tick(REFRESH_INTERVAL.toMillis() - 1, TimeUnit.MILLISECONDS); assertThat(refreshable.getAndClear()).isEmpty(); }
@Test public void canRecoverFromSupplierThrowingExceptionsInitially() { Refreshable<Long> refreshable = getIncrementingLongRefreshableThrowingOnSpecificValue(1L); assertThat(refreshable.getAndClear()).isEmpty(); scheduler.tick(REFRESH_INTERVAL.toMillis() + 1, TimeUnit.MILLISECONDS); assertRefreshableContainsAndClear(refreshable, 2L); }
@Test public void canConfigureRefreshInterval() { Duration doubleRefreshInterval = REFRESH_INTERVAL.multipliedBy(2L); AtomicLong atomicLong = new AtomicLong(); PollingRefreshable<Long> lessFrequentlyPollingRefreshable = PollingRefreshable.createWithSpecificPoller( atomicLong::incrementAndGet, doubleRefreshInterval, scheduler); Refreshable<Long> refreshable = lessFrequentlyPollingRefreshable.getRefreshable(); assertRefreshableContainsAndClear(refreshable, 1L); // This is only about half of our poller's refresh interval, so we shouldn't have polled yet scheduler.tick(REFRESH_INTERVAL.toMillis() + 1, TimeUnit.MILLISECONDS); assertThat(refreshable.getAndClear()).isEmpty(); // But now we cumulatively have ticked by 2*REFRESH_INTERVAL + 1, so we should have polled scheduler.tick(REFRESH_INTERVAL.toMillis(), TimeUnit.MILLISECONDS); assertRefreshableContainsAndClear(refreshable, 2L); }
@Test public void refreshableIsInitializedWithTheSupplierValue() { PollingRefreshable<Long> pollingRefreshable = createPollingRefreshableWithTestScheduler(() -> 1L); assertRefreshableContainsAndClear(pollingRefreshable.getRefreshable(), 1L); }
@Test public void refreshableIsNotRepopulatedWithStaleSupplierValuesEvenAfterTheRefreshInterval() { PollingRefreshable<Long> pollingRefreshable = createPollingRefreshableWithTestScheduler(() -> 1L); assertRefreshableContainsAndClear(pollingRefreshable.getRefreshable(), 1L); scheduler.tick(REFRESH_INTERVAL.toMillis() + 1, TimeUnit.MILLISECONDS); assertThat(pollingRefreshable.getRefreshable().getAndClear()).isNotPresent(); }