Skip to content

Commit

Permalink
tests for cache trimming (danikula#112, PR 114)
Browse files Browse the repository at this point in the history
  • Loading branch information
danikula committed Apr 21, 2017
1 parent 699aa05 commit e0ee4a7
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,48 @@ public void testGetProxiedUrlForExistedCache() throws Exception {
proxy.shutdown();
}

@Test
public void testTrimFileCacheForTotalCountLru() throws Exception {
FileNameGenerator fileNameGenerator = new Md5FileNameGenerator();
HttpProxyCacheServer proxy = new HttpProxyCacheServer.Builder(RuntimeEnvironment.application)
.cacheDirectory(cacheFolder)
.fileNameGenerator(fileNameGenerator)
.maxCacheFilesCount(2)
.build();
readProxyResponse(proxy, proxy.getProxyUrl(HTTP_DATA_URL), 0);
assertThat(new File(cacheFolder, fileNameGenerator.generate(HTTP_DATA_URL))).exists();

readProxyResponse(proxy, proxy.getProxyUrl(HTTP_DATA_URL_ONE_REDIRECT), 0);
assertThat(new File(cacheFolder, fileNameGenerator.generate(HTTP_DATA_URL_ONE_REDIRECT))).exists();

readProxyResponse(proxy, proxy.getProxyUrl(HTTP_DATA_URL_3_REDIRECTS), 0);
assertThat(new File(cacheFolder, fileNameGenerator.generate(HTTP_DATA_URL_3_REDIRECTS))).exists();

waitForAsyncTrimming();
assertThat(new File(cacheFolder, fileNameGenerator.generate(HTTP_DATA_URL))).doesNotExist();
}

@Test
public void testTrimFileCacheForTotalSizeLru() throws Exception {
FileNameGenerator fileNameGenerator = new Md5FileNameGenerator();
HttpProxyCacheServer proxy = new HttpProxyCacheServer.Builder(RuntimeEnvironment.application)
.cacheDirectory(cacheFolder)
.fileNameGenerator(fileNameGenerator)
.maxCacheSize(HTTP_DATA_SIZE * 3 - 1)
.build();
readProxyResponse(proxy, proxy.getProxyUrl(HTTP_DATA_URL), 0);
assertThat(new File(cacheFolder, fileNameGenerator.generate(HTTP_DATA_URL))).exists();

readProxyResponse(proxy, proxy.getProxyUrl(HTTP_DATA_URL_ONE_REDIRECT), 0);
assertThat(new File(cacheFolder, fileNameGenerator.generate(HTTP_DATA_URL_ONE_REDIRECT))).exists();

readProxyResponse(proxy, proxy.getProxyUrl(HTTP_DATA_URL_3_REDIRECTS), 0);
assertThat(new File(cacheFolder, fileNameGenerator.generate(HTTP_DATA_URL_3_REDIRECTS))).exists();

waitForAsyncTrimming();
assertThat(new File(cacheFolder, fileNameGenerator.generate(HTTP_DATA_URL))).doesNotExist();
}

private Pair<File, Response> readProxyData(String url, int offset) throws IOException {
File file = file(cacheFolder, url);
HttpProxyCacheServer proxy = newProxy(cacheFolder);
Expand All @@ -321,4 +363,8 @@ private HttpProxyCacheServer newProxy(File cacheDir) {
.cacheDirectory(cacheDir)
.build();
}

private void waitForAsyncTrimming() throws InterruptedException {
Thread.sleep(500);
}
}
51 changes: 47 additions & 4 deletions test/src/test/java/com/danikula/videocache/file/FileCacheTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.danikula.videocache.file;

import com.danikula.android.garden.io.Files;
import com.danikula.android.garden.io.IoUtils;
import com.danikula.videocache.BaseTest;
import com.danikula.videocache.Cache;
import com.danikula.videocache.ProxyCacheException;
Expand All @@ -11,6 +10,7 @@
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import static com.danikula.videocache.support.ProxyCacheTestUtils.ASSETS_DATA_NAME;
Expand All @@ -19,6 +19,7 @@
import static com.danikula.videocache.support.ProxyCacheTestUtils.getTempFile;
import static com.danikula.videocache.support.ProxyCacheTestUtils.loadAssetFile;
import static com.danikula.videocache.support.ProxyCacheTestUtils.newCacheFile;
import static com.google.common.io.Files.write;
import static org.fest.assertions.api.Assertions.assertThat;

/**
Expand Down Expand Up @@ -95,7 +96,8 @@ public void testAppendDiscCache() throws Exception {
public void testIsFileCacheCompleted() throws Exception {
File file = newCacheFile();
File partialFile = new File(file.getParentFile(), file.getName() + ".download");
IoUtils.saveToFile(loadAssetFile(ASSETS_DATA_NAME), partialFile);
write(loadAssetFile(ASSETS_DATA_NAME), partialFile);
write(loadAssetFile(ASSETS_DATA_NAME), partialFile);
Cache fileCache = new FileCache(partialFile);

assertThat(file.exists()).isFalse();
Expand All @@ -114,7 +116,7 @@ public void testIsFileCacheCompleted() throws Exception {
@Test(expected = ProxyCacheException.class)
public void testErrorWritingCompletedCache() throws Exception {
File file = newCacheFile();
IoUtils.saveToFile(loadAssetFile(ASSETS_DATA_NAME), file);
write(loadAssetFile(ASSETS_DATA_NAME), file);
FileCache fileCache = new FileCache(file);
fileCache.append(generate(100), 20);
Assert.fail();
Expand All @@ -124,7 +126,7 @@ public void testErrorWritingCompletedCache() throws Exception {
public void testErrorWritingAfterCompletion() throws Exception {
File file = newCacheFile();
File partialFile = new File(file.getParentFile(), file.getName() + ".download");
IoUtils.saveToFile(loadAssetFile(ASSETS_DATA_NAME), partialFile);
write(loadAssetFile(ASSETS_DATA_NAME), partialFile);
FileCache fileCache = new FileCache(partialFile);
fileCache.complete();
fileCache.append(generate(100), 20);
Expand All @@ -140,4 +142,45 @@ public void testFileErrorForDiscCache() throws Exception {
fileCache.available();
Assert.fail();
}

@Test
public void testTrimAfterCompletionForTotalCountLru() throws Exception {
File cacheDir = newCacheFile();
DiskUsage diskUsage = new TotalCountLruDiskUsage(2);
byte[] data = loadAssetFile(ASSETS_DATA_NAME);
saveAndCompleteCache(diskUsage, data,
new File(cacheDir, "0.dat"),
new File(cacheDir, "1.dat"),
new File(cacheDir, "2.dat")
);
waitForAsyncTrimming();
assertThat(new File(cacheDir, "0.dat")).doesNotExist();
}

@Test
public void testTrimAfterCompletionForTotalSizeLru() throws Exception {
File cacheDir = newCacheFile();
byte[] data = loadAssetFile(ASSETS_DATA_NAME);
DiskUsage diskUsage = new TotalSizeLruDiskUsage(data.length*3-1);
saveAndCompleteCache(diskUsage, data,
new File(cacheDir, "0.dat"),
new File(cacheDir, "1.dat"),
new File(cacheDir, "2.dat")
);
waitForAsyncTrimming();
assertThat(new File(cacheDir, "0.dat")).doesNotExist();
}

private void saveAndCompleteCache(DiskUsage diskUsage, byte[] data, File... files) throws ProxyCacheException, IOException {
for (File file : files) {
FileCache fileCache = new FileCache(file, diskUsage);
fileCache.append(data, data.length);
fileCache.complete();
assertThat(file).exists();
}
}

private void waitForAsyncTrimming() throws InterruptedException {
Thread.sleep(500);
}
}

0 comments on commit e0ee4a7

Please sign in to comment.