hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1230174 - in /httpcomponents/httpclient/trunk: ./ httpclient-cache/src/main/java/org/apache/http/impl/client/cache/memcached/ httpclient-cache/src/test/java/org/apache/http/impl/client/cache/memcached/
Date Wed, 11 Jan 2012 18:12:51 GMT
Author: jonm
Date: Wed Jan 11 18:12:50 2012
New Revision: 1230174

URL: http://svn.apache.org/viewvc?rev=1230174&view=rev
Log:
HTTPCLIENT-1157: MemcachedHttpCacheStorage should throw IOExceptions instead of RuntimeExceptions.
Contributed by James Miller <jamesmiller01 at gmail dot com>.

Modified:
    httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/memcached/MemcachedHttpCacheStorage.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/memcached/TestMemcachedHttpCacheStorage.java

Modified: httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt?rev=1230174&r1=1230173&r2=1230174&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/trunk/RELEASE_NOTES.txt Wed Jan 11 18:12:50 2012
@@ -1,5 +1,9 @@
 Changes since 4.2 ALPHA1
 -------------------
+* [HTTPCLIENT-1157] MemcachedHttpCacheStroage should throw IOExceptions
+  instead of RuntimeExceptions.
+  Contributed by James Miller <jamesmiller01 at gmail dot com>.
+  
 * [HTTPCLIENT-1152] MemcachedHttpCacheStorage should verify class of
   returned object before casting.
   Contributed by Rajika Kumarasiri <rajika at wso2 dot com>.

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/memcached/MemcachedHttpCacheStorage.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/memcached/MemcachedHttpCacheStorage.java?rev=1230174&r1=1230173&r2=1230174&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/memcached/MemcachedHttpCacheStorage.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/memcached/MemcachedHttpCacheStorage.java
Wed Jan 11 18:12:50 2012
@@ -36,6 +36,7 @@ import net.spy.memcached.CASResponse;
 import net.spy.memcached.CASValue;
 import net.spy.memcached.MemcachedClient;
 import net.spy.memcached.MemcachedClientIF;
+import net.spy.memcached.OperationTimeoutException;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -124,7 +125,11 @@ public class MemcachedHttpCacheStorage i
     public void putEntry(String url, HttpCacheEntry entry) throws IOException  {
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         serializer.writeTo(entry, bos);
-        client.set(url, 0, bos.toByteArray());
+        try {
+            client.set(url, 0, bos.toByteArray());
+        } catch (OperationTimeoutException ex) {
+            throw new IOException(ex);
+        }
     }
 
     private byte[] convertToByteArray(Object o) {
@@ -144,36 +149,47 @@ public class MemcachedHttpCacheStorage i
     }
     
     public HttpCacheEntry getEntry(String url) throws IOException {
-        return reconstituteEntry(client.get(url));
+        try {
+            return reconstituteEntry(client.get(url));
+        } catch (OperationTimeoutException ex) {
+            throw new IOException(ex);
+        }
     }
 
     public void removeEntry(String url) throws IOException {
-        client.delete(url);
+        try {
+            client.delete(url);
+        } catch (OperationTimeoutException ex) {
+            throw new IOException(ex);
+        }
     }
 
     public void updateEntry(String url, HttpCacheUpdateCallback callback)
             throws HttpCacheUpdateException, IOException {
         int numRetries = 0;
         do {
-
-            CASValue<Object> v = client.gets(url);
-            HttpCacheEntry existingEntry = (v == null) ? null
-                    : reconstituteEntry(v.getValue());
-            HttpCacheEntry updatedEntry = callback.update(existingEntry);
-
-            if (v == null) {
-                putEntry(url, updatedEntry);
-                return;
-
-            } else {
-                ByteArrayOutputStream bos = new ByteArrayOutputStream();
-                serializer.writeTo(updatedEntry, bos);
-                CASResponse casResult = client.cas(url, v.getCas(), bos.toByteArray());
-                if (casResult != CASResponse.OK) {
-                    numRetries++;
-                } else return;
+            try {
+                CASValue<Object> v = client.gets(url);
+                HttpCacheEntry existingEntry = (v == null) ? null
+                        : reconstituteEntry(v.getValue());
+                HttpCacheEntry updatedEntry = callback.update(existingEntry);
+
+                if (v == null) {
+                    putEntry(url, updatedEntry);
+                    return;
+
+                } else {
+                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
+                    serializer.writeTo(updatedEntry, bos);
+                    CASResponse casResult = client.cas(url, v.getCas(),
+                            bos.toByteArray());
+                    if (casResult != CASResponse.OK) {
+                        numRetries++;
+                    } else return;
+                }
+            } catch (OperationTimeoutException ex) {
+                throw new IOException(ex);
             }
-
         } while (numRetries <= maxUpdateRetries);
 
         throw new HttpCacheUpdateException("Failed to update");

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/memcached/TestMemcachedHttpCacheStorage.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/memcached/TestMemcachedHttpCacheStorage.java?rev=1230174&r1=1230173&r2=1230174&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/memcached/TestMemcachedHttpCacheStorage.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/memcached/TestMemcachedHttpCacheStorage.java
Wed Jan 11 18:12:50 2012
@@ -35,6 +35,7 @@ import junit.framework.TestCase;
 import net.spy.memcached.CASResponse;
 import net.spy.memcached.CASValue;
 import net.spy.memcached.MemcachedClientIF;
+import net.spy.memcached.OperationTimeoutException;
 
 import org.apache.http.client.cache.HttpCacheEntry;
 import org.apache.http.client.cache.HttpCacheEntrySerializer;
@@ -102,6 +103,22 @@ public class TestMemcachedHttpCacheStora
     }
 
     @Test
+    public void testCacheGetThrowsTimeoutException()
+            throws UnsupportedEncodingException, IOException {
+        final String url = "foo";
+        EasyMock.expect(mockMemcachedClient.get(url)).andThrow(
+                new OperationTimeoutException("op timeout"));
+        replayMocks();
+        try {
+            impl.getEntry(url);
+            fail("IOException not thrown");
+        } catch (IOException ex) {
+            assertTrue(ex instanceof IOException);
+        }
+        verifyMocks();
+    }
+
+    @Test
     public void testCacheGetNullEntry() throws IOException {
         final String url = "foo";
 
@@ -187,6 +204,44 @@ public class TestMemcachedHttpCacheStora
     }
 
     @Test
+    public void testCacheUpdateThrowsTimeoutException() throws IOException, HttpCacheUpdateException
{
+        final String url = "foo";
+        final HttpCacheEntry existingValue = HttpTestUtils.makeCacheEntry();
+        final HttpCacheEntry updatedValue = HttpTestUtils.makeCacheEntry();
+
+        CASValue<Object> v = new CASValue<Object>(1234, new byte[] {});
+
+        HttpCacheUpdateCallback callback = new HttpCacheUpdateCallback() {
+            public HttpCacheEntry update(HttpCacheEntry old) {
+                assertEquals(existingValue, old);
+                return updatedValue;
+            }
+        };
+
+        // get existing old entry
+        EasyMock.expect(mockMemcachedClient.gets(url)).andReturn(v);
+        EasyMock.expect(
+                mockSerializer.readFrom(EasyMock.isA(InputStream.class)))
+                .andReturn(existingValue);
+
+        // update
+        EasyMock.expect(
+                mockMemcachedClient.cas(EasyMock.eq(url), EasyMock.eq(v
+                        .getCas()), EasyMock.aryEq(new byte[0]))).andThrow(
+                new OperationTimeoutException("op timeout"));
+        mockSerializer.writeTo(EasyMock.same(updatedValue), EasyMock
+                .isA(OutputStream.class));
+        replayMocks();
+        try {
+            impl.updateEntry(url, callback);
+            fail("IOException not thrown");
+        } catch(IOException ex) {
+            assertTrue(ex instanceof IOException);
+        }
+        verifyMocks();
+    }
+
+    @Test
     public void testSingleCacheUpdateRetry() throws IOException,
             HttpCacheUpdateException {
         final String url = "foo";
@@ -264,4 +319,45 @@ public class TestMemcachedHttpCacheStora
         verifyMocks();
     }
 
+    @Test
+    public void testCachePutThrowsIOExceptionOnTimeout() throws Exception {
+        final String url = "foo";
+        final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
+        impl = new MemcachedHttpCacheStorage(mockMemcachedClient);
+        
+        EasyMock.expect(mockMemcachedClient.set(EasyMock.eq(url), EasyMock.anyInt(), EasyMock.anyObject()))
+            .andThrow(new OperationTimeoutException(url));
+        
+        replayMocks();
+        boolean sawIOException = false;
+        try {
+            impl.putEntry(url, entry);
+            fail("should have thrown exception");
+        } catch (IOException expected) {
+            sawIOException = true;
+        }
+        assertTrue(sawIOException);
+        verifyMocks();
+    }
+    
+    @Test
+    public void testCacheDeleteThrowsIOExceptionOnTimeout() throws Exception {
+        final String url = "foo";
+        impl = new MemcachedHttpCacheStorage(mockMemcachedClient);
+        
+        EasyMock.expect(mockMemcachedClient.delete(url))
+            .andThrow(new OperationTimeoutException(url));
+        
+        replayMocks();
+        boolean sawIOException = false;
+        try {
+            impl.removeEntry(url);
+            fail("should have thrown exception");
+        } catch (IOException expected) {
+            sawIOException = true;
+        }
+        assertTrue(sawIOException);
+        verifyMocks();
+    }
+
 }



Mime
View raw message