hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1022965 - in /httpcomponents/httpclient/trunk/httpclient-cache/src: main/java/org/apache/http/impl/client/cache/CachingHttpClient.java test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java
Date Fri, 15 Oct 2010 15:20:10 GMT
Author: olegk
Date: Fri Oct 15 15:20:10 2010
New Revision: 1022965

URL: http://svn.apache.org/viewvc?rev=1022965&view=rev
Log:
HTTPCLIENT-1011: Handling of IOExceptions thrown by cache components
Contributed by Jonathan Moore <jonathan_moore at comcast.com>

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

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=1022965&r1=1022964&r2=1022965&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
Fri Oct 15 15:20:10 2010
@@ -386,13 +386,22 @@ public class CachingHttpClient implement
         }
         request.addHeader("Via",via);
 
-        responseCache.flushInvalidatedCacheEntriesFor(target, request);
+        try {
+            responseCache.flushInvalidatedCacheEntriesFor(target, request);
+        } catch (IOException ioe) {
+            log.warn("Unable to flush invalidated entries from cache", ioe);
+        }
 
         if (!cacheableRequestPolicy.isServableFromCache(request)) {
             return callBackend(target, request, context);
         }
 
-        HttpCacheEntry entry = responseCache.getCacheEntry(target, request);
+        HttpCacheEntry entry = null;
+        try {
+            entry = responseCache.getCacheEntry(target, request);
+        } catch (IOException ioe) {
+            log.warn("Unable to retrieve entries from cache", ioe);
+        }
         if (entry == null) {
             cacheMisses.getAndIncrement();
             if (log.isDebugEnabled()) {
@@ -599,18 +608,31 @@ public class CachingHttpClient implement
         boolean cacheable = responseCachingPolicy.isResponseCacheable(request, backendResponse);
         if (cacheable &&
             !alreadyHaveNewerCacheEntry(target, request, backendResponse)) {
-            return responseCache.cacheAndReturnResponse(target, request, backendResponse,
requestDate,
-                    responseDate);
+            try {
+                return responseCache.cacheAndReturnResponse(target, request, backendResponse,
requestDate,
+                        responseDate);
+            } catch (IOException ioe) {
+                log.warn("Unable to store entries in cache", ioe);
+            }
         }
         if (!cacheable) {
-            responseCache.flushCacheEntriesFor(target, request);
+            try {
+                responseCache.flushCacheEntriesFor(target, request);
+            } catch (IOException ioe) {
+                log.warn("Unable to flush invalid cache entries", ioe);
+            }
         }
         return backendResponse;
     }
 
     private boolean alreadyHaveNewerCacheEntry(HttpHost target, HttpRequest request,
             HttpResponse backendResponse) throws IOException {
-        HttpCacheEntry existing = responseCache.getCacheEntry(target, request);
+        HttpCacheEntry existing = null;
+        try {
+            existing = responseCache.getCacheEntry(target, request);
+        } catch (IOException ioe) {
+            // nop
+        }
         if (existing == null) return false;
         Header entryDateHeader = existing.getFirstHeader("Date");
         if (entryDateHeader == null) return false;

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=1022965&r1=1022964&r2=1022965&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
Fri Oct 15 15:20:10 2010
@@ -1635,6 +1635,32 @@ public class TestCachingHttpClient {
         Assert.assertTrue(impl.isSharedCache());
     }
 
+    @Test
+    public void testTreatsCacheIOExceptionsAsCacheMiss()
+        throws Exception {
+
+        impl = new CachingHttpClient(mockBackend, mockCache);
+        HttpResponse resp = HttpTestUtils.make200Response();
+
+        mockCache.flushInvalidatedCacheEntriesFor(host, request);
+        EasyMock.expectLastCall().andThrow(new IOException()).anyTimes();
+        EasyMock.expect(mockCache.getCacheEntry(EasyMock.same(host),
+                EasyMock.isA(HttpRequest.class)))
+            .andThrow(new IOException()).anyTimes();
+        EasyMock.expect(mockCache.cacheAndReturnResponse(EasyMock.same(host),
+                EasyMock.isA(HttpRequest.class), EasyMock.isA(HttpResponse.class),
+                EasyMock.isA(Date.class), EasyMock.isA(Date.class)))
+            .andThrow(new IOException()).anyTimes();
+        EasyMock.expect(mockBackend.execute(EasyMock.same(host),
+                EasyMock.isA(HttpRequest.class), (HttpContext)EasyMock.isNull()))
+            .andReturn(resp);
+
+        replayMocks();
+        HttpResponse result = impl.execute(host, request);
+        verifyMocks();
+        Assert.assertSame(resp, result);
+    }
+
     private void getCacheEntryReturns(HttpCacheEntry result) throws IOException {
         EasyMock.expect(mockCache.getCacheEntry(host, request)).andReturn(result);
     }



Mime
View raw message