@Test
public void shouldTriggerBackgroundRefreshHalfWayThroughExpiration() throws Exception {

  KeysDownloader instance = newInstanceForTests();
  httpResponseBuilder =
      new HttpResponseBuilder().setContent("keys1").setCacheControlWithMaxAgeInSeconds(3L);
  // Fetched and cached keys
  assertEquals("keys1", instance.download());
  // Keys changed
  httpResponseBuilder = new HttpResponseBuilder().setContent("keys2");
  // 1.5 seconds later ...
  currentTimeInMillis += 1500L;
  // Old keys are sill returned, but a background fetch is initiated
  assertEquals("keys1", instance.download());
  // Wait background fetch to complete
  waitForLatch(backgroundFetchFinishedLatch);
  // 10ms later ...
  currentTimeInMillis += 10;
  // Keys changed again
  httpResponseBuilder = new HttpResponseBuilder().setContent("keys3");
  // Keys fetched in the background are used
  assertEquals("keys2", instance.download());
  // Single background fetch should have been triggered
  assertEquals(1, backgroundFetchStartedCount.get());
}