/** @see <a href="https://github.com/JakeWharton/DiskLruCache/issues/2">Issue #2</a> */ @Test public void aggressiveClearingHandlesWrite() throws Exception { FileUtils.deleteDirectory(cacheDir); set("a", "a", "a"); assertValue("a", "a", "a"); }
@Test public void rebuildJournalOnRepeatedReads() throws Exception { set("a", "a", "a"); set("b", "b", "b"); long lastJournalLength = 0; while (true) { long journalLength = journalFile.length(); assertValue("a", "a", "a"); assertValue("b", "b", "b"); if (journalLength < lastJournalLength) { System.out .printf("Journal compacted from %s bytes to %s bytes\n", lastJournalLength, journalLength); break; // Test passed! } lastJournalLength = journalLength; } }
@Test public void rebuildJournalOnRepeatedEdits() throws Exception { long lastJournalLength = 0; while (true) { long journalLength = journalFile.length(); set("a", "a", "a"); set("b", "b", "b"); if (journalLength < lastJournalLength) { System.out .printf("Journal compacted from %s bytes to %s bytes\n", lastJournalLength, journalLength); break; } lastJournalLength = journalLength; } // Sanity check that a rebuilt journal behaves normally. assertValue("a", "a", "a"); assertValue("b", "b", "b"); }
/** @see <a href="https://github.com/JakeWharton/DiskLruCache/issues/28">Issue #28</a> */ @Test public void rebuildJournalOnRepeatedReadsWithOpenAndClose() throws Exception { set("a", "a", "a"); set("b", "b", "b"); long lastJournalLength = 0; while (true) { long journalLength = journalFile.length(); assertValue("a", "a", "a"); assertValue("b", "b", "b"); cache.close(); cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE); if (journalLength < lastJournalLength) { System.out .printf("Journal compacted from %s bytes to %s bytes\n", lastJournalLength, journalLength); break; // Test passed! } lastJournalLength = journalLength; } }
@Test public void evictionHonorsLruFromPreviousSession() throws Exception { set("a", "a", "a"); set("b", "b", "b"); set("c", "c", "c"); set("d", "d", "d"); set("e", "e", "e"); set("f", "f", "f"); cache.get("b"); // 'B' is now least recently used. assertThat(cache.size()).isEqualTo(12); cache.close(); cache = DiskLruCache.open(cacheDir, appVersion, 2, 10); set("g", "g", "g"); cache.flush(); assertThat(cache.size()).isEqualTo(10); assertAbsent("a"); assertValue("b", "b", "b"); assertAbsent("c"); assertValue("d", "d", "d"); assertValue("e", "e", "e"); assertValue("f", "f", "f"); assertValue("g", "g", "g"); }
@Test public void editSameVersion() throws Exception { set("a", "a", "a"); DiskLruCache.Value value = cache.get("a"); DiskLruCache.Editor editor = value.edit(); editor.set(1, "a2"); editor.commit(); assertValue("a", "a", "a2"); }
@Test public void evictionHonorsLruFromCurrentSession() throws Exception { cache.close(); cache = DiskLruCache.open(cacheDir, appVersion, 2, 10); set("a", "a", "a"); set("b", "b", "b"); set("c", "c", "c"); set("d", "d", "d"); set("e", "e", "e"); cache.get("b"); // 'B' is now least recently used. // Causing the size to grow to 12 should evict 'A'. set("f", "f", "f"); // Causing the size to grow to 12 should evict 'C'. set("g", "g", "g"); cache.flush(); assertThat(cache.size()).isEqualTo(10); assertAbsent("a"); assertValue("b", "b", "b"); assertAbsent("c"); assertValue("d", "d", "d"); assertValue("e", "e", "e"); assertValue("f", "f", "f"); }
assertThat(cache.size()).isEqualTo(8); assertAbsent("a"); assertValue("b", "bb", "bbbb"); assertValue("c", "c", "c"); assertThat(cache.size()).isEqualTo(10); assertAbsent("a"); assertValue("b", "bb", "bbbb"); assertValue("c", "c", "c"); assertValue("d", "d", "d"); assertAbsent("b"); assertAbsent("c"); assertValue("d", "d", "d"); assertValue("e", "eeee", "eeee");
@Test public void openWithTruncatedLineDiscardsThatLine() throws Exception { cache.close(); writeFile(getCleanFile("k1", 0), "A"); writeFile(getCleanFile("k1", 1), "B"); Writer writer = new FileWriter(journalFile); writer.write(MAGIC + "\n" + VERSION_1 + "\n100\n2\n\nCLEAN k1 1 1"); // no trailing newline writer.close(); cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE); assertThat(cache.get("k1")).isNull(); // The journal is not corrupt when editing after a truncated line. set("k1", "C", "D"); cache.close(); cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE); assertValue("k1", "C", "D"); }
@Test public void evictOnUpdate() throws Exception { cache.close(); cache = DiskLruCache.open(cacheDir, appVersion, 2, 10); set("a", "a", "aa"); // size 3 set("b", "b", "bb"); // size 3 set("c", "c", "cc"); // size 3 assertThat(cache.size()).isEqualTo(9); // Causing the size to grow to 11 should evict 'A'. set("b", "b", "bbbb"); cache.flush(); assertThat(cache.size()).isEqualTo(8); assertAbsent("a"); assertValue("b", "b", "bbbb"); assertValue("c", "c", "cc"); }
@Test public void editSnapshotAfterChangeAborted() throws Exception { set("a", "a", "a"); DiskLruCache.Value value = cache.get("a"); DiskLruCache.Editor toAbort = value.edit(); toAbort.set(0, "b"); toAbort.abort(); DiskLruCache.Editor editor = value.edit(); editor.set(1, "a2"); editor.commit(); assertValue("a", "a", "a2"); }