private Refreshable<Long> getIncrementingLongRefreshableThrowingOnSpecificValue(long badValue) { AtomicLong atomicLong = new AtomicLong(); PollingRefreshable<Long> pollingRefreshable = PollingRefreshable.createWithSpecificPoller( () -> { long newValue = atomicLong.incrementAndGet(); if (newValue == badValue) { throw new RuntimeException(badValue + " is illegal"); } return newValue; }, REFRESH_INTERVAL, scheduler); return pollingRefreshable.getRefreshable(); } }
@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(); }
@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 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); }
type, RefreshableProxyInvocationHandler.create( configPollingRefreshable.getRefreshable(), serverListConfig -> { if (serverListConfig.hasAtLeastOneServer()) {
type, RefreshableProxyInvocationHandler.create( configPollingRefreshable.getRefreshable(), serverListConfig -> { if (serverListConfig.hasAtLeastOneServer()) {