Propagate TimeoutException when possible

This allows clients to recover from transient errors.
This commit is contained in:
Andrew Gaul 2012-06-25 22:54:05 -07:00 committed by Andrew Gaul
parent 1edbb0ab4b
commit a66c146a54
2 changed files with 34 additions and 2 deletions

View File

@ -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<K, V> 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;
}
}
});
}

View File

@ -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);
}
}
@Test
void testThrowsTimeoutException() throws Exception {
int attempts = 3;
BackoffExponentiallyAndRetryOnThrowableCacheLoader<String, Boolean> backoff = new BackoffExponentiallyAndRetryOnThrowableCacheLoader<String, Boolean>(
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);
}
}