From a66c146a54e0ebd7e430e9b713fa0b3d50b21907 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Mon, 25 Jun 2012 22:54:05 -0700 Subject: [PATCH] Propagate TimeoutException when possible This allows clients to recover from transient errors. --- ...ntiallyAndRetryOnThrowableCacheLoader.java | 13 ++++++++++- ...llyAndRetryOnThrowableCacheLoaderTest.java | 23 ++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/jclouds/cache/internal/BackoffExponentiallyAndRetryOnThrowableCacheLoader.java b/core/src/main/java/org/jclouds/cache/internal/BackoffExponentiallyAndRetryOnThrowableCacheLoader.java index 967a046143..abdecf8c94 100644 --- a/core/src/main/java/org/jclouds/cache/internal/BackoffExponentiallyAndRetryOnThrowableCacheLoader.java +++ b/core/src/main/java/org/jclouds/cache/internal/BackoffExponentiallyAndRetryOnThrowableCacheLoader.java @@ -23,8 +23,10 @@ import static com.google.common.base.Preconditions.checkNotNull; import java.util.Map; import java.util.concurrent.Callable; +import java.util.concurrent.TimeoutException; import org.jclouds.cache.ForwardingCacheLoader; +import org.jclouds.util.Throwables2; import com.google.common.annotations.Beta; import com.google.common.cache.CacheLoader; @@ -93,7 +95,16 @@ public class BackoffExponentiallyAndRetryOnThrowableCacheLoader extends Fo @Override public V call() throws Exception { - return BackoffExponentiallyAndRetryOnThrowableCacheLoader.super.load(key); + try { + return BackoffExponentiallyAndRetryOnThrowableCacheLoader.super.load(key); + } catch (Exception e) { + TimeoutException te = Throwables2.getFirstThrowableOfType(e, + TimeoutException.class); + if (te != null) { + throw te; + } + throw e; + } } }); } diff --git a/core/src/test/java/org/jclouds/cache/internal/BackoffExponentiallyAndRetryOnThrowableCacheLoaderTest.java b/core/src/test/java/org/jclouds/cache/internal/BackoffExponentiallyAndRetryOnThrowableCacheLoaderTest.java index 0b0517aef2..01c7628989 100644 --- a/core/src/test/java/org/jclouds/cache/internal/BackoffExponentiallyAndRetryOnThrowableCacheLoaderTest.java +++ b/core/src/test/java/org/jclouds/cache/internal/BackoffExponentiallyAndRetryOnThrowableCacheLoaderTest.java @@ -25,6 +25,9 @@ import static org.easymock.EasyMock.verify; import static org.testng.Assert.assertSame; import static org.testng.Assert.assertTrue; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; + import org.jclouds.rest.ResourceNotFoundException; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -73,4 +76,22 @@ public class BackoffExponentiallyAndRetryOnThrowableCacheLoaderTest { } verify(mock); } -} \ No newline at end of file + + @Test + void testThrowsTimeoutException() throws Exception { + int attempts = 3; + BackoffExponentiallyAndRetryOnThrowableCacheLoader backoff = new BackoffExponentiallyAndRetryOnThrowableCacheLoader( + ResourceNotFoundException.class, 50l, 500l, attempts, mock); + + expect(mock.load("foo")).andThrow(new ExecutionException(new TimeoutException())); + + replay(mock); + try { + backoff.load("foo"); + assertTrue(false); + } catch (TimeoutException e) { + + } + verify(mock); + } +}