Skip to content

Commit

Permalink
Close and delete cache when test finishes
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman committed Nov 2, 2021
1 parent 9e52a57 commit 40dd984
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.kohsuke.github.AbstractGitHubWireMockTest;
Expand Down Expand Up @@ -67,6 +68,7 @@ public OkHttpConnectorTest() {
private static int maxAgeNoneHitCount = 11;

private GHRateLimit rateLimitBefore;
private Cache cache = null;

@Override
protected WireMockConfiguration getWireMockOptions() {
Expand All @@ -88,6 +90,13 @@ public void setupRepo() throws Exception {
}
}

@After
public void deleteCache() throws IOException {
if (cache != null) {
cache.delete();
}
}

@Test
public void DefaultConnector() throws Exception {

Expand Down Expand Up @@ -120,7 +129,6 @@ public void OkHttpConnector_NoCache() throws Exception {

checkRequestAndLimit(okhttpNetworkRequestCount, okhttpRateLimitUsed);

Cache cache = client.cache();
assertThat("Cache", cache, is(nullValue()));
}

Expand All @@ -147,8 +155,6 @@ public void OkHttpConnector_Cache_MaxAgeNone() throws Exception {

checkRequestAndLimit(maxAgeNoneNetworkRequestCount, maxAgeNoneRateLimitUsed);

Cache cache = client.cache();

// NOTE: this is actually bad.
// This elevated hit count is the stale requests returning bad data took longer to detect a change.
assertThat("getHitCount", cache.hitCount(), is(maxAgeNoneHitCount));
Expand Down Expand Up @@ -176,7 +182,6 @@ public void OkHttpConnector_Cache_MaxAge_Three() throws Exception {

checkRequestAndLimit(maxAgeThreeNetworkRequestCount, maxAgeThreeRateLimitUsed);

Cache cache = client.cache();
assertThat("getHitCount", cache.hitCount(), is(maxAgeThreeHitCount));
}

Expand All @@ -202,7 +207,6 @@ public void OkHttpConnector_Cache_MaxAgeDefault_Zero() throws Exception {

checkRequestAndLimit(maxAgeZeroNetworkRequestCount, maxAgeZeroRateLimitUsed);

Cache cache = client.cache();
assertThat("getHitCount", cache.hitCount(), is(maxAgeZeroHitCount));
}

Expand All @@ -228,7 +232,7 @@ private OkHttpClient createClient(boolean useCache) throws IOException {
File cacheDir = new File("target/cache/" + baseFilesClassPath + "/" + mockGitHub.getMethodName());
cacheDir.mkdirs();
FileUtils.cleanDirectory(cacheDir);
Cache cache = new Cache(cacheDir, 100 * 1024L * 1024L);
cache = new Cache(cacheDir, 100 * 1024L * 1024L);

builder.cache(cache);
}
Expand Down

0 comments on commit 40dd984

Please sign in to comment.