Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public Cache API and test #6550

Merged
merged 1 commit into from
Apr 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions okhttp/src/main/kotlin/okhttp3/Cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ import java.util.TreeSet
* [rfc_7234]: http://tools.ietf.org/html/rfc7234
*/
@OptIn(ExperimentalFileSystem::class)
class Cache
internal constructor(
class Cache(
directory: Path,
maxSize: Long,
fileSystem: FileSystem
Expand Down
52 changes: 51 additions & 1 deletion okhttp/src/test/java/okhttp3/CacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package okhttp3;

import java.io.File;
import java.io.IOException;
import java.net.CookieManager;
import java.net.HttpURLConnection;
Expand All @@ -25,6 +24,7 @@
import java.security.cert.Certificate;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
Expand All @@ -44,6 +44,8 @@
import okio.Buffer;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.FileSystem;
import okio.ForwardingFileSystem;
import okio.GzipSink;
import okio.Okio;
import okio.Path;
Expand Down Expand Up @@ -84,6 +86,7 @@ public final class CacheTest {
platform.assumeNotBouncyCastle();

server.setProtocolNegotiationEnabled(false);
fileSystem.emulateUnix();
cache = new Cache(Path.get("/cache/"), Integer.MAX_VALUE, fileSystem);
client = clientTestRule.newClientBuilder()
.cache(cache)
Expand Down Expand Up @@ -2610,6 +2613,53 @@ private RecordedRequest assertConditionallyCached(MockResponse response) throws
lastModifiedDate);
}

@Test
public void testPublicPathConstructor() throws IOException {
List<String> events = new ArrayList<>();

fileSystem.createDirectories(cache.directoryPath());

fileSystem.createDirectories(cache.directoryPath());

FileSystem loggingFileSystem = new ForwardingFileSystem(fileSystem) {
@Override
public Path onPathParameter(Path path, java.lang.String functionName, java.lang.String parameterName) {
events.add(functionName + ":" + path);
return path;
}

@Override
public Path onPathResult(Path path, java.lang.String functionName) {
events.add(functionName + ":" + path);
return path;
}
};
Path path = Path.get("/cache");
Cache c = new Cache(path, 100000L, loggingFileSystem);

assertThat(c.directoryPath()).isEqualTo(path);

c.size();

assertThat(events).containsExactly("metadataOrNull:/cache/journal.bkp",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it.

"metadataOrNull:/cache",
"sink:/cache/journal.bkp",
"delete:/cache/journal.bkp",
"metadataOrNull:/cache/journal",
"metadataOrNull:/cache",
"sink:/cache/journal.tmp",
"metadataOrNull:/cache/journal",
"atomicMove:/cache/journal.tmp",
"atomicMove:/cache/journal",
"appendingSink:/cache/journal");

events.clear();

c.size();

assertThat(events).isEmpty();
}

private void assertFullyCached(MockResponse response) throws Exception {
server.enqueue(response.setBody("A"));
server.enqueue(response.setBody("B"));
Expand Down