@ParameterizedTest @MethodSource("unchecked") void testRunThrowsSubclassWithRetryOnException(Class<? extends Throwable> throwable) throws Exception { @SuppressWarnings("unchecked") Class<? extends Throwable> superclass = (Class<? extends Throwable>) throwable.getSuperclass(); Retryer retryer = RetryerBuilder.newBuilder() .retryIfExceptionOfType(superclass) .build(); Thrower thrower = new Thrower(throwable, 5); retryer.run(thrower); assertEquals(5, thrower.invocations); }
@ParameterizedTest @MethodSource("unchecked") void testRunThrowsWithNoRetryOnException(Class<? extends Throwable> throwable) throws Exception { Retryer retryer = RetryerBuilder.newBuilder().build(); Thrower thrower = new Thrower(throwable, 5); try { retryer.run(thrower); fail("Should have thrown"); } catch (RetryException e) { assertSame(e.getCause().getClass(), throwable); } assertEquals(1, thrower.invocations); }
@ParameterizedTest @MethodSource("unchecked") void testRunThrowsWithRetryOnException(Class<? extends Throwable> throwable) throws Exception { Retryer retryer = RetryerBuilder.newBuilder() .retryIfExceptionOfType(Throwable.class) .build(); Thrower thrower = new Thrower(throwable, 5); retryer.run(thrower); assertEquals(5, thrower.invocations); }
@ParameterizedTest @MethodSource("unchecked") void testRunThrowsWhenRetriesAreStopped(Class<? extends Throwable> throwable) throws Exception { Retryer retryer = RetryerBuilder.newBuilder() .retryIfExceptionOfType(throwable) .withStopStrategy(StopStrategies.stopAfterAttempt(3)) .build(); Thrower thrower = new Thrower(throwable, 5); try { retryer.run(thrower); fail("Should have thrown"); } catch (RetryException e) { assertSame(e.getCause().getClass(), throwable); } assertEquals(3, thrower.invocations); }
@Test void testRunThatIsInterrupted() throws Exception { Retryer retryer = RetryerBuilder.newBuilder() .retryIfRuntimeException() .withStopStrategy(StopStrategies.stopAfterAttempt(10)) .build(); Interrupter thrower = new Interrupter(4); boolean interrupted = false; try { retryer.run(thrower); fail("Should have thrown"); } catch(InterruptedException ignored) { interrupted = true; } //noinspection ConstantConditions assertTrue(interrupted); assertEquals(4, thrower.invocations); }
@Test public void testRunWhenBlockerIsInterrupted() throws Exception { Retryer retryer = RetryerBuilder.newBuilder() .retryIfException() .withStopStrategy(StopStrategies.stopAfterAttempt(10)) .withBlockStrategy(new InterruptingBlockStrategy(3)) .build(); Thrower thrower = new Thrower(Exception.class, 5); boolean interrupted = false; try { retryer.run(thrower); fail("Should have thrown"); } catch (InterruptedException e) { interrupted = true; } //noinspection ConstantConditions assertTrue(interrupted); assertEquals(3, thrower.invocations); }