public DecorateRunnable withRetry(Retry retryContext) { runnable = Retry.decorateRunnable(retryContext, runnable); return this; }
/** * Decorates and executes the decorated Runnable. * * @param runnable the original Runnable */ default void executeRunnable(Runnable runnable){ decorateRunnable(this, runnable).run(); }
@Test public void shouldNotRetry() { // Create a Retry with default configuration Retry retryContext = Retry.ofDefaults("id"); // Decorate the invocation of the HelloWorldService Runnable runnable = Retry.decorateRunnable(retryContext, helloWorldService::sayHelloWorld); // When runnable.run(); // Then the helloWorldService should be invoked 1 time BDDMockito.then(helloWorldService).should(Mockito.times(1)).sayHelloWorld(); Assertions.assertThat(sleptTime).isEqualTo(0); }
@Test public void testDecorateRunnable() { // Given the HelloWorldService throws an exception BDDMockito.willThrow(new WebServiceException("BAM!")).given(helloWorldService).sayHelloWorld(); // Create a Retry with default configuration Retry retry = Retry.ofDefaults("id"); // Decorate the invocation of the HelloWorldService Runnable runnable = Retry.decorateRunnable(retry, helloWorldService::sayHelloWorld); // When Try<Void> result = Try.run(runnable::run); // Then the helloWorldService should be invoked 3 times BDDMockito.then(helloWorldService).should(Mockito.times(3)).sayHelloWorld(); // and the result should be a failure Assertions.assertThat(result.isFailure()).isTrue(); // and the returned exception should be of type RuntimeException Assertions.assertThat(result.failed().get()).isInstanceOf(WebServiceException.class); Assertions.assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION*2); }