@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 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 refreshableIsInitializedWithTheSupplierValue() { PollingRefreshable<Long> pollingRefreshable = createPollingRefreshableWithTestScheduler(() -> 1L); assertRefreshableContainsAndClear(pollingRefreshable.getRefreshable(), 1L); }
@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 refreshableIsNotRepopulatedWithStaleSupplierValuesEvenAfterTheRefreshInterval() { PollingRefreshable<Long> pollingRefreshable = createPollingRefreshableWithTestScheduler(() -> 1L); assertRefreshableContainsAndClear(pollingRefreshable.getRefreshable(), 1L); scheduler.tick(REFRESH_INTERVAL.toMillis() + 1, TimeUnit.MILLISECONDS); assertThat(pollingRefreshable.getRefreshable().getAndClear()).isNotPresent(); }