private <T> PollingRefreshable<T> createPollingRefreshableWithTestScheduler(Supplier<T> supplier) { return PollingRefreshable.createWithSpecificPoller(supplier, REFRESH_INTERVAL, scheduler); }
public static <T> PollingRefreshable<T> create(Supplier<T> supplier, Duration refreshInterval) { return createWithSpecificPoller(supplier, refreshInterval, PTExecutors.newSingleThreadScheduledExecutor(new NamedThreadFactory("polling-refreshable", true))); }
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 shutsDownExecutorWhenClosed() { ScheduledExecutorService scheduledExecutor = mock(ScheduledExecutorService.class); PollingRefreshable<Long> pollingRefreshable = PollingRefreshable.createWithSpecificPoller( () -> 1L, REFRESH_INTERVAL, scheduledExecutor); pollingRefreshable.close(); verify(scheduledExecutor).shutdown(); }
@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); }
public static <T> PollingRefreshable<T> create(Supplier<T> supplier, Duration refreshInterval) { return createWithSpecificPoller(supplier, refreshInterval, PTExecutors.newSingleThreadScheduledExecutor(new NamedThreadFactory("polling-refreshable", true))); }