@Test
public void testConnectResponseBeforeTimeout() throws Exception {

  long startTimeMs = SystemClock.elapsedRealtime();
  final ConditionVariable startCondition = buildUrlRequestStartedCondition();
  final CountDownLatch openLatch = new CountDownLatch(1);
  AtomicReference<Exception> exceptionOnTestThread = new AtomicReference<>();
  new Thread() {
    @Override
    public void run() {
      try {
        dataSourceUnderTest.open(testDataSpec);
      } catch (HttpDataSourceException e) {
        exceptionOnTestThread.set(e);
      } finally {
        openLatch.countDown();
      }
    }
  }.start();
  startCondition.block();
  // We should still be trying to open.
  assertNotCountedDown(openLatch);
  // We should still be trying to open as we approach the timeout.
  SystemClock.setCurrentTimeMillis(startTimeMs + TEST_CONNECT_TIMEOUT_MS - 1);
  assertNotCountedDown(openLatch);
  // The response arrives just in time.
  dataSourceUnderTest.urlRequestCallback.onResponseStarted(mockUrlRequest, testUrlResponseInfo);
  openLatch.await();
  assertThat(exceptionOnTestThread.get()).isNull();
}