Skip to content

Commit

Permalink
Reverted ExceptionHandler rename.
Browse files Browse the repository at this point in the history
  • Loading branch information
vam-google committed Mar 21, 2017
1 parent f9a0b1f commit d2c24a9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 50 deletions.
49 changes: 24 additions & 25 deletions google-cloud-core/src/main/java/com/google/cloud/BaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.google.cloud;

import com.google.cloud.HttpExceptionRetryAlgorithm.Interceptor;
import com.google.cloud.ExceptionHandler.Interceptor;

/**
* Base class for service objects.
Expand All @@ -26,37 +26,36 @@
public abstract class BaseService<OptionsT extends ServiceOptions<?, OptionsT>>
implements Service<OptionsT> {

public static final Interceptor EXCEPTION_HANDLER_INTERCEPTOR =
new Interceptor() {

private static final long serialVersionUID = -8429573486870467828L;

@Override
public RetryResult afterEval(Exception exception, RetryResult retryResult) {
return Interceptor.RetryResult.CONTINUE_EVALUATION;
}

@Override
public RetryResult beforeEval(Exception exception) {
if (exception instanceof BaseServiceException) {
boolean retriable = ((BaseServiceException) exception).isRetryable();
return retriable ? Interceptor.RetryResult.RETRY : Interceptor.RetryResult.NO_RETRY;
}
return Interceptor.RetryResult.CONTINUE_EVALUATION;
}
};
public static final HttpExceptionRetryAlgorithm EXCEPTION_HANDLER =
HttpExceptionRetryAlgorithm.newBuilder()
.abortOn(RuntimeException.class)
.addInterceptors(EXCEPTION_HANDLER_INTERCEPTOR)
.build();
public static final Interceptor EXCEPTION_HANDLER_INTERCEPTOR = new Interceptor() {

private static final long serialVersionUID = -8429573486870467828L;

@Override
public RetryResult afterEval(Exception exception, RetryResult retryResult) {
return Interceptor.RetryResult.CONTINUE_EVALUATION;
}

@Override
public RetryResult beforeEval(Exception exception) {
if (exception instanceof BaseServiceException) {
boolean retriable = ((BaseServiceException) exception).isRetryable();
return retriable ? Interceptor.RetryResult.RETRY : Interceptor.RetryResult.NO_RETRY;
}
return Interceptor.RetryResult.CONTINUE_EVALUATION;
}
};
public static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.newBuilder()
.abortOn(RuntimeException.class)
.addInterceptors(EXCEPTION_HANDLER_INTERCEPTOR)
.build();

private final OptionsT options;

protected BaseService(OptionsT options) {
this.options = options;
}


@Override
public OptionsT getOptions() {
return options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
/**
* Exception retry algorithm implementation used by {@link RetryHelper}.
*/
public final class HttpExceptionRetryAlgorithm implements ExceptionRetryAlgorithm, Serializable {
public final class ExceptionHandler implements ExceptionRetryAlgorithm, Serializable {

private static final long serialVersionUID = -2460707015779532919L;

private static final HttpExceptionRetryAlgorithm DEFAULT_INSTANCE =
private static final ExceptionHandler DEFAULT_INSTANCE =
newBuilder().retryOn(Exception.class).abortOn(RuntimeException.class).build();

private final ImmutableList<Interceptor> interceptors;
Expand Down Expand Up @@ -76,7 +76,7 @@ enum RetryResult {
}

/**
* HttpExceptionRetryAlgorithm builder.
* ExceptionHandler builder.
*/
public static class Builder {

Expand Down Expand Up @@ -132,10 +132,10 @@ public final Builder abortOn(Class<? extends Exception>... exceptions) {
}

/**
* Returns a new HttpExceptionRetryAlgorithm instance.
* Returns a new ExceptionHandler instance.
*/
public HttpExceptionRetryAlgorithm build() {
return new HttpExceptionRetryAlgorithm(this);
public ExceptionHandler build() {
return new ExceptionHandler(this);
}
}

Expand Down Expand Up @@ -170,7 +170,7 @@ public boolean equals(Object obj) {
}
}

private HttpExceptionRetryAlgorithm(Builder builder) {
private ExceptionHandler(Builder builder) {
interceptors = builder.interceptors.build();
retriableExceptions = builder.retriableExceptions.build();
nonRetriableExceptions = builder.nonRetriableExceptions.build();
Expand Down Expand Up @@ -278,10 +278,10 @@ public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof HttpExceptionRetryAlgorithm)) {
if (!(obj instanceof ExceptionHandler)) {
return false;
}
HttpExceptionRetryAlgorithm other = (HttpExceptionRetryAlgorithm) obj;
ExceptionHandler other = (ExceptionHandler) obj;
return Objects.equals(interceptors, other.interceptors)
&& Objects.equals(retriableExceptions, other.retriableExceptions)
&& Objects.equals(nonRetriableExceptions, other.nonRetriableExceptions)
Expand All @@ -292,7 +292,7 @@ public boolean equals(Object obj) {
/**
* Returns an instance which retry any checked exception and abort on any runtime exception.
*/
public static HttpExceptionRetryAlgorithm getDefaultInstance() {
public static ExceptionHandler getDefaultInstance() {
return DEFAULT_INSTANCE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class RetryHelper {
public static <V> V runWithRetries(
Callable<V> callable,
RetrySettings retrySettings,
HttpExceptionRetryAlgorithm exceptionRetryAlgorithm,
ExceptionHandler exceptionRetryAlgorithm,
ApiClock clock)
throws RetryHelperException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.cloud.HttpExceptionRetryAlgorithm.Interceptor;
import com.google.cloud.HttpExceptionRetryAlgorithm.Interceptor.RetryResult;
import com.google.cloud.ExceptionHandler.Interceptor;
import com.google.cloud.ExceptionHandler.Interceptor.RetryResult;

import org.junit.Rule;
import org.junit.Test;
Expand All @@ -34,9 +34,9 @@
import java.util.concurrent.atomic.AtomicReference;

/**
* Tests for {@link HttpExceptionRetryAlgorithm}.
* Tests for {@link ExceptionHandler}.
*/
public class HttpExceptionRetryAlgorithmTest {
public class ExceptionHandlerTest {

@Rule
public ExpectedException thrown = ExpectedException.none();
Expand Down Expand Up @@ -82,15 +82,15 @@ public Object call() throws Error {
}

// using default exception handler (retry upon any non-runtime exceptions)
HttpExceptionRetryAlgorithm handler = HttpExceptionRetryAlgorithm.getDefaultInstance();
ExceptionHandler handler = ExceptionHandler.getDefaultInstance();
assertValidCallable(new A(), handler);
assertValidCallable(new B(), handler);
assertValidCallable(new C(), handler);
assertValidCallable(new D(), handler);
assertValidCallable(new E(), handler);
assertInvalidCallable(new F(), handler);

handler = HttpExceptionRetryAlgorithm.newBuilder()
handler = ExceptionHandler.newBuilder()
.retryOn(FileNotFoundException.class, NullPointerException.class)
.build();
assertInvalidCallable(new A(), handler);
Expand All @@ -101,11 +101,11 @@ public Object call() throws Error {
assertInvalidCallable(new F(), handler);
}

private static <T> void assertValidCallable(Callable<T> callable, HttpExceptionRetryAlgorithm handler) {
private static <T> void assertValidCallable(Callable<T> callable, ExceptionHandler handler) {
handler.verifyCaller(callable);
}

private static <T> void assertInvalidCallable(Callable<T> callable, HttpExceptionRetryAlgorithm handler) {
private static <T> void assertInvalidCallable(Callable<T> callable, ExceptionHandler handler) {
try {
handler.verifyCaller(callable);
fail("Expected RetryHelper constructor to fail");
Expand All @@ -116,12 +116,12 @@ private static <T> void assertInvalidCallable(Callable<T> callable, HttpExceptio

@Test
public void testShouldTry() {
HttpExceptionRetryAlgorithm handler = HttpExceptionRetryAlgorithm.newBuilder().retryOn(IOException.class).build();
ExceptionHandler handler = ExceptionHandler.newBuilder().retryOn(IOException.class).build();
assertTrue(handler.accept(new IOException()));
assertTrue(handler.accept(new ClosedByInterruptException()));
assertFalse(handler.accept(new RuntimeException()));

HttpExceptionRetryAlgorithm.Builder builder = HttpExceptionRetryAlgorithm.newBuilder()
ExceptionHandler.Builder builder = ExceptionHandler.newBuilder()
.retryOn(IOException.class, NullPointerException.class)
.abortOn(RuntimeException.class, ClosedByInterruptException.class,
InterruptedException.class);
Expand Down Expand Up @@ -188,7 +188,7 @@ public RetryResult afterEval(Exception exception, RetryResult retryResult) {

};

HttpExceptionRetryAlgorithm handler = HttpExceptionRetryAlgorithm.newBuilder().addInterceptors(interceptor).build();
ExceptionHandler handler = ExceptionHandler.newBuilder().addInterceptors(interceptor).build();
thrown.expect(NullPointerException.class);
handler.accept(new Exception());
}
Expand All @@ -210,7 +210,7 @@ public RetryResult afterEval(Exception exception, RetryResult retryResult) {

};

HttpExceptionRetryAlgorithm handler = HttpExceptionRetryAlgorithm.newBuilder().addInterceptors(interceptor).build();
ExceptionHandler handler = ExceptionHandler.newBuilder().addInterceptors(interceptor).build();
thrown.expect(NullPointerException.class);
handler.accept(new Exception());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public class SerializationTest extends BaseSerializationTest {

private static final BaseServiceException BASE_SERVICE_EXCEPTION =
new BaseServiceException(42, "message", "reason", true);
private static final HttpExceptionRetryAlgorithm EXCEPTION_HANDLER =
HttpExceptionRetryAlgorithm.getDefaultInstance();
private static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.getDefaultInstance();
private static final Identity IDENTITY = Identity.allAuthenticatedUsers();
private static final PageImpl<String> PAGE =
new PageImpl<>(null, "cursor", ImmutableList.of("string1", "string2"));
Expand Down

0 comments on commit d2c24a9

Please sign in to comment.