### Eclipse Workspace Patch 1.0 #P httpcomponents-client Index: httpclient-cache/src/test/resources/commons-logging.properties =================================================================== --- httpclient-cache/src/test/resources/commons-logging.properties (revision 0) +++ httpclient-cache/src/test/resources/commons-logging.properties (revision 0) @@ -0,0 +1,2 @@ +# Turn off logging for unit test runs +org.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog \ No newline at end of file Index: httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java =================================================================== --- httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java (revision 1021885) +++ httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java (working copy) @@ -1584,7 +1584,7 @@ Assert.assertEquals(HttpStatus.SC_NOT_MODIFIED, result.getStatusLine().getStatusCode()); } - + @Test public void testReturns200ForIfModifiedSinceFailsIfRequestServedFromOrigin() throws Exception { @@ -1635,6 +1635,32 @@ 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); } Index: httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java =================================================================== --- httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java (revision 1021885) +++ httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java (working copy) @@ -386,13 +386,22 @@ } 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 @@ 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;