Don't setContentLength() in CacheDataSource if the current request ignores cache.

Otherwise an empty cache span is created.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=146014081
This commit is contained in:
cblay 2017-01-30 11:04:29 -08:00 committed by Oliver Woodman
parent a7dd0f7609
commit 2e7f9fb6cb
2 changed files with 19 additions and 7 deletions

View File

@ -119,6 +119,13 @@ public class CacheDataSourceTest extends InstrumentationTestCase {
C.LENGTH_UNSET, KEY_2)));
}
public void testIgnoreCacheForUnsetLengthRequests() throws Exception {
CacheDataSource cacheDataSource = createCacheDataSource(false, true,
CacheDataSource.FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS);
assertReadData(cacheDataSource, true, 0, C.LENGTH_UNSET);
MoreAsserts.assertEmpty(simpleCache.getKeys());
}
private void assertCacheAndRead(boolean unboundedRequest, boolean simulateUnknownLength)
throws IOException {
// Read all data from upstream and cache
@ -171,6 +178,12 @@ public class CacheDataSourceTest extends InstrumentationTestCase {
private CacheDataSource createCacheDataSource(boolean setReadException,
boolean simulateUnknownLength) {
return createCacheDataSource(setReadException, simulateUnknownLength,
CacheDataSource.FLAG_BLOCK_ON_CACHE);
}
private CacheDataSource createCacheDataSource(boolean setReadException,
boolean simulateUnknownLength, @CacheDataSource.Flags int flags) {
Builder builder = new Builder();
if (setReadException) {
builder.appendReadError(new IOException("Shouldn't read from upstream"));
@ -178,8 +191,7 @@ public class CacheDataSourceTest extends InstrumentationTestCase {
builder.setSimulateUnknownLength(simulateUnknownLength);
builder.appendReadData(TEST_DATA);
FakeDataSource upstream = builder.build();
return new CacheDataSource(simpleCache, upstream, CacheDataSource.FLAG_BLOCK_ON_CACHE,
MAX_CACHE_FILE_SIZE);
return new CacheDataSource(simpleCache, upstream, flags, MAX_CACHE_FILE_SIZE);
}
}

View File

@ -330,16 +330,16 @@ public final class CacheDataSource implements DataSource {
// bytesRemaining == C.LENGTH_UNSET) and got a resolved length from open() request
if (currentRequestUnbounded && currentBytesRemaining != C.LENGTH_UNSET) {
bytesRemaining = currentBytesRemaining;
// If writing into cache
if (lockedSpan != null) {
setContentLength(dataSpec.position + bytesRemaining);
}
setContentLength(dataSpec.position + bytesRemaining);
}
return successful;
}
private void setContentLength(long length) throws IOException {
cache.setContentLength(key, length);
// If writing into cache
if (currentDataSource == cacheWriteDataSource) {
cache.setContentLength(key, length);
}
}
private void closeCurrentSource() throws IOException {