@Test public void shouldNotTriggerBackgroundRefreshBeforeHalfWayThroughExpiration() throws Exception { KeysDownloader instance = newInstanceForTests(); httpResponseBuilder = new HttpResponseBuilder().setContent("keys1").setCacheControlWithMaxAgeInSeconds(3L); assertEquals("keys1", instance.download()); httpResponseBuilder = new HttpResponseBuilder().setContent("keys2"); currentTimeInMillis += 1500L - 1; assertEquals("keys1", instance.download()); assertEquals(0, backgroundFetchStartedCount.get()); }
public String download() throws IOException { synchronized (instanceStateLock) { if (hasNonExpiredDataCached()) { if (shouldProactivelyRefreshDataInBackground()) { refreshInBackground(); } return cachedData; } } synchronized (fetchDataLock) { synchronized (instanceStateLock) { if (hasNonExpiredDataCached()) { return cachedData; } } return fetchAndCacheData(); } }
KeysDownloader { public String download() throws IOException { synchronized (instanceStateLock) { if (hasNonExpiredDataCached()) { if (shouldProactivelyRefreshDataInBackground()) { refreshInBackground(); } return cachedData; } } synchronized (fetchDataLock) { synchronized (instanceStateLock) { if (hasNonExpiredDataCached()) { return cachedData; } } return fetchAndCacheData(); } } }
KeysDownloader { public String download() throws IOException { synchronized (instanceStateLock) { if (hasNonExpiredDataCached()) { if (shouldProactivelyRefreshDataInBackground()) { refreshInBackground(); } return cachedData; } } synchronized (fetchDataLock) { synchronized (instanceStateLock) { if (hasNonExpiredDataCached()) { return cachedData; } } return fetchAndCacheData(); } } KeysDownloader(Executor backgroundExecutor, HttpTransport httpTransport, String url); }
KeysDownloader { public String download() throws IOException { synchronized (instanceStateLock) { if (hasNonExpiredDataCached()) { if (shouldProactivelyRefreshDataInBackground()) { refreshInBackground(); } return cachedData; } } synchronized (fetchDataLock) { synchronized (instanceStateLock) { if (hasNonExpiredDataCached()) { return cachedData; } } return fetchAndCacheData(); } } KeysDownloader(Executor backgroundExecutor, HttpTransport httpTransport, String url); String download(); HttpTransport getHttpTransport(); String getUrl(); void refreshInBackground(); }
KeysDownloader { public String download() throws IOException { synchronized (instanceStateLock) { if (hasNonExpiredDataCached()) { if (shouldProactivelyRefreshDataInBackground()) { refreshInBackground(); } return cachedData; } } synchronized (fetchDataLock) { synchronized (instanceStateLock) { if (hasNonExpiredDataCached()) { return cachedData; } } return fetchAndCacheData(); } } KeysDownloader(Executor backgroundExecutor, HttpTransport httpTransport, String url); String download(); HttpTransport getHttpTransport(); String getUrl(); void refreshInBackground();  }