hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1051235 - in /httpcomponents/httpclient/trunk/httpclient-cache/src: main/java/org/apache/http/impl/client/cache/ test/java/org/apache/http/impl/client/cache/
Date Mon, 20 Dec 2010 18:30:46 GMT
Author: jonm
Date: Mon Dec 20 18:30:46 2010
New Revision: 1051235

URL: http://svn.apache.org/viewvc?rev=1051235&view=rev
Log:
HTTPCLIENT-975: stale-while-revalidate also yields to must-revalidate,
proxy-revalidate on a shared cache, and requests with explicit
freshness constraints.

Modified:
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestRFC5861Compliance.java

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java?rev=1051235&r1=1051234&r2=1051235&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java
Mon Dec 20 18:30:46 2010
@@ -430,7 +430,9 @@ public class CachingHttpClient implement
         log.debug("Revalidating the cache entry");
 
         try {
-            if (asynchRevalidator != null && validityPolicy.mayReturnStaleWhileRevalidating(entry,
now)) {
+            if (asynchRevalidator != null
+                && !staleResponseNotAllowed(request, entry, now)
+                && validityPolicy.mayReturnStaleWhileRevalidating(entry, now)) {
                 final HttpResponse resp = responseGenerator.generateResponse(entry);
                 resp.addHeader(HeaderConstants.WARNING, "110 localhost \"Response is stale\"");
                 

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java?rev=1051235&r1=1051234&r2=1051235&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java
Mon Dec 20 18:30:46 2010
@@ -351,6 +351,8 @@ public class TestCachingHttpClient {
         getCacheEntryReturns(mockCacheEntry);
         cacheEntrySuitable(false);
         cacheEntryValidatable(true);
+        cacheEntryMustRevalidate(false);
+        cacheEntryProxyRevalidate(false);
         mayReturnStaleWhileRevalidating(false);
         revalidateCacheEntryReturns(mockBackendResponse);
 
@@ -1944,6 +1946,16 @@ public class TestCachingHttpClient {
                 EasyMock.<HttpCacheEntry>anyObject())).andReturn(b);
     }
     
+    private void cacheEntryMustRevalidate(boolean b) {
+        EasyMock.expect(mockValidityPolicy.mustRevalidate(mockCacheEntry))
+            .andReturn(b);
+    }
+
+    private void cacheEntryProxyRevalidate(boolean b) {
+        EasyMock.expect(mockValidityPolicy.proxyRevalidate(mockCacheEntry))
+            .andReturn(b);
+    }
+    
     private void mayReturnStaleWhileRevalidating(boolean b) {
         EasyMock.expect(mockValidityPolicy.mayReturnStaleWhileRevalidating(
                 EasyMock.<HttpCacheEntry>anyObject(), EasyMock.<Date>anyObject())).andReturn(b);

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestRFC5861Compliance.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestRFC5861Compliance.java?rev=1051235&r1=1051234&r2=1051235&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestRFC5861Compliance.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestRFC5861Compliance.java
Mon Dec 20 18:30:46 2010
@@ -300,4 +300,140 @@ public class TestRFC5861Compliance exten
         }
         assertTrue(warning110Found);
     }
+    
+    @Test
+    public void testStaleWhileRevalidateYieldsToMustRevalidate()
+        throws Exception {
+        
+        Date now = new Date();
+        Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
+        
+        params.setAsynchronousWorkersMax(1);
+        impl = new CachingHttpClient(mockBackend, cache, params);
+        
+        HttpRequest req1 = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
+        HttpResponse resp1 = HttpTestUtils.make200Response();
+        resp1.setHeader("Cache-Control", "public, max-age=5, stale-while-revalidate=15, must-revalidate");
+        resp1.setHeader("ETag","\"etag\"");
+        resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
+
+        backendExpectsAnyRequest().andReturn(resp1);
+
+        HttpRequest req2 = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
+        HttpResponse resp2 = HttpTestUtils.make200Response();
+        resp2.setHeader("Cache-Control", "public, max-age=5, stale-while-revalidate=15, must-revalidate");
+        resp2.setHeader("ETag","\"etag\"");
+        resp2.setHeader("Date", DateUtils.formatDate(now));
+        
+        backendExpectsAnyRequest().andReturn(resp2);
+
+        replayMocks();
+        impl.execute(host, req1);
+        HttpResponse result = impl.execute(host, req2);
+        verifyMocks();
+
+        assertEquals(HttpStatus.SC_OK, result.getStatusLine().getStatusCode());
+        boolean warning110Found = false;
+        for(Header h : result.getHeaders("Warning")) {
+            for(WarningValue wv : WarningValue.getWarningValues(h)) {
+                if (wv.getWarnCode() == 110) {
+                    warning110Found = true;
+                    break;
+                }
+            }
+        }
+        assertFalse(warning110Found);
+    }
+
+    @Test
+    public void testStaleWhileRevalidateYieldsToProxyRevalidateForSharedCache()
+        throws Exception {
+        
+        Date now = new Date();
+        Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
+        
+        params.setAsynchronousWorkersMax(1);
+        params.setSharedCache(true);
+        impl = new CachingHttpClient(mockBackend, cache, params);
+        
+        HttpRequest req1 = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
+        HttpResponse resp1 = HttpTestUtils.make200Response();
+        resp1.setHeader("Cache-Control", "public, max-age=5, stale-while-revalidate=15, proxy-revalidate");
+        resp1.setHeader("ETag","\"etag\"");
+        resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
+
+        backendExpectsAnyRequest().andReturn(resp1);
+
+        HttpRequest req2 = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
+        HttpResponse resp2 = HttpTestUtils.make200Response();
+        resp2.setHeader("Cache-Control", "public, max-age=5, stale-while-revalidate=15, proxy-revalidate");
+        resp2.setHeader("ETag","\"etag\"");
+        resp2.setHeader("Date", DateUtils.formatDate(now));
+        
+        backendExpectsAnyRequest().andReturn(resp2);
+
+        replayMocks();
+        impl.execute(host, req1);
+        HttpResponse result = impl.execute(host, req2);
+        verifyMocks();
+
+        assertEquals(HttpStatus.SC_OK, result.getStatusLine().getStatusCode());
+        boolean warning110Found = false;
+        for(Header h : result.getHeaders("Warning")) {
+            for(WarningValue wv : WarningValue.getWarningValues(h)) {
+                if (wv.getWarnCode() == 110) {
+                    warning110Found = true;
+                    break;
+                }
+            }
+        }
+        assertFalse(warning110Found);
+    }
+
+    @Test
+    public void testStaleWhileRevalidateYieldsToExplicitFreshnessRequest()
+        throws Exception {
+        
+        Date now = new Date();
+        Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
+        
+        params.setAsynchronousWorkersMax(1);
+        params.setSharedCache(true);
+        impl = new CachingHttpClient(mockBackend, cache, params);
+        
+        HttpRequest req1 = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
+        HttpResponse resp1 = HttpTestUtils.make200Response();
+        resp1.setHeader("Cache-Control", "public, max-age=5, stale-while-revalidate=15");
+        resp1.setHeader("ETag","\"etag\"");
+        resp1.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
+
+        backendExpectsAnyRequest().andReturn(resp1);
+
+        HttpRequest req2 = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
+        req2.setHeader("Cache-Control","min-fresh=2");
+        HttpResponse resp2 = HttpTestUtils.make200Response();
+        resp2.setHeader("Cache-Control", "public, max-age=5, stale-while-revalidate=15");
+        resp2.setHeader("ETag","\"etag\"");
+        resp2.setHeader("Date", DateUtils.formatDate(now));
+        
+        backendExpectsAnyRequest().andReturn(resp2);
+
+        replayMocks();
+        impl.execute(host, req1);
+        HttpResponse result = impl.execute(host, req2);
+        verifyMocks();
+
+        assertEquals(HttpStatus.SC_OK, result.getStatusLine().getStatusCode());
+        boolean warning110Found = false;
+        for(Header h : result.getHeaders("Warning")) {
+            for(WarningValue wv : WarningValue.getWarningValues(h)) {
+                if (wv.getWarnCode() == 110) {
+                    warning110Found = true;
+                    break;
+                }
+            }
+        }
+        assertFalse(warning110Found);
+    }
+    
 }



Mime
View raw message