Search in sources :

Example 1 with RetryResult

use of com.google.cloud.ExceptionHandler.Interceptor.RetryResult in project google-cloud-java by GoogleCloudPlatform.

the class ExceptionHandlerTest method testNullRetryResultFromBeforeEval.

@Test
public void testNullRetryResultFromBeforeEval() {
    @SuppressWarnings("serial") Interceptor interceptor = new Interceptor() {

        @Override
        public RetryResult beforeEval(Exception exception) {
            return null;
        }

        @Override
        public RetryResult afterEval(Exception exception, RetryResult retryResult) {
            return RetryResult.CONTINUE_EVALUATION;
        }
    };
    ExceptionHandler handler = ExceptionHandler.newBuilder().addInterceptors(interceptor).build();
    thrown.expect(NullPointerException.class);
    handler.accept(new Exception());
}
Also used : RetryResult(com.google.cloud.ExceptionHandler.Interceptor.RetryResult) Interceptor(com.google.cloud.ExceptionHandler.Interceptor) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) ExpectedException(org.junit.rules.ExpectedException) Test(org.junit.Test)

Example 2 with RetryResult

use of com.google.cloud.ExceptionHandler.Interceptor.RetryResult in project google-cloud-java by GoogleCloudPlatform.

the class ExceptionHandlerTest method testNullRetryResultFromAfterEval.

@Test
public void testNullRetryResultFromAfterEval() {
    @SuppressWarnings("serial") Interceptor interceptor = new Interceptor() {

        @Override
        public RetryResult beforeEval(Exception exception) {
            return RetryResult.CONTINUE_EVALUATION;
        }

        @Override
        public RetryResult afterEval(Exception exception, RetryResult retryResult) {
            return null;
        }
    };
    ExceptionHandler handler = ExceptionHandler.newBuilder().addInterceptors(interceptor).build();
    thrown.expect(NullPointerException.class);
    handler.accept(new Exception());
}
Also used : RetryResult(com.google.cloud.ExceptionHandler.Interceptor.RetryResult) Interceptor(com.google.cloud.ExceptionHandler.Interceptor) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) ExpectedException(org.junit.rules.ExpectedException) Test(org.junit.Test)

Example 3 with RetryResult

use of com.google.cloud.ExceptionHandler.Interceptor.RetryResult in project google-cloud-java by GoogleCloudPlatform.

the class ExceptionHandlerTest method testShouldTry.

@Test
public void testShouldTry() {
    ExceptionHandler handler = ExceptionHandler.newBuilder().retryOn(IOException.class).build();
    assertTrue(handler.accept(new IOException()));
    assertTrue(handler.accept(new ClosedByInterruptException()));
    assertFalse(handler.accept(new RuntimeException()));
    ExceptionHandler.Builder builder = ExceptionHandler.newBuilder().retryOn(IOException.class, NullPointerException.class).abortOn(RuntimeException.class, ClosedByInterruptException.class, InterruptedException.class);
    handler = builder.build();
    assertTrue(handler.accept(new IOException()));
    assertFalse(handler.accept(new ClosedByInterruptException()));
    assertFalse(handler.accept(new InterruptedException()));
    assertFalse(handler.accept(new RuntimeException()));
    assertTrue(handler.accept(new NullPointerException()));
    final AtomicReference<RetryResult> before = new AtomicReference<>(RetryResult.NO_RETRY);
    @SuppressWarnings("serial") Interceptor interceptor = new Interceptor() {

        @Override
        public RetryResult afterEval(Exception exception, RetryResult retryResult) {
            return retryResult == RetryResult.NO_RETRY ? RetryResult.RETRY : RetryResult.NO_RETRY;
        }

        @Override
        public RetryResult beforeEval(Exception exception) {
            return before.get();
        }
    };
    builder.addInterceptors(interceptor);
    handler = builder.build();
    assertFalse(handler.accept(new IOException()));
    assertFalse(handler.accept(new ClosedByInterruptException()));
    assertFalse(handler.accept(new InterruptedException()));
    assertFalse(handler.accept(new RuntimeException()));
    assertFalse(handler.accept(new NullPointerException()));
    before.set(RetryResult.RETRY);
    assertTrue(handler.accept(new IOException()));
    assertTrue(handler.accept(new ClosedByInterruptException()));
    assertTrue(handler.accept(new InterruptedException()));
    assertTrue(handler.accept(new RuntimeException()));
    assertTrue(handler.accept(new NullPointerException()));
    before.set(RetryResult.CONTINUE_EVALUATION);
    assertFalse(handler.accept(new IOException()));
    assertTrue(handler.accept(new ClosedByInterruptException()));
    assertTrue(handler.accept(new InterruptedException()));
    assertTrue(handler.accept(new RuntimeException()));
    assertFalse(handler.accept(new NullPointerException()));
}
Also used : RetryResult(com.google.cloud.ExceptionHandler.Interceptor.RetryResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) ExpectedException(org.junit.rules.ExpectedException) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) Interceptor(com.google.cloud.ExceptionHandler.Interceptor) Test(org.junit.Test)

Aggregations

Interceptor (com.google.cloud.ExceptionHandler.Interceptor)3 RetryResult (com.google.cloud.ExceptionHandler.Interceptor.RetryResult)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)3 Test (org.junit.Test)3 ExpectedException (org.junit.rules.ExpectedException)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)1