Remove empty CachedContent in SimpleCache.releaseHoleSpan()

In startReadWrite*() methods a new CachedContent is created if the there
isn't one already for the given key. If the span is release without
writing any content, this fix removes the added CachedContent.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=183666821
This commit is contained in:
eguven 2018-01-29 08:09:15 -08:00 committed by Oliver Woodman
parent 1dde2adbf3
commit 168a5d63c5
4 changed files with 7 additions and 5 deletions

View File

@ -236,6 +236,7 @@ public final class SimpleCache implements Cache {
Assertions.checkNotNull(cachedContent); Assertions.checkNotNull(cachedContent);
Assertions.checkState(cachedContent.isLocked()); Assertions.checkState(cachedContent.isLocked());
cachedContent.setLocked(false); cachedContent.setLocked(false);
index.maybeRemove(cachedContent.key);
notifyAll(); notifyAll();
} }

View File

@ -147,8 +147,7 @@ public final class CacheDataSourceTest {
} }
@Test @Test
public void testCantResolvedLengthContentReadInOneConnectionAndLengthIsResolved() public void testUnknownLengthContentReadInOneConnectionAndLengthIsResolved() throws Exception {
throws Exception {
FakeDataSource upstream = new FakeDataSource(); FakeDataSource upstream = new FakeDataSource();
upstream upstream
.getDataSet() .getDataSet()
@ -157,7 +156,8 @@ public final class CacheDataSourceTest {
.setSimulateUnknownLength(true); .setSimulateUnknownLength(true);
CacheDataSource cacheDataSource = new CacheDataSource(cache, upstream, 0); CacheDataSource cacheDataSource = new CacheDataSource(cache, upstream, 0);
cacheDataSource.open(new DataSpec(testDataUri, 0, C.LENGTH_UNSET, testDataKey)); int flags = DataSpec.FLAG_ALLOW_CACHING_UNKNOWN_LENGTH;
cacheDataSource.open(new DataSpec(testDataUri, 0, C.LENGTH_UNSET, testDataKey, flags));
TestUtil.readToEnd(cacheDataSource); TestUtil.readToEnd(cacheDataSource);
cacheDataSource.close(); cacheDataSource.close();

View File

@ -100,8 +100,8 @@ public final class DashDownloadTest extends ActivityInstrumentationTestCase2<Hos
dashDownloader.remove(); dashDownloader.remove();
assertWithMessage("There should be no content left.").that(cache.getKeys().size()).isEqualTo(0); assertWithMessage("There should be no cache key left").that(cache.getKeys()).isEmpty();
assertWithMessage("There should be no content left.").that(cache.getCacheSpace()).isEqualTo(0); assertWithMessage("There should be no content left").that(cache.getCacheSpace()).isEqualTo(0);
} }
public void testPartialDownload() throws Exception { public void testPartialDownload() throws Exception {

View File

@ -131,6 +131,7 @@ public final class CacheAsserts {
/** Asserts that the cache is empty. */ /** Asserts that the cache is empty. */
public static void assertCacheEmpty(Cache cache) { public static void assertCacheEmpty(Cache cache) {
assertThat(cache.getCacheSpace()).isEqualTo(0); assertThat(cache.getCacheSpace()).isEqualTo(0);
assertThat(cache.getKeys()).isEmpty();
} }
private CacheAsserts() {} private CacheAsserts() {}