Skip to content

Commit

Permalink
Merge pull request square#2818 from square/jwilson.0829.update_the_cache
Browse files Browse the repository at this point in the history
Fix conditional cache hits to update timestamp fields.
  • Loading branch information
swankjesse authored Oct 6, 2016
2 parents 10360b8 + 8b680f3 commit 26d4e22
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
14 changes: 11 additions & 3 deletions okhttp-tests/src/test/java/okhttp3/CacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1834,21 +1834,29 @@ public void assertCookies(HttpUrl url, String... expectedCookies) throws Excepti
server.enqueue(new MockResponse()
.setBody("B"));

// cache miss; seed the cache
// A cache miss writes the cache.
long t0 = System.currentTimeMillis();
Response response1 = get(server.url("/a"));
assertEquals("A", response1.body().string());
assertEquals(null, response1.header("Allow"));
assertEquals(0, response1.receivedResponseAtMillis() - t0, 250.0);

// conditional cache hit; update the cache
// A conditional cache hit updates the cache.
Thread.sleep(500); // Make sure t0 and t1 are distinct.
long t1 = System.currentTimeMillis();
Response response2 = get(server.url("/a"));
assertEquals(HttpURLConnection.HTTP_OK, response2.code());
assertEquals("A", response2.body().string());
assertEquals("GET, HEAD", response2.header("Allow"));
assertEquals(0, response2.receivedResponseAtMillis() - t1, 250.0);

// full cache hit
// A full cache hit reads the cache.
Thread.sleep(500); // Make sure t1 and t2 are distinct.
long t2 = System.currentTimeMillis();
Response response3 = get(server.url("/a"));
assertEquals("A", response3.body().string());
assertEquals("GET, HEAD", response3.header("Allow"));
assertEquals(0, response3.receivedResponseAtMillis() - t1, 250.0);

assertEquals(2, server.getRequestCount());
}
Expand Down
10 changes: 5 additions & 5 deletions okhttp-tests/src/test/java/okhttp3/CallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ private void postBodyRetransmittedAfterAuthorizationFail(String body) throws Exc
.build();

// Store a response in the cache.
long request1At = System.currentTimeMillis();
long request1SentAt = System.currentTimeMillis();
executeSynchronously("/", "Accept-Language", "fr-CA", "Accept-Charset", "UTF-8")
.assertCode(200)
.assertHeader("Donut", "a")
Expand All @@ -1273,8 +1273,8 @@ private void postBodyRetransmittedAfterAuthorizationFail(String body) throws Exc
.assertRequestHeader("Accept-Language", "en-US")
.assertRequestHeader("Accept-Charset", "UTF-8")
.assertRequestHeader("If-None-Match") // No If-None-Match on the user's request.
.assertSentRequestAtMillis(request1At, request1ReceivedAt)
.assertReceivedResponseAtMillis(request1At, request1ReceivedAt);
.assertSentRequestAtMillis(request2SentAt, request2ReceivedAt)
.assertReceivedResponseAtMillis(request2SentAt, request2ReceivedAt);

// Check the cached response. Its request contains only the saved Vary headers.
cacheHit.cacheResponse()
Expand All @@ -1285,8 +1285,8 @@ private void postBodyRetransmittedAfterAuthorizationFail(String body) throws Exc
.assertRequestHeader("Accept-Language") // No Vary on Accept-Language.
.assertRequestHeader("Accept-Charset", "UTF-8") // Because of Vary on Accept-Charset.
.assertRequestHeader("If-None-Match") // This wasn't present in the original request.
.assertSentRequestAtMillis(request1At, request1ReceivedAt)
.assertReceivedResponseAtMillis(request1At, request1ReceivedAt);
.assertSentRequestAtMillis(request1SentAt, request1ReceivedAt)
.assertReceivedResponseAtMillis(request1SentAt, request1ReceivedAt);

// Check the network response. It has the caller's request, plus some caching headers.
cacheHit.networkResponse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public CacheInterceptor(InternalCache cache) {
if (validate(cacheResponse, networkResponse)) {
Response response = cacheResponse.newBuilder()
.headers(combine(cacheResponse.headers(), networkResponse.headers()))
.sentRequestAtMillis(networkResponse.sentRequestAtMillis())
.receivedResponseAtMillis(networkResponse.receivedResponseAtMillis())
.cacheResponse(stripBody(cacheResponse))
.networkResponse(stripBody(networkResponse))
.build();
Expand Down

0 comments on commit 26d4e22

Please sign in to comment.