use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.
the class CronetDataSourceTest method testRedirectIncreasesConnectionTimeout.
@Test
public void testRedirectIncreasesConnectionTimeout() throws InterruptedException {
when(mockClock.elapsedRealtime()).thenReturn(0L);
final ConditionVariable startCondition = buildUrlRequestStartedCondition();
final ConditionVariable timedOutCondition = new ConditionVariable();
final AtomicInteger openExceptions = new AtomicInteger(0);
new Thread() {
@Override
public void run() {
try {
dataSourceUnderTest.open(testDataSpec);
fail();
} catch (HttpDataSourceException e) {
// Expected.
assertTrue(e instanceof CronetDataSource.OpenException);
assertTrue(e.getCause() instanceof SocketTimeoutException);
openExceptions.getAndIncrement();
timedOutCondition.open();
}
}
}.start();
startCondition.block();
// We should still be trying to open.
assertFalse(timedOutCondition.block(50));
// We should still be trying to open as we approach the timeout.
when(mockClock.elapsedRealtime()).thenReturn((long) TEST_CONNECT_TIMEOUT_MS - 1);
assertFalse(timedOutCondition.block(50));
// A redirect arrives just in time.
dataSourceUnderTest.onRedirectReceived(mockUrlRequest, testUrlResponseInfo, "RandomRedirectedUrl1");
long newTimeoutMs = 2 * TEST_CONNECT_TIMEOUT_MS - 1;
when(mockClock.elapsedRealtime()).thenReturn(newTimeoutMs - 1);
// Give the thread some time to run.
assertFalse(timedOutCondition.block(newTimeoutMs));
// We should still be trying to open as we approach the new timeout.
assertFalse(timedOutCondition.block(50));
// A redirect arrives just in time.
dataSourceUnderTest.onRedirectReceived(mockUrlRequest, testUrlResponseInfo, "RandomRedirectedUrl2");
newTimeoutMs = 3 * TEST_CONNECT_TIMEOUT_MS - 2;
when(mockClock.elapsedRealtime()).thenReturn(newTimeoutMs - 1);
// Give the thread some time to run.
assertFalse(timedOutCondition.block(newTimeoutMs));
// We should still be trying to open as we approach the new timeout.
assertFalse(timedOutCondition.block(50));
// Now we timeout.
when(mockClock.elapsedRealtime()).thenReturn(newTimeoutMs);
timedOutCondition.block();
verify(mockTransferListener, never()).onTransferStart(dataSourceUnderTest, testDataSpec);
assertEquals(1, openExceptions.get());
}
use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.
the class CronetDataSourceTest method testConnectTimeout.
@Test
public void testConnectTimeout() {
when(mockClock.elapsedRealtime()).thenReturn(0L);
final ConditionVariable startCondition = buildUrlRequestStartedCondition();
final ConditionVariable timedOutCondition = new ConditionVariable();
new Thread() {
@Override
public void run() {
try {
dataSourceUnderTest.open(testDataSpec);
fail();
} catch (HttpDataSourceException e) {
// Expected.
assertTrue(e instanceof CronetDataSource.OpenException);
assertTrue(e.getCause() instanceof SocketTimeoutException);
assertEquals(TEST_CONNECTION_STATUS, ((CronetDataSource.OpenException) e).cronetConnectionStatus);
timedOutCondition.open();
}
}
}.start();
startCondition.block();
// We should still be trying to open.
assertFalse(timedOutCondition.block(50));
// We should still be trying to open as we approach the timeout.
when(mockClock.elapsedRealtime()).thenReturn((long) TEST_CONNECT_TIMEOUT_MS - 1);
assertFalse(timedOutCondition.block(50));
// Now we timeout.
when(mockClock.elapsedRealtime()).thenReturn((long) TEST_CONNECT_TIMEOUT_MS);
timedOutCondition.block();
verify(mockTransferListener, never()).onTransferStart(dataSourceUnderTest, testDataSpec);
}
use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.
the class CronetDataSourceTest method testConnectResponseBeforeTimeout.
@Test
public void testConnectResponseBeforeTimeout() {
when(mockClock.elapsedRealtime()).thenReturn(0L);
final ConditionVariable startCondition = buildUrlRequestStartedCondition();
final ConditionVariable openCondition = new ConditionVariable();
new Thread() {
@Override
public void run() {
try {
dataSourceUnderTest.open(testDataSpec);
openCondition.open();
} catch (HttpDataSourceException e) {
fail();
}
}
}.start();
startCondition.block();
// We should still be trying to open.
assertFalse(openCondition.block(50));
// We should still be trying to open as we approach the timeout.
when(mockClock.elapsedRealtime()).thenReturn((long) TEST_CONNECT_TIMEOUT_MS - 1);
assertFalse(openCondition.block(50));
// The response arrives just in time.
dataSourceUnderTest.onResponseStarted(mockUrlRequest, testUrlResponseInfo);
openCondition.block();
}
use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.
the class CronetDataSourceTest method readInterrupted.
@Test
public void readInterrupted() throws HttpDataSourceException, InterruptedException {
mockResponseStartSuccess();
dataSourceUnderTest.open(testDataSpec);
final ConditionVariable startCondition = buildReadStartedCondition();
final CountDownLatch timedOutLatch = new CountDownLatch(1);
byte[] returnedBuffer = new byte[8];
Thread thread = new Thread() {
@Override
public void run() {
try {
dataSourceUnderTest.read(returnedBuffer, 0, 8);
fail();
} catch (HttpDataSourceException e) {
// Expected.
assertThat(e).hasCauseThat().isInstanceOf(InterruptedIOException.class);
timedOutLatch.countDown();
}
}
};
thread.start();
startCondition.block();
assertNotCountedDown(timedOutLatch);
// Now we interrupt.
thread.interrupt();
timedOutLatch.await();
}
use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.
the class CronetDataSourceTest method redirectIncreasesConnectionTimeout.
@Test
public void redirectIncreasesConnectionTimeout() throws Exception {
long startTimeMs = SystemClock.elapsedRealtime();
final ConditionVariable startCondition = buildUrlRequestStartedCondition();
final CountDownLatch timedOutLatch = new CountDownLatch(1);
final AtomicInteger openExceptions = new AtomicInteger(0);
new Thread() {
@Override
public void run() {
try {
dataSourceUnderTest.open(testDataSpec);
fail();
} catch (HttpDataSourceException e) {
// Expected.
assertThat(e).isInstanceOf(CronetDataSource.OpenException.class);
assertThat(e).hasCauseThat().isInstanceOf(SocketTimeoutException.class);
openExceptions.getAndIncrement();
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.
setSystemClockInMsAndTriggerPendingMessages(/* nowMs= */
startTimeMs + TEST_CONNECT_TIMEOUT_MS - 1);
assertNotCountedDown(timedOutLatch);
// A redirect arrives just in time.
dataSourceUnderTest.urlRequestCallback.onRedirectReceived(mockUrlRequest, testUrlResponseInfo, "RandomRedirectedUrl1");
long newTimeoutMs = 2 * TEST_CONNECT_TIMEOUT_MS - 1;
setSystemClockInMsAndTriggerPendingMessages(/* nowMs= */
startTimeMs + newTimeoutMs - 1);
// We should still be trying to open as we approach the new timeout.
assertNotCountedDown(timedOutLatch);
// A redirect arrives just in time.
dataSourceUnderTest.urlRequestCallback.onRedirectReceived(mockUrlRequest, testUrlResponseInfo, "RandomRedirectedUrl2");
newTimeoutMs = 3 * TEST_CONNECT_TIMEOUT_MS - 2;
setSystemClockInMsAndTriggerPendingMessages(/* nowMs= */
startTimeMs + newTimeoutMs - 1);
// We should still be trying to open as we approach the new timeout.
assertNotCountedDown(timedOutLatch);
// Now we timeout.
setSystemClockInMsAndTriggerPendingMessages(/* nowMs= */
startTimeMs + newTimeoutMs + 10);
timedOutLatch.await();
verify(mockTransferListener, never()).onTransferStart(dataSourceUnderTest, testDataSpec, /* isNetwork= */
true);
assertThat(openExceptions.get()).isEqualTo(1);
}
Aggregations