Merge pull request #689 from andrewgaul/propagate-timeout-exception

Propagate TimeoutException when possible
This commit is contained in:
Adrian Cole 2012-10-04 14:46:28 -07:00
commit 4e32001b84
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.Map;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.TimeoutException;
import org.jclouds.cache.ForwardingCacheLoader; import org.jclouds.cache.ForwardingCacheLoader;
import org.jclouds.util.Throwables2;
import com.google.common.annotations.Beta; import com.google.common.annotations.Beta;
import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheLoader;
@ -93,7 +95,16 @@ public class BackoffExponentiallyAndRetryOnThrowableCacheLoader<K, V> extends Fo
@Override @Override
public V call() throws Exception { 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.assertSame;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import org.jclouds.rest.ResourceNotFoundException; import org.jclouds.rest.ResourceNotFoundException;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -73,4 +76,22 @@ public class BackoffExponentiallyAndRetryOnThrowableCacheLoaderTest {
} }
verify(mock); 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);
}
}