Search in sources :

Example 1 with ConditionVariable

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());
}
Also used : ConditionVariable(android.os.ConditionVariable) SocketTimeoutException(java.net.SocketTimeoutException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpDataSourceException(com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException) Test(org.junit.Test)

Example 2 with ConditionVariable

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);
}
Also used : ConditionVariable(android.os.ConditionVariable) SocketTimeoutException(java.net.SocketTimeoutException) HttpDataSourceException(com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException) Test(org.junit.Test)

Example 3 with ConditionVariable

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();
}
Also used : ConditionVariable(android.os.ConditionVariable) HttpDataSourceException(com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException) Test(org.junit.Test)

Example 4 with ConditionVariable

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();
}
Also used : ConditionVariable(android.os.ConditionVariable) InterruptedIOException(java.io.InterruptedIOException) HttpDataSourceException(com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with ConditionVariable

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);
}
Also used : ConditionVariable(android.os.ConditionVariable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpDataSourceException(com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)15 ConditionVariable (android.os.ConditionVariable)12 HttpDataSourceException (com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException)9 ConditionVariable (com.google.android.exoplayer2.util.ConditionVariable)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 InterruptedIOException (java.io.InterruptedIOException)4 SocketTimeoutException (java.net.SocketTimeoutException)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 HandlerThread (android.os.HandlerThread)2 MediaPeriod (com.google.android.exoplayer2.source.MediaPeriod)2 HandlerWrapper (com.google.android.exoplayer2.util.HandlerWrapper)2 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Handler (android.os.Handler)1 Looper (android.os.Looper)1 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)1 MediaItem (com.google.android.exoplayer2.MediaItem)1 PlaybackException (com.google.android.exoplayer2.PlaybackException)1 Player (com.google.android.exoplayer2.Player)1 RobolectricUtil.createRobolectricConditionVariable (com.google.android.exoplayer2.robolectric.RobolectricUtil.createRobolectricConditionVariable)1