Search in sources :

Example 1 with FailOnCloseDataSink

use of com.google.android.exoplayer2.testutil.FailOnCloseDataSink 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);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) FakeDataSet(com.google.android.exoplayer2.testutil.FakeDataSet) FileDataSource(com.google.android.exoplayer2.upstream.FileDataSource) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) FailOnCloseDataSink(com.google.android.exoplayer2.testutil.FailOnCloseDataSink) Test(org.junit.Test)

Aggregations

FailOnCloseDataSink (com.google.android.exoplayer2.testutil.FailOnCloseDataSink)1 FakeDataSet (com.google.android.exoplayer2.testutil.FakeDataSet)1 FakeDataSource (com.google.android.exoplayer2.testutil.FakeDataSource)1 DataSpec (com.google.android.exoplayer2.upstream.DataSpec)1 FileDataSource (com.google.android.exoplayer2.upstream.FileDataSource)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1