use of com.google.android.exoplayer2.upstream.cache.CacheDataSource in project ExoPlayer by google.
the class CacheDataSourceTest method propagatesHttpHeadersUpstream.
@Test
public void propagatesHttpHeadersUpstream() throws Exception {
CacheDataSource cacheDataSource = createCacheDataSource(/* setReadException= */
false, /* unknownLength= */
false);
DataSpec dataSpec = buildDataSpec(/* position= */
2, /* length= */
5);
cacheDataSource.open(dataSpec);
DataSpec[] upstreamDataSpecs = upstreamDataSource.getAndClearOpenedDataSpecs();
assertThat(upstreamDataSpecs).hasLength(1);
assertThat(upstreamDataSpecs[0].httpRequestHeaders).isEqualTo(this.httpRequestHeaders);
}
use of com.google.android.exoplayer2.upstream.cache.CacheDataSource in project ExoPlayer by google.
the class CacheDataSourceTest method deleteCachedWhileReadingFromUpstreamWithReadOnlyCacheDataSourceDoesNotCrash.
@Test
public void deleteCachedWhileReadingFromUpstreamWithReadOnlyCacheDataSourceDoesNotCrash() throws Exception {
// Create a fake data source with a 1 KB default data.
FakeDataSource upstream = new FakeDataSource();
int dataLength = 1024;
upstream.getDataSet().newDefaultData().appendReadData(dataLength).endData();
// Cache the latter half of the data.
int halfDataLength = 512;
DataSpec dataSpec = buildDataSpec(halfDataLength, C.LENGTH_UNSET);
CacheWriter cacheWriter = new CacheWriter(new CacheDataSource(cache, upstream), dataSpec, /* temporaryBuffer= */
null, /* progressListener= */
null);
cacheWriter.cache();
// Create cache read-only CacheDataSource.
CacheDataSource cacheDataSource = new CacheDataSource(cache, upstream, new FileDataSource(), null, 0, null);
// Open source and read some data from upstream as the data hasn't cached yet.
cacheDataSource.open(unboundedDataSpec);
DataSourceUtil.readExactly(cacheDataSource, 100);
// Delete cached data.
cache.removeResource(cacheDataSource.getCacheKeyFactory().buildCacheKey(unboundedDataSpec));
assertCacheEmpty(cache);
// Read the rest of the data.
DataSourceUtil.readToEnd(cacheDataSource);
cacheDataSource.close();
}
use of com.google.android.exoplayer2.upstream.cache.CacheDataSource in project ExoPlayer by google.
the class CacheDataSourceTest method ignoreCacheForUnsetLengthRequests.
@Test
public void ignoreCacheForUnsetLengthRequests() throws Exception {
FakeDataSource upstream = new FakeDataSource();
upstream.getDataSet().setData(testDataUri, TEST_DATA);
CacheDataSource cacheDataSource = new CacheDataSource(cache, upstream, CacheDataSource.FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS);
cacheDataSource.open(unboundedDataSpec);
DataSourceUtil.readToEnd(cacheDataSource);
cacheDataSource.close();
assertThat(cache.getKeys()).isEmpty();
}
use of com.google.android.exoplayer2.upstream.cache.CacheDataSource in project ExoPlayer by google.
the class CacheWriterTest method cache_afterFailureOnClose_succeeds.
@Test
public void cache_afterFailureOnClose_succeeds() throws Exception {
FakeDataSet fakeDataSet = new FakeDataSet().setRandomData("test_data", 100);
FakeDataSource upstreamDataSource = new FakeDataSource(fakeDataSet);
AtomicBoolean failOnClose = new AtomicBoolean(/* initialValue= */
true);
FailOnCloseDataSink dataSink = new FailOnCloseDataSink(cache, failOnClose);
CacheDataSource cacheDataSource = new CacheDataSource(cache, upstreamDataSource, new FileDataSource(), dataSink, /* flags= */
0, /* eventListener= */
null);
CachingCounters counters = new CachingCounters();
CacheWriter cacheWriter = new CacheWriter(cacheDataSource, new DataSpec(Uri.parse("test_data")), /* temporaryBuffer= */
null, counters);
// DataSink.close failing must cause the operation to fail rather than succeed.
assertThrows(IOException.class, cacheWriter::cache);
// Since all of the bytes were read through the DataSource chain successfully before the sink
// was closed, the progress listener will have seen all of the bytes being cached, even though
// this may not really be the case.
counters.assertValues(/* bytesAlreadyCached= */
0, /* bytesNewlyCached= */
100, /* contentLength= */
100);
failOnClose.set(false);
// The bytes will be downloaded again, but cached successfully this time.
cacheWriter.cache();
counters.assertValues(/* bytesAlreadyCached= */
0, /* bytesNewlyCached= */
100, /* contentLength= */
100);
assertCachedData(cache, fakeDataSet);
}
use of com.google.android.exoplayer2.upstream.cache.CacheDataSource in project ExoPlayer by google.
the class CacheWriterTest method cacheUnknownLengthPartialCaching.
@Test
public void cacheUnknownLengthPartialCaching() throws Exception {
FakeDataSet fakeDataSet = new FakeDataSet().newData("test_data").setSimulateUnknownLength(true).appendReadData(TestUtil.buildTestData(100)).endData();
FakeDataSource dataSource = new FakeDataSource(fakeDataSet);
Uri testUri = Uri.parse("test_data");
DataSpec dataSpec = new DataSpec(testUri, /* position= */
10, /* length= */
20);
CachingCounters counters = new CachingCounters();
CacheWriter cacheWriter = new CacheWriter(new CacheDataSource(cache, dataSource), dataSpec, /* temporaryBuffer= */
null, counters);
cacheWriter.cache();
counters.assertValues(0, 20, 20);
counters.reset();
cacheWriter = new CacheWriter(new CacheDataSource(cache, dataSource), new DataSpec(testUri), /* temporaryBuffer= */
null, counters);
cacheWriter.cache();
counters.assertValues(20, 80, 100);
assertCachedData(cache, fakeDataSet);
}
Aggregations