@Test
public void testConnectTimeout() throws InterruptedException {

  long startTimeMs = SystemClock.elapsedRealtime();
  final ConditionVariable startCondition = buildUrlRequestStartedCondition();
  final CountDownLatch timedOutLatch = new CountDownLatch(1);
  new Thread() {
    @Override
    public void run() {
      try {
        dataSourceUnderTest.open(testDataSpec);
        fail();
      } catch (HttpDataSourceException e) {
        // Expected.
        assertThat(e instanceof CronetDataSource.OpenException).isTrue();
        assertThat(e.getCause() instanceof SocketTimeoutException).isTrue();
        assertThat(((CronetDataSource.OpenException) e).cronetConnectionStatus)
            .isEqualTo(TEST_CONNECTION_STATUS);
        timedOutLatch.countDown();
      }
    }
  }.start();
  startCondition.block();
  // We should still be trying to open.
  assertNotCountedDown(timedOutLatch);
  // We should still be trying to open as we approach the timeout.
  SystemClock.setCurrentTimeMillis(startTimeMs + TEST_CONNECT_TIMEOUT_MS - 1);
  assertNotCountedDown(timedOutLatch);
  // Now we timeout.
  SystemClock.setCurrentTimeMillis(startTimeMs + TEST_CONNECT_TIMEOUT_MS + 10);
  timedOutLatch.await();
  verify(mockTransferListener, never())
      .onTransferStart(dataSourceUnderTest, testDataSpec, /* isNetwork= */ true);
}