public static <T> PollingRefreshable<T> create(Supplier<T> supplier) { return create(supplier, DEFAULT_REFRESH_INTERVAL); }
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(); } }
@VisibleForTesting static <T> PollingRefreshable<T> createWithSpecificPoller( Supplier<T> supplier, Duration refreshInterval, ScheduledExecutorService poller) { PollingRefreshable<T> pollingRefreshable = new PollingRefreshable<>(supplier, refreshInterval, poller); pollingRefreshable.scheduleUpdates(); return pollingRefreshable; }
String userAgent) { PollingRefreshable<ServerListConfig> configPollingRefreshable = PollingRefreshable.create(serverListConfigSupplier); return Reflection.newProxy( type, RefreshableProxyInvocationHandler.create( configPollingRefreshable.getRefreshable(), serverListConfig -> { if (serverListConfig.hasAtLeastOneServer()) {
@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 shutsDownExecutorWhenClosed() { ScheduledExecutorService scheduledExecutor = mock(ScheduledExecutorService.class); PollingRefreshable<Long> pollingRefreshable = PollingRefreshable.createWithSpecificPoller( () -> 1L, REFRESH_INTERVAL, scheduledExecutor); pollingRefreshable.close(); verify(scheduledExecutor).shutdown(); }
private <T> PollingRefreshable<T> createPollingRefreshableWithTestScheduler(Supplier<T> supplier) { return PollingRefreshable.createWithSpecificPoller(supplier, REFRESH_INTERVAL, scheduler); }
String userAgent) { PollingRefreshable<ServerListConfig> configPollingRefreshable = PollingRefreshable.create(serverListConfigSupplier); return Reflection.newProxy( type, RefreshableProxyInvocationHandler.create( configPollingRefreshable.getRefreshable(), serverListConfig -> { if (serverListConfig.hasAtLeastOneServer()) {
@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); }
public static <T> PollingRefreshable<T> create(Supplier<T> supplier, Duration refreshInterval) { return createWithSpecificPoller(supplier, refreshInterval, PTExecutors.newSingleThreadScheduledExecutor(new NamedThreadFactory("polling-refreshable", true))); }
@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 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 cannotSetRefreshIntervalToZero() { assertThatThrownBy(() -> PollingRefreshable.create(() -> 1L, Duration.ZERO)) .isInstanceOf(IllegalArgumentException.class); }
@VisibleForTesting static <T> PollingRefreshable<T> createWithSpecificPoller( Supplier<T> supplier, Duration refreshInterval, ScheduledExecutorService poller) { PollingRefreshable<T> pollingRefreshable = new PollingRefreshable<>(supplier, refreshInterval, poller); pollingRefreshable.scheduleUpdates(); return pollingRefreshable; }
public static <T> PollingRefreshable<T> create(Supplier<T> supplier, Duration refreshInterval) { return createWithSpecificPoller(supplier, refreshInterval, PTExecutors.newSingleThreadScheduledExecutor(new NamedThreadFactory("polling-refreshable", true))); }
@Test public void refreshableIsInitializedWithTheSupplierValue() { PollingRefreshable<Long> pollingRefreshable = createPollingRefreshableWithTestScheduler(() -> 1L); assertRefreshableContainsAndClear(pollingRefreshable.getRefreshable(), 1L); }
@Test public void cannotSetNegativeRefreshInterval() { assertThatThrownBy(() -> PollingRefreshable.create(() -> 1L, Duration.of(-1, ChronoUnit.SECONDS))) .isInstanceOf(IllegalArgumentException.class); }
@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); }
public static <T> PollingRefreshable<T> create(Supplier<T> supplier) { return create(supplier, DEFAULT_REFRESH_INTERVAL); }