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);
}
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);
}
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();
}
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);
}
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));
}
Aggregations