@Test public void waitIntervalOverTenMillisShouldSucceed() { RetryConfig config = RetryConfig.custom().waitDuration(Duration.ofSeconds(10)).build(); Assertions.assertThat(config).isNotNull(); }
@Test public void waitIntervalOfTenMillisShouldSucceed() { RetryConfig config = RetryConfig.custom().waitDuration(Duration.ofMillis(10)).build(); Assertions.assertThat(config).isNotNull(); }
@Test(expected = IllegalArgumentException.class) public void zeroWaitIntervalShouldFail() { RetryConfig.custom().waitDuration(Duration.ofMillis(0)).build(); }
@Test(expected = IllegalArgumentException.class) public void waitIntervalUnderTenMillisShouldFail() { RetryConfig.custom().waitDuration(Duration.ofMillis(5)).build(); }
@Test() public void shouldUseIgnoreExceptionToBuildPredicate() { RetryConfig retryConfig = RetryConfig.custom() .ignoreExceptions(RuntimeException.class, ExtendsExtendsException.class).build(); final Predicate<? super Throwable> failurePredicate = retryConfig.getExceptionPredicate(); then(failurePredicate.test(new Exception())).isEqualTo(true); // not explicitly excluded then(failurePredicate.test(new ExtendsError())).isEqualTo(true); // not explicitly excluded then(failurePredicate.test(new ExtendsException())).isEqualTo(true); // not explicitly excluded then(failurePredicate.test(new ExtendsException2())).isEqualTo(true); // not explicitly excluded then(failurePredicate.test(new RuntimeException())).isEqualTo(false); // explicitly excluded then(failurePredicate.test(new ExtendsRuntimeException())).isEqualTo(false); // inherits excluded from ExtendsException then(failurePredicate.test(new ExtendsExtendsException())).isEqualTo(false); // explicitly excluded }
@Test() public void shouldUseRecordExceptionToBuildPredicate() { RetryConfig retryConfig = RetryConfig.custom() .retryExceptions(RuntimeException.class, ExtendsExtendsException.class).build(); final Predicate<? super Throwable> failurePredicate = retryConfig.getExceptionPredicate(); then(failurePredicate.test(new Exception())).isEqualTo(false); // not explicitly included then(failurePredicate.test(new ExtendsError())).isEqualTo(false); // not explicitly included then(failurePredicate.test(new ExtendsException())).isEqualTo(false); // not explicitly included then(failurePredicate.test(new ExtendsException2())).isEqualTo(false); // not explicitly included then(failurePredicate.test(new RuntimeException())).isEqualTo(true); // explicitly included then(failurePredicate.test(new ExtendsRuntimeException())).isEqualTo(true); // inherits included from ExtendsException then(failurePredicate.test(new ExtendsExtendsException())).isEqualTo(true); // explicitly included }
@Test public void canBuildRetryFromRegistryWithConfig() { RetryConfig config = RetryConfig.custom().maxAttempts(1000).waitDuration(Duration.ofSeconds(300)).build(); Retry retry = retryRegistry.retry("testName", config); Assertions.assertThat(retry).isNotNull(); Assertions.assertThat(retryRegistry.getAllRetries()).hasSize(1); }
@Test public void canBuildRetryFromRegistryWithConfigSupplier() { RetryConfig config = RetryConfig.custom().maxAttempts(1000).waitDuration(Duration.ofSeconds(300)).build(); AsyncRetry retry = retryRegistry.retry("testName", () -> config); Assertions.assertThat(retry).isNotNull(); Assertions.assertThat(retryRegistry.getAllRetries()).hasSize(1); }
@Test public void canBuildRetryFromRegistryWithConfigSupplier() { RetryConfig config = RetryConfig.custom().maxAttempts(1000).waitDuration(Duration.ofSeconds(300)).build(); Retry retry = retryRegistry.retry("testName", () -> config); Assertions.assertThat(retry).isNotNull(); Assertions.assertThat(retryRegistry.getAllRetries()).hasSize(1); }
@Test public void canBuildRetryFromRegistryWithConfig() { RetryConfig config = RetryConfig.custom().maxAttempts(1000).waitDuration(Duration.ofSeconds(300)).build(); AsyncRetry retry = retryRegistry.retry("testName", config); Assertions.assertThat(retry).isNotNull(); Assertions.assertThat(retryRegistry.getAllRetries()).hasSize(1); }
@Test(expected = IllegalArgumentException.class) public void zeroMaxAttemptsShouldFail() { RetryConfig.custom().maxAttempts(0).build(); }
@Test() public void shouldUseIgnoreExceptionOverRecordToBuildPredicate() { RetryConfig retryConfig = RetryConfig.custom() .retryExceptions(RuntimeException.class, ExtendsExtendsException.class) .ignoreExceptions(ExtendsException.class, ExtendsRuntimeException.class) .build(); final Predicate<? super Throwable> failurePredicate = retryConfig.getExceptionPredicate(); then(failurePredicate.test(new Exception())).isEqualTo(false); // not explicitly included then(failurePredicate.test(new ExtendsError())).isEqualTo(false); // not explicitly included then(failurePredicate.test(new ExtendsException())).isEqualTo(false); // explicitly excluded then(failurePredicate.test(new ExtendsException2())).isEqualTo(false); // not explicitly included then(failurePredicate.test(new RuntimeException())).isEqualTo(true); // explicitly included then(failurePredicate.test(new ExtendsRuntimeException())).isEqualTo(false); // explicitly excluded then(failurePredicate.test(new ExtendsExtendsException())).isEqualTo(false); // inherits excluded from ExtendsException }
@Test public void canBuildRetryRegistryWithConfig() { RetryConfig config = RetryConfig.custom().maxAttempts(1000).waitDuration(Duration.ofSeconds(300)).build(); retryRegistry = RetryRegistry.of(config); Retry retry = retryRegistry.retry("testName", () -> config); Assertions.assertThat(retry).isNotNull(); Assertions.assertThat(retryRegistry.getAllRetries()).hasSize(1); } }
@Test public void canBuildRetryRegistryWithConfig() { RetryConfig config = RetryConfig.custom().maxAttempts(1000).waitDuration(Duration.ofSeconds(300)).build(); retryRegistry = AsyncRetryRegistry.of(config); AsyncRetry retry = retryRegistry.retry("testName", () -> config); Assertions.assertThat(retry).isNotNull(); Assertions.assertThat(retryRegistry.getAllRetries()).hasSize(1); } }
@Test public void shouldNotRetryWithResult() { // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world"); // Create a Retry with default configuration final RetryConfig tryAgain = RetryConfig.<String>custom().retryOnResult(s -> s.contains("tryAgain")) .maxAttempts(2).build(); Retry retry = Retry.of("id", tryAgain); // Decorate the invocation of the HelloWorldService Supplier<String> supplier = Retry.decorateSupplier(retry, helloWorldService::returnHelloWorld); // When String result = supplier.get(); // Then the helloWorldService should be invoked 1 time BDDMockito.then(helloWorldService).should(Mockito.times(1)).returnHelloWorld(); assertThat(result).isEqualTo("Hello world"); assertThat(sleptTime).isEqualTo(0); }
@Test public void shouldRetryWithResult() { // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world"); // Create a Retry with default configuration final RetryConfig tryAgain = RetryConfig.<String>custom().retryOnResult(s -> s.contains("Hello world")) .maxAttempts(2).build(); Retry retry = Retry.of("id", tryAgain); // Decorate the invocation of the HelloWorldService Supplier<String> supplier = Retry.decorateSupplier(retry, helloWorldService::returnHelloWorld); // When String result = supplier.get(); // Then the helloWorldService should be invoked 1 time BDDMockito.then(helloWorldService).should(Mockito.times(2)).returnHelloWorld(); assertThat(result).isEqualTo("Hello world"); }
@Test() public void shouldBuilderCreateConfigEveryTime() { final RetryConfig.Builder builder = RetryConfig.custom(); builder.maxAttempts(5); final RetryConfig config1 = builder.build(); builder.maxAttempts(3); final RetryConfig config2 = builder.build(); assertThat(config2.getMaxAttempts()).isEqualTo(3); assertThat(config1.getMaxAttempts()).isEqualTo(5); }
@Test public void testExecuteSupplierWithResult() { // Given the HelloWorldService throws an exception BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!")) .willReturn("Hello world"); // Create a Retry with default configuration final RetryConfig tryAgain = RetryConfig.<String>custom().retryOnResult(s -> s.contains("Hello world")) .maxAttempts(2).build(); Retry retry = Retry.of("id", tryAgain); // Decorate the invocation of the HelloWorldService String result = retry.executeSupplier(helloWorldService::returnHelloWorld); // Then the helloWorldService should be invoked 2 times BDDMockito.then(helloWorldService).should(Mockito.times(2)).returnHelloWorld(); assertThat(result).isEqualTo("Hello world"); assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION); }
@Test public void shouldConsumeIgnoredErrorEvent() { given(helloWorldService.returnHelloWorld()) .willThrow(new WebServiceException("BAM!")); RetryConfig retryConfig = RetryConfig.custom() .retryOnException(throwable -> Match(throwable).of( Case($(instanceOf(WebServiceException.class)), false), Case($(), true))) .build(); retry = Retry.of("testName", retryConfig); retry.getEventPublisher() .onIgnoredError(event -> logger.info(event.getEventType().toString())); Try.ofSupplier(Retry.decorateSupplier(retry, helloWorldService::returnHelloWorld)); then(logger).should(times(1)).info("IGNORED_ERROR"); then(helloWorldService).should(times(1)).returnHelloWorld(); }
@Test public void shouldConsumeIgnoredErrorEvent() { given(helloWorldService.returnHelloWorld()) .willThrow(new WebServiceException("BAM!")); RetryConfig retryConfig = RetryConfig.custom() .retryOnException(throwable -> Match(throwable).of( Case($(instanceOf(WebServiceException.class)), false), Case($(), true))) .build(); retry = AsyncRetry.of("testName", retryConfig); retry.getEventPublisher() .onIgnoredError(event -> logger.info(event.getEventType().toString())); Try.of(() -> awaitResult(retry.executeCompletionStage(scheduler, () -> helloWorldService.returnHelloWorld()))); then(logger).should(times(1)).info("IGNORED_ERROR"); then(helloWorldService).should(times(1)).returnHelloWorld(); }