@Before public void setUp(){ helloWorldService = mock(HelloWorldService.class); logger = mock(Logger.class); retry = Retry.ofDefaults("testName"); }
@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 testExecuteRunnable() { // Create a Retry with default configuration Retry retry = Retry.ofDefaults("id"); // Decorate the invocation of the HelloWorldService retry.executeRunnable(helloWorldService::sayHelloWorld); // Then the helloWorldService should be invoked 1 time BDDMockito.then(helloWorldService).should(Mockito.times(1)).sayHelloWorld(); Assertions.assertThat(sleptTime).isEqualTo(0); }
@Test public void shouldNotRetry() { // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world"); // Create a Retry with default configuration Retry retry = Retry.ofDefaults("id"); // 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 testExecuteCallable() throws Exception { // Given the HelloWorldService throws an exception BDDMockito.given(helloWorldService.returnHelloWorldWithException()).willThrow(new WebServiceException("BAM!")) .willReturn("Hello world"); // Create a Retry with default configuration Retry retry = Retry.ofDefaults("id"); // Decorate the invocation of the HelloWorldService String result = retry.executeCallable(helloWorldService::returnHelloWorldWithException); // Then the helloWorldService should be invoked 2 times BDDMockito.then(helloWorldService).should(Mockito.times(2)).returnHelloWorldWithException(); assertThat(result).isEqualTo("Hello world"); assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION); }
@Test public void testExecuteSupplier() { // Given the HelloWorldService throws an exception BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!")) .willReturn("Hello world"); // Create a Retry with default configuration Retry retry = Retry.ofDefaults("id"); // 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 testDecorateCallable() throws Exception { // Given the HelloWorldService throws an exception BDDMockito.given(helloWorldService.returnHelloWorldWithException()).willThrow(new WebServiceException("BAM!")) .willReturn("Hello world"); // Create a Retry with default configuration Retry retry = Retry.ofDefaults("id"); // Decorate the invocation of the HelloWorldService Callable<String> callable = Retry.decorateCallable(retry, helloWorldService::returnHelloWorldWithException); // When String result = callable.call(); // Then the helloWorldService should be invoked 2 times BDDMockito.then(helloWorldService).should(Mockito.times(2)).returnHelloWorldWithException(); assertThat(result).isEqualTo("Hello world"); assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION); }
@Test public void testDecorateSupplier() { // Given the HelloWorldService throws an exception BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!")) .willReturn("Hello world"); // Create a Retry with default configuration Retry retry = Retry.ofDefaults("id"); // 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 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 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); }
@Test public void shouldReturnAfterThreeAttempts() { // 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 CheckedRunnable retryableRunnable = Retry.decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld); // When Try<Void> result = Try.run(retryableRunnable); // 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); }
@Test public void shouldReturnAfterThreeAttempts() { // Given the HelloWorldService throws an exception BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!")); // Create a Retry with default configuration Retry retry = Retry.ofDefaults("id"); // Decorate the invocation of the HelloWorldService CheckedFunction0<String> retryableSupplier = Retry .decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld); // When Try<String> result = Try.of(retryableSupplier); // Then the helloWorldService should be invoked 3 times BDDMockito.then(helloWorldService).should(Mockito.times(3)).returnHelloWorld(); // and the result should be a failure assertThat(result.isFailure()).isTrue(); // and the returned exception should be of type RuntimeException assertThat(result.failed().get()).isInstanceOf(WebServiceException.class); assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2); }
@Test public void shouldReturnAfterThreeAttemptsAndRecover() { // Given the HelloWorldService throws an exception BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!")); // Create a Retry with default configuration Retry retry = Retry.ofDefaults("id"); // Decorate the invocation of the HelloWorldService CheckedFunction0<String> retryableSupplier = Retry .decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld); // When Try<String> result = Try.of(retryableSupplier).recover((throwable) -> "Hello world from recovery function"); assertThat(retry.getMetrics().getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(1); // Then the helloWorldService should be invoked 3 times BDDMockito.then(helloWorldService).should(Mockito.times(3)).returnHelloWorld(); // and the returned exception should be of type RuntimeException assertThat(result.get()).isEqualTo("Hello world from recovery function"); assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2); }
@Test public void shouldReturnSuccessfullyAfterSecondAttempt() { // Given the HelloWorldService throws an exception BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new WebServiceException("BAM!")) .willReturn("Hello world"); // Create a Retry with default configuration Retry retry = Retry.ofDefaults("id"); // Decorate the invocation of the HelloWorldService CheckedFunction0<String> retryableSupplier = Retry .decorateCheckedSupplier(retry, helloWorldService::returnHelloWorld); // When Try<String> result = Try.of(retryableSupplier); // Then the helloWorldService should be invoked 2 times BDDMockito.then(helloWorldService).should(Mockito.times(2)).returnHelloWorld(); assertThat(result.get()).isEqualTo("Hello world"); assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION); }
@Test public void testDecorateFunction() { // Given the HelloWorldService returns Hello world given(helloWorldService.returnHelloWorldWithName("Name")).willReturn("Hello world Name"); CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); Function<String, String> decoratedFunction = Decorators.ofFunction(helloWorldService::returnHelloWorldWithName) .withCircuitBreaker(circuitBreaker) .withRetry(Retry.ofDefaults("id")) .withRateLimiter(RateLimiter.ofDefaults("testName")) .withBulkhead(Bulkhead.ofDefaults("testName")) .decorate(); String result = decoratedFunction.apply("Name"); assertThat(result).isEqualTo("Hello world Name"); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1); }
@Test public void testDecorateRunnable() { CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); Runnable decoratedRunnable = Decorators.ofRunnable(() -> helloWorldService.sayHelloWorld()) .withCircuitBreaker(circuitBreaker) .withRetry(Retry.ofDefaults("id")) .withRateLimiter(RateLimiter.ofDefaults("testName")) .withBulkhead(Bulkhead.ofDefaults("testName")) .decorate(); decoratedRunnable.run(); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1); // Then the helloWorldService should be invoked 1 time BDDMockito.then(helloWorldService).should(times(1)).sayHelloWorld(); }
@Test public void testDecorateCheckedRunnable() throws IOException { CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); CheckedRunnable decoratedRunnable = Decorators.ofCheckedRunnable(() -> helloWorldService.sayHelloWorldWithException()) .withCircuitBreaker(circuitBreaker) .withRetry(Retry.ofDefaults("id")) .withRateLimiter(RateLimiter.ofDefaults("testName")) .withBulkhead(Bulkhead.ofDefaults("testName")) .decorate(); Try.run(decoratedRunnable); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1); // Then the helloWorldService should be invoked 1 time BDDMockito.then(helloWorldService).should(times(1)).sayHelloWorldWithException(); }
@Test public void testDecorateCheckedFunction() throws IOException { // Given the HelloWorldService returns Hello world given(helloWorldService.returnHelloWorldWithNameWithException("Name")).willReturn("Hello world Name"); CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); CheckedFunction1<String, String> decoratedFunction = Decorators.ofCheckedFunction(helloWorldService::returnHelloWorldWithNameWithException) .withCircuitBreaker(circuitBreaker) .withRetry(Retry.ofDefaults("id")) .withRateLimiter(RateLimiter.ofDefaults("testName")) .withBulkhead(Bulkhead.ofDefaults("testName")) .decorate(); String result = Try.of(() -> decoratedFunction.apply("Name")).get(); assertThat(result).isEqualTo("Hello world Name"); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1); }
@Test public void testDecoratorBuilderWithRetry() { // Given the HelloWorldService returns Hello world given(helloWorldService.returnHelloWorld()).willThrow(new RuntimeException("BAM!")); ; CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); Supplier<String> decoratedSupplier = Decorators.ofSupplier(() -> helloWorldService.returnHelloWorld()) .withCircuitBreaker(circuitBreaker) .withRetry(Retry.ofDefaults("id")) .withBulkhead(Bulkhead.ofDefaults("testName")) .decorate(); Try.of(decoratedSupplier::get); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(3); assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(3); // Then the helloWorldService should be invoked 1 time BDDMockito.then(helloWorldService).should(times(3)).returnHelloWorld(); }
@Test public void testDecorateCheckedSupplier() throws IOException { // Given the HelloWorldService returns Hello world given(helloWorldService.returnHelloWorldWithException()).willReturn("Hello world"); CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); CheckedFunction0<String> decoratedSupplier = Decorators.ofCheckedSupplier(() -> helloWorldService.returnHelloWorldWithException()) .withCircuitBreaker(circuitBreaker) .withRetry(Retry.ofDefaults("id")) .withRateLimiter(RateLimiter.ofDefaults("testName")) .withBulkhead(Bulkhead.ofDefaults("testName")) .decorate(); String result = Try.of(decoratedSupplier).get(); assertThat(result).isEqualTo("Hello world"); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1); // Then the helloWorldService should be invoked 1 time BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorldWithException(); }
@Test public void shouldReturnAfterTwoAttempts() { // Given the HelloWorldService throws an exception BDDMockito.willThrow(new WebServiceException("BAM!")).willDoNothing().given(helloWorldService).sayHelloWorld(); // Create a Retry with default configuration Retry retry = Retry.ofDefaults("id"); TestSubscriber<RetryEvent.Type> testSubscriber = toFlowable(retry.getEventPublisher()) .map(RetryEvent::getEventType) .test(); // Decorate the invocation of the HelloWorldService CheckedRunnable retryableRunnable = Retry.decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld); // When Try<Void> result = Try.run(retryableRunnable); // Then the helloWorldService should be invoked 2 times BDDMockito.then(helloWorldService).should(Mockito.times(2)).sayHelloWorld(); // and the result should be a sucess Assertions.assertThat(result.isSuccess()).isTrue(); Assertions.assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION); testSubscriber.assertValueCount(2).assertValues(RetryEvent.Type.RETRY, RetryEvent.Type.SUCCESS); }