Skip to content

Commit e0ee4a7

Browse files
committed
tests for cache trimming (#112, PR 114)
1 parent 699aa05 commit e0ee4a7

2 files changed

Lines changed: 93 additions & 4 deletions

File tree

test/src/test/java/com/danikula/videocache/HttpProxyCacheServerTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,48 @@ public void testGetProxiedUrlForExistedCache() throws Exception {
296296
proxy.shutdown();
297297
}
298298

299+
@Test
300+
public void testTrimFileCacheForTotalCountLru() throws Exception {
301+
FileNameGenerator fileNameGenerator = new Md5FileNameGenerator();
302+
HttpProxyCacheServer proxy = new HttpProxyCacheServer.Builder(RuntimeEnvironment.application)
303+
.cacheDirectory(cacheFolder)
304+
.fileNameGenerator(fileNameGenerator)
305+
.maxCacheFilesCount(2)
306+
.build();
307+
readProxyResponse(proxy, proxy.getProxyUrl(HTTP_DATA_URL), 0);
308+
assertThat(new File(cacheFolder, fileNameGenerator.generate(HTTP_DATA_URL))).exists();
309+
310+
readProxyResponse(proxy, proxy.getProxyUrl(HTTP_DATA_URL_ONE_REDIRECT), 0);
311+
assertThat(new File(cacheFolder, fileNameGenerator.generate(HTTP_DATA_URL_ONE_REDIRECT))).exists();
312+
313+
readProxyResponse(proxy, proxy.getProxyUrl(HTTP_DATA_URL_3_REDIRECTS), 0);
314+
assertThat(new File(cacheFolder, fileNameGenerator.generate(HTTP_DATA_URL_3_REDIRECTS))).exists();
315+
316+
waitForAsyncTrimming();
317+
assertThat(new File(cacheFolder, fileNameGenerator.generate(HTTP_DATA_URL))).doesNotExist();
318+
}
319+
320+
@Test
321+
public void testTrimFileCacheForTotalSizeLru() throws Exception {
322+
FileNameGenerator fileNameGenerator = new Md5FileNameGenerator();
323+
HttpProxyCacheServer proxy = new HttpProxyCacheServer.Builder(RuntimeEnvironment.application)
324+
.cacheDirectory(cacheFolder)
325+
.fileNameGenerator(fileNameGenerator)
326+
.maxCacheSize(HTTP_DATA_SIZE * 3 - 1)
327+
.build();
328+
readProxyResponse(proxy, proxy.getProxyUrl(HTTP_DATA_URL), 0);
329+
assertThat(new File(cacheFolder, fileNameGenerator.generate(HTTP_DATA_URL))).exists();
330+
331+
readProxyResponse(proxy, proxy.getProxyUrl(HTTP_DATA_URL_ONE_REDIRECT), 0);
332+
assertThat(new File(cacheFolder, fileNameGenerator.generate(HTTP_DATA_URL_ONE_REDIRECT))).exists();
333+
334+
readProxyResponse(proxy, proxy.getProxyUrl(HTTP_DATA_URL_3_REDIRECTS), 0);
335+
assertThat(new File(cacheFolder, fileNameGenerator.generate(HTTP_DATA_URL_3_REDIRECTS))).exists();
336+
337+
waitForAsyncTrimming();
338+
assertThat(new File(cacheFolder, fileNameGenerator.generate(HTTP_DATA_URL))).doesNotExist();
339+
}
340+
299341
private Pair<File, Response> readProxyData(String url, int offset) throws IOException {
300342
File file = file(cacheFolder, url);
301343
HttpProxyCacheServer proxy = newProxy(cacheFolder);
@@ -321,4 +363,8 @@ private HttpProxyCacheServer newProxy(File cacheDir) {
321363
.cacheDirectory(cacheDir)
322364
.build();
323365
}
366+
367+
private void waitForAsyncTrimming() throws InterruptedException {
368+
Thread.sleep(500);
369+
}
324370
}

test/src/test/java/com/danikula/videocache/file/FileCacheTest.java

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.danikula.videocache.file;
22

33
import com.danikula.android.garden.io.Files;
4-
import com.danikula.android.garden.io.IoUtils;
54
import com.danikula.videocache.BaseTest;
65
import com.danikula.videocache.Cache;
76
import com.danikula.videocache.ProxyCacheException;
@@ -11,6 +10,7 @@
1110
import org.junit.Test;
1211

1312
import java.io.File;
13+
import java.io.IOException;
1414
import java.util.Arrays;
1515

1616
import static com.danikula.videocache.support.ProxyCacheTestUtils.ASSETS_DATA_NAME;
@@ -19,6 +19,7 @@
1919
import static com.danikula.videocache.support.ProxyCacheTestUtils.getTempFile;
2020
import static com.danikula.videocache.support.ProxyCacheTestUtils.loadAssetFile;
2121
import static com.danikula.videocache.support.ProxyCacheTestUtils.newCacheFile;
22+
import static com.google.common.io.Files.write;
2223
import static org.fest.assertions.api.Assertions.assertThat;
2324

2425
/**
@@ -95,7 +96,8 @@ public void testAppendDiscCache() throws Exception {
9596
public void testIsFileCacheCompleted() throws Exception {
9697
File file = newCacheFile();
9798
File partialFile = new File(file.getParentFile(), file.getName() + ".download");
98-
IoUtils.saveToFile(loadAssetFile(ASSETS_DATA_NAME), partialFile);
99+
write(loadAssetFile(ASSETS_DATA_NAME), partialFile);
100+
write(loadAssetFile(ASSETS_DATA_NAME), partialFile);
99101
Cache fileCache = new FileCache(partialFile);
100102

101103
assertThat(file.exists()).isFalse();
@@ -114,7 +116,7 @@ public void testIsFileCacheCompleted() throws Exception {
114116
@Test(expected = ProxyCacheException.class)
115117
public void testErrorWritingCompletedCache() throws Exception {
116118
File file = newCacheFile();
117-
IoUtils.saveToFile(loadAssetFile(ASSETS_DATA_NAME), file);
119+
write(loadAssetFile(ASSETS_DATA_NAME), file);
118120
FileCache fileCache = new FileCache(file);
119121
fileCache.append(generate(100), 20);
120122
Assert.fail();
@@ -124,7 +126,7 @@ public void testErrorWritingCompletedCache() throws Exception {
124126
public void testErrorWritingAfterCompletion() throws Exception {
125127
File file = newCacheFile();
126128
File partialFile = new File(file.getParentFile(), file.getName() + ".download");
127-
IoUtils.saveToFile(loadAssetFile(ASSETS_DATA_NAME), partialFile);
129+
write(loadAssetFile(ASSETS_DATA_NAME), partialFile);
128130
FileCache fileCache = new FileCache(partialFile);
129131
fileCache.complete();
130132
fileCache.append(generate(100), 20);
@@ -140,4 +142,45 @@ public void testFileErrorForDiscCache() throws Exception {
140142
fileCache.available();
141143
Assert.fail();
142144
}
145+
146+
@Test
147+
public void testTrimAfterCompletionForTotalCountLru() throws Exception {
148+
File cacheDir = newCacheFile();
149+
DiskUsage diskUsage = new TotalCountLruDiskUsage(2);
150+
byte[] data = loadAssetFile(ASSETS_DATA_NAME);
151+
saveAndCompleteCache(diskUsage, data,
152+
new File(cacheDir, "0.dat"),
153+
new File(cacheDir, "1.dat"),
154+
new File(cacheDir, "2.dat")
155+
);
156+
waitForAsyncTrimming();
157+
assertThat(new File(cacheDir, "0.dat")).doesNotExist();
158+
}
159+
160+
@Test
161+
public void testTrimAfterCompletionForTotalSizeLru() throws Exception {
162+
File cacheDir = newCacheFile();
163+
byte[] data = loadAssetFile(ASSETS_DATA_NAME);
164+
DiskUsage diskUsage = new TotalSizeLruDiskUsage(data.length*3-1);
165+
saveAndCompleteCache(diskUsage, data,
166+
new File(cacheDir, "0.dat"),
167+
new File(cacheDir, "1.dat"),
168+
new File(cacheDir, "2.dat")
169+
);
170+
waitForAsyncTrimming();
171+
assertThat(new File(cacheDir, "0.dat")).doesNotExist();
172+
}
173+
174+
private void saveAndCompleteCache(DiskUsage diskUsage, byte[] data, File... files) throws ProxyCacheException, IOException {
175+
for (File file : files) {
176+
FileCache fileCache = new FileCache(file, diskUsage);
177+
fileCache.append(data, data.length);
178+
fileCache.complete();
179+
assertThat(file).exists();
180+
}
181+
}
182+
183+
private void waitForAsyncTrimming() throws InterruptedException {
184+
Thread.sleep(500);
185+
}
143186
}

0 commit comments

Comments
 (0)