Search in sources :

Example 21 with HttpDataSourceException

use of androidx.media3.datasource.HttpDataSource.HttpDataSourceException in project media by androidx.

the class CronetDataSourceTest method factory_noFallbackFactoryCronetNotAvailable_delegateDefaultRequestPropertiesToInternalFallbackFactory.

// Tests deprecated fallback functionality.
@SuppressWarnings("deprecation")
@Test
public void factory_noFallbackFactoryCronetNotAvailable_delegateDefaultRequestPropertiesToInternalFallbackFactory() throws HttpDataSourceException, InterruptedException {
    MockWebServer mockWebServer = new MockWebServer();
    mockWebServer.enqueue(new MockResponse());
    CronetEngineWrapper cronetEngineWrapper = new CronetEngineWrapper((CronetEngine) null);
    Map<String, String> defaultRequestProperties = new HashMap<>();
    defaultRequestProperties.put("0", "defaultRequestProperty0");
    HttpDataSource dataSourceUnderTest = new CronetDataSource.Factory(cronetEngineWrapper, executorService).setDefaultRequestProperties(defaultRequestProperties).createDataSource();
    dataSourceUnderTest.open(new DataSpec.Builder().setUri(mockWebServer.url("/test-path").toString()).build());
    Headers headers = mockWebServer.takeRequest(10, SECONDS).getHeaders();
    assertThat(headers.get("0")).isEqualTo("defaultRequestProperty0");
    assertThat(dataSourceUnderTest).isInstanceOf(DefaultHttpDataSource.class);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HashMap(java.util.HashMap) Headers(okhttp3.Headers) MockWebServer(okhttp3.mockwebserver.MockWebServer) DataSpec(androidx.media3.datasource.DataSpec) DefaultHttpDataSource(androidx.media3.datasource.DefaultHttpDataSource) HttpDataSource(androidx.media3.datasource.HttpDataSource) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 22 with HttpDataSourceException

use of androidx.media3.datasource.HttpDataSource.HttpDataSourceException in project media by androidx.

the class CronetDataSourceTest method connectInterrupted.

@Test
public void connectInterrupted() throws InterruptedException {
    long startTimeMs = SystemClock.elapsedRealtime();
    final ConditionVariable startCondition = buildUrlRequestStartedCondition();
    final CountDownLatch timedOutLatch = new CountDownLatch(1);
    Thread thread = 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(InterruptedIOException.class);
                assertThat(((CronetDataSource.OpenException) e).cronetConnectionStatus).isEqualTo(TEST_INVALID_CONNECTION_STATUS);
                timedOutLatch.countDown();
            }
        }
    };
    thread.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);
    // Now we interrupt.
    thread.interrupt();
    timedOutLatch.await();
    verify(mockTransferListener, never()).onTransferStart(dataSourceUnderTest, testDataSpec, /* isNetwork= */
    true);
}
Also used : ConditionVariable(android.os.ConditionVariable) InterruptedIOException(java.io.InterruptedIOException) HttpDataSourceException(androidx.media3.datasource.HttpDataSource.HttpDataSourceException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 23 with HttpDataSourceException

use of androidx.media3.datasource.HttpDataSource.HttpDataSourceException in project media by androidx.

the class CronetDataSourceTest method connectResponseBeforeTimeout.

@Test
public void connectResponseBeforeTimeout() 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.
    setSystemClockInMsAndTriggerPendingMessages(/* nowMs= */
    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();
}
Also used : ConditionVariable(android.os.ConditionVariable) HttpDataSourceException(androidx.media3.datasource.HttpDataSource.HttpDataSourceException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) HttpDataSourceException(androidx.media3.datasource.HttpDataSource.HttpDataSourceException) NetworkException(org.chromium.net.NetworkException) InterruptedIOException(java.io.InterruptedIOException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) InvalidResponseCodeException(androidx.media3.datasource.HttpDataSource.InvalidResponseCodeException) Test(org.junit.Test)

Example 24 with HttpDataSourceException

use of androidx.media3.datasource.HttpDataSource.HttpDataSourceException in project media by androidx.

the class CronetDataSourceTest method overread.

@Test
public void overread() throws HttpDataSourceException {
    testDataSpec = new DataSpec(Uri.parse(TEST_URL), 0, 16);
    testResponseHeader.put("Content-Length", Long.toString(16L));
    mockResponseStartSuccess();
    mockReadSuccess(0, 16);
    dataSourceUnderTest.open(testDataSpec);
    byte[] returnedBuffer = new byte[8];
    int bytesRead = dataSourceUnderTest.read(returnedBuffer, 0, 8);
    assertThat(bytesRead).isEqualTo(8);
    assertThat(returnedBuffer).isEqualTo(buildTestDataArray(0, 8));
    // The current buffer is kept if not completely consumed by DataSource reader.
    returnedBuffer = new byte[8];
    bytesRead += dataSourceUnderTest.read(returnedBuffer, 0, 6);
    assertThat(bytesRead).isEqualTo(14);
    assertThat(returnedBuffer).isEqualTo(suffixZeros(buildTestDataArray(8, 6), 8));
    // 2 bytes left at this point.
    returnedBuffer = new byte[8];
    bytesRead += dataSourceUnderTest.read(returnedBuffer, 0, 8);
    assertThat(bytesRead).isEqualTo(16);
    assertThat(returnedBuffer).isEqualTo(suffixZeros(buildTestDataArray(14, 2), 8));
    // Should have only called read on cronet once.
    verify(mockUrlRequest, times(1)).read(any(ByteBuffer.class));
    verify(mockTransferListener, times(1)).onBytesTransferred(dataSourceUnderTest, testDataSpec, /* isNetwork= */
    true, 8);
    verify(mockTransferListener, times(1)).onBytesTransferred(dataSourceUnderTest, testDataSpec, /* isNetwork= */
    true, 6);
    verify(mockTransferListener, times(1)).onBytesTransferred(dataSourceUnderTest, testDataSpec, /* isNetwork= */
    true, 2);
    // Now we already returned the 16 bytes initially asked.
    // Try to read again even though all requested 16 bytes are already returned.
    // Return C.RESULT_END_OF_INPUT
    returnedBuffer = new byte[16];
    int bytesOverRead = dataSourceUnderTest.read(returnedBuffer, 0, 16);
    assertThat(bytesOverRead).isEqualTo(C.RESULT_END_OF_INPUT);
    assertThat(returnedBuffer).isEqualTo(new byte[16]);
    // C.RESULT_END_OF_INPUT should not be reported though the TransferListener.
    verify(mockTransferListener, never()).onBytesTransferred(dataSourceUnderTest, testDataSpec, /* isNetwork= */
    true, C.RESULT_END_OF_INPUT);
    // There should still be only one call to read on cronet.
    verify(mockUrlRequest, times(1)).read(any(ByteBuffer.class));
    // Check for connection not automatically closed.
    verify(mockUrlRequest, never()).cancel();
    assertThat(bytesRead).isEqualTo(16);
}
Also used : DataSpec(androidx.media3.datasource.DataSpec) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 25 with HttpDataSourceException

use of androidx.media3.datasource.HttpDataSource.HttpDataSourceException in project media by androidx.

the class CronetDataSourceTest method factory_noFallbackFactoryCronetNotAvailable_delegateTransferListenerToInternalFallbackFactory.

// Tests deprecated fallback functionality.
@SuppressWarnings("deprecation")
@Test
public void factory_noFallbackFactoryCronetNotAvailable_delegateTransferListenerToInternalFallbackFactory() throws HttpDataSourceException {
    MockWebServer mockWebServer = new MockWebServer();
    mockWebServer.enqueue(new MockResponse());
    CronetEngineWrapper cronetEngineWrapper = new CronetEngineWrapper((CronetEngine) null);
    HttpDataSource dataSourceUnderTest = new CronetDataSource.Factory(cronetEngineWrapper, executorService).setTransferListener(mockTransferListener).createDataSource();
    DataSpec dataSpec = new DataSpec.Builder().setUri(mockWebServer.url("/test-path").toString()).build();
    dataSourceUnderTest.open(dataSpec);
    verify(mockTransferListener).onTransferInitializing(eq(dataSourceUnderTest), eq(dataSpec), /* isNetwork= */
    eq(true));
    verify(mockTransferListener).onTransferStart(eq(dataSourceUnderTest), eq(dataSpec), /* isNetwork= */
    eq(true));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) DataSpec(androidx.media3.datasource.DataSpec) DefaultHttpDataSource(androidx.media3.datasource.DefaultHttpDataSource) HttpDataSource(androidx.media3.datasource.HttpDataSource) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)22 DataSpec (androidx.media3.datasource.DataSpec)15 HttpDataSourceException (androidx.media3.datasource.HttpDataSource.HttpDataSourceException)7 ConditionVariable (android.os.ConditionVariable)6 InterruptedIOException (java.io.InterruptedIOException)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 ByteBuffer (java.nio.ByteBuffer)5 DefaultHttpDataSource (androidx.media3.datasource.DefaultHttpDataSource)4 HttpDataSource (androidx.media3.datasource.HttpDataSource)4 MockResponse (okhttp3.mockwebserver.MockResponse)4 MockWebServer (okhttp3.mockwebserver.MockWebServer)4 Nullable (androidx.annotation.Nullable)3 IOException (java.io.IOException)3 Headers (okhttp3.Headers)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 DataSourceException (androidx.media3.datasource.DataSourceException)2 SocketTimeoutException (java.net.SocketTimeoutException)2 UnknownHostException (java.net.UnknownHostException)2 HashMap (java.util.HashMap)2 List (java.util.List)2