From d998d6e2d201bd26bab6f7624ccfa2242b58a05e Mon Sep 17 00:00:00 2001 From: Anonymous Date: Thu, 11 Oct 2018 11:02:44 -0700 Subject: Import of Volley from GitHub to AOSP. - 321174452380acd5caa7c19aac99cb95a2ed0ff6 Add a default image bitmap and an error image bitmap. (#2... by Ammar Aijazi - cb5d331761c4785b325acc7ff5caf8f41e932c69 Bump Volley to 1.2.0-SNAPSHOT. by Jeff Davidson - c31351334868a17cb79ab5b067d798bc80e343a3 Fix the build. by Jeff Davidson - 99d8ed84ddf653cb5f61a9ad63e5a3ced799d9ce Reduce cache churn from large entries. by Jeff Davidson - 6b7b3a2df79506269a0730a095849c5a87fc9cbb Enforce disk size limit more strictly in DiskBasedCache. by Jeff Davidson - 64481a321b9e57c78f22c7df53732e03f530933a Don't drop batched responses when new requests complete. ... by Jeff Davidson - 4e60e9d2ac2f2c81661a7f0d1bcbc72c190059c2 Bump version to 1.1.2 after final 1.1.1 release by Jeff Davidson - d1a3d5388c79ff1a6fdb904daeff9b39d0bb7d26 Delete Android.mk (#204) by Jeff Davidson GitOrigin-RevId: 321174452380acd5caa7c19aac99cb95a2ed0ff6 Change-Id: I433459689ad2fe1bf40e8ff30d196d948ef73f81 --- .../android/volley/toolbox/DiskBasedCacheTest.java | 144 ++++++++++++++++++--- .../volley/toolbox/NetworkImageViewTest.java | 3 + 2 files changed, 129 insertions(+), 18 deletions(-) (limited to 'src/test/java/com/android/volley/toolbox') diff --git a/src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java b/src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java index fb6392c..e499a37 100644 --- a/src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java +++ b/src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java @@ -166,36 +166,133 @@ public class DiskBasedCacheTest { } @Test - public void testTrim() { - Cache.Entry entry = randomData(2 * MAX_SIZE); + public void testTooLargeEntry() { + Cache.Entry entry = randomData(MAX_SIZE - getEntrySizeOnDisk("oversize")); cache.put("oversize", entry); - assertThatEntriesAreEqual(cache.get("oversize"), entry); + assertThat(cache.get("oversize"), is(nullValue())); + } - entry = randomData(1024); - cache.put("kilobyte", entry); + @Test + public void testMaxSizeEntry() { + Cache.Entry entry = randomData(MAX_SIZE - getEntrySizeOnDisk("maxsize") - 1); + cache.put("maxsize", entry); - assertThat(cache.get("oversize"), is(nullValue())); - assertThatEntriesAreEqual(cache.get("kilobyte"), entry); + assertThatEntriesAreEqual(cache.get("maxsize"), entry); + } + + @Test + public void testTrimAtThreshold() { + // Start with the largest possible entry. + Cache.Entry entry = randomData(MAX_SIZE - getEntrySizeOnDisk("maxsize") - 1); + cache.put("maxsize", entry); + + assertThatEntriesAreEqual(cache.get("maxsize"), entry); - Cache.Entry entry2 = randomData(1024); - cache.put("kilobyte2", entry2); - Cache.Entry entry3 = randomData(1024); - cache.put("kilobyte3", entry3); + // Now any new entry should cause the first one to be cleared. + entry = randomData(0); + cache.put("bit", entry); - assertThatEntriesAreEqual(cache.get("kilobyte"), entry); - assertThatEntriesAreEqual(cache.get("kilobyte2"), entry2); - assertThatEntriesAreEqual(cache.get("kilobyte3"), entry3); + assertThat(cache.get("goodsize"), is(nullValue())); + assertThatEntriesAreEqual(cache.get("bit"), entry); + } - entry = randomData(MAX_SIZE); + @Test + public void testTrimWithMultipleEvictions_underHysteresisThreshold() { + Cache.Entry entry1 = randomData(MAX_SIZE / 3 - getEntrySizeOnDisk("entry1") - 1); + cache.put("entry1", entry1); + Cache.Entry entry2 = randomData(MAX_SIZE / 3 - getEntrySizeOnDisk("entry2") - 1); + cache.put("entry2", entry2); + Cache.Entry entry3 = randomData(MAX_SIZE / 3 - getEntrySizeOnDisk("entry3") - 1); + cache.put("entry3", entry3); + + assertThatEntriesAreEqual(cache.get("entry1"), entry1); + assertThatEntriesAreEqual(cache.get("entry2"), entry2); + assertThatEntriesAreEqual(cache.get("entry3"), entry3); + + Cache.Entry entry = + randomData( + (int) (DiskBasedCache.HYSTERESIS_FACTOR * MAX_SIZE) + - getEntrySizeOnDisk("max")); cache.put("max", entry); - assertThat(cache.get("kilobyte"), is(nullValue())); - assertThat(cache.get("kilobyte2"), is(nullValue())); - assertThat(cache.get("kilobyte3"), is(nullValue())); + assertThat(cache.get("entry1"), is(nullValue())); + assertThat(cache.get("entry2"), is(nullValue())); + assertThat(cache.get("entry3"), is(nullValue())); assertThatEntriesAreEqual(cache.get("max"), entry); } + @Test + public void testTrimWithMultipleEvictions_atHysteresisThreshold() { + Cache.Entry entry1 = randomData(MAX_SIZE / 3 - getEntrySizeOnDisk("entry1") - 1); + cache.put("entry1", entry1); + Cache.Entry entry2 = randomData(MAX_SIZE / 3 - getEntrySizeOnDisk("entry2") - 1); + cache.put("entry2", entry2); + Cache.Entry entry3 = randomData(MAX_SIZE / 3 - getEntrySizeOnDisk("entry3") - 1); + cache.put("entry3", entry3); + + assertThatEntriesAreEqual(cache.get("entry1"), entry1); + assertThatEntriesAreEqual(cache.get("entry2"), entry2); + assertThatEntriesAreEqual(cache.get("entry3"), entry3); + + Cache.Entry entry = + randomData( + (int) (DiskBasedCache.HYSTERESIS_FACTOR * MAX_SIZE) + - getEntrySizeOnDisk("max") + + 1); + cache.put("max", entry); + + assertThat(cache.get("entry1"), is(nullValue())); + assertThat(cache.get("entry2"), is(nullValue())); + assertThat(cache.get("entry3"), is(nullValue())); + assertThat(cache.get("max"), is(nullValue())); + } + + @Test + public void testTrimWithPartialEvictions() { + Cache.Entry entry1 = randomData(MAX_SIZE / 3 - getEntrySizeOnDisk("entry1") - 1); + cache.put("entry1", entry1); + Cache.Entry entry2 = randomData(MAX_SIZE / 3 - getEntrySizeOnDisk("entry2") - 1); + cache.put("entry2", entry2); + Cache.Entry entry3 = randomData(MAX_SIZE / 3 - getEntrySizeOnDisk("entry3") - 1); + cache.put("entry3", entry3); + + assertThatEntriesAreEqual(cache.get("entry1"), entry1); + assertThatEntriesAreEqual(cache.get("entry2"), entry2); + assertThatEntriesAreEqual(cache.get("entry3"), entry3); + + Cache.Entry entry4 = randomData((MAX_SIZE - getEntrySizeOnDisk("entry4") - 1) / 2); + cache.put("entry4", entry4); + + assertThat(cache.get("entry1"), is(nullValue())); + assertThat(cache.get("entry2"), is(nullValue())); + assertThatEntriesAreEqual(cache.get("entry3"), entry3); + assertThatEntriesAreEqual(cache.get("entry4"), entry4); + } + + @Test + public void testLargeEntryDoesntClearCache() { + // Writing a large entry to an empty cache should succeed + Cache.Entry largeEntry = randomData(MAX_SIZE - getEntrySizeOnDisk("largeEntry") - 1); + cache.put("largeEntry", largeEntry); + + assertThatEntriesAreEqual(cache.get("largeEntry"), largeEntry); + + // Reset and fill up ~half the cache. + cache.clear(); + Cache.Entry entry = randomData(MAX_SIZE / 2 - getEntrySizeOnDisk("entry") - 1); + cache.put("entry", entry); + + assertThatEntriesAreEqual(cache.get("entry"), entry); + + // Writing the large entry should no-op, because otherwise the pruning algorithm would clear + // the whole cache, since the large entry is above the hysteresis threshold. + cache.put("largeEntry", largeEntry); + + assertThat(cache.get("largeEntry"), is(nullValue())); + assertThatEntriesAreEqual(cache.get("entry"), entry); + } + @Test @SuppressWarnings("TryFinallyCanBeTryWithResources") public void testGetBadMagic() throws IOException { @@ -518,4 +615,15 @@ public class DiskBasedCacheTest { private File[] listCachedFiles() { return temporaryFolder.getRoot().listFiles(); } + + private int getEntrySizeOnDisk(String key) { + // Header size is: + // 4 bytes for magic int + // 8 + len(key) bytes for key (long length) + // 8 bytes for etag (long length + 0 characters) + // 32 bytes for serverDate, lastModified, ttl, and softTtl longs + // 4 bytes for length of header list int + // == 56 + len(key) bytes total. + return 56 + key.length(); + } } diff --git a/src/test/java/com/android/volley/toolbox/NetworkImageViewTest.java b/src/test/java/com/android/volley/toolbox/NetworkImageViewTest.java index af8fad9..7705a8f 100644 --- a/src/test/java/com/android/volley/toolbox/NetworkImageViewTest.java +++ b/src/test/java/com/android/volley/toolbox/NetworkImageViewTest.java @@ -20,6 +20,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import android.content.Context; +import android.graphics.Bitmap; import android.util.AttributeSet; import android.view.ViewGroup.LayoutParams; import android.widget.ImageView.ScaleType; @@ -89,7 +90,9 @@ public class NetworkImageViewTest { assertNotNull( NetworkImageView.class.getMethod("setImageUrl", String.class, ImageLoader.class)); + assertNotNull(NetworkImageView.class.getMethod("setDefaultImageBitmap", Bitmap.class)); assertNotNull(NetworkImageView.class.getMethod("setDefaultImageResId", int.class)); + assertNotNull(NetworkImageView.class.getMethod("setErrorImageBitmap", Bitmap.class)); assertNotNull(NetworkImageView.class.getMethod("setErrorImageResId", int.class)); } } -- cgit v1.2.3