@Before public void setUp(){ logger = mock(Logger.class); circuitBreaker = CircuitBreaker.ofDefaults("testName"); }
@Test public void testConstructors() { final CircuitBreakerRegistry registry = new InMemoryCircuitBreakerRegistry(); CircuitBreakerExports.ofIterable("boo_breakers", singleton(CircuitBreaker.ofDefaults("foo"))); CircuitBreakerExports.ofCircuitBreakerRegistry("boo_breakers", registry); CircuitBreakerExports.ofSupplier("boo_breakers", () -> singleton(CircuitBreaker.ofDefaults("foo"))); CircuitBreakerExports.ofIterable(singleton(CircuitBreaker.ofDefaults("foo"))); CircuitBreakerExports.ofCircuitBreakerRegistry(registry); CircuitBreakerExports.ofSupplier(() -> singleton(CircuitBreaker.ofDefaults("foo"))); }
@Test(expected = NullPointerException.class) public void testConstructorWithNullName() { CircuitBreakerExports.ofCircuitBreaker(null, CircuitBreaker.ofDefaults("foo")); }
@Test public void shouldInvokeAsyncApply() throws ExecutionException, InterruptedException { // tag::shouldInvokeAsyncApply[] // Given CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("backendName"); assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED); // When Supplier<String> decoratedSupplier = CircuitBreaker .decorateSupplier(circuitBreaker, () -> "This can be any method which returns: 'Hello"); CompletableFuture<String> future = CompletableFuture.supplyAsync(decoratedSupplier) .thenApply(value -> value + " world'"); //Then assertThat(future.get()).isEqualTo("This can be any method which returns: 'Hello world'"); // end::shouldInvokeAsyncApply[] CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(0); }
@Test public void shouldInvokeRecoverFunction() { // tag::shouldInvokeRecoverFunction[] // Given CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName"); // When I decorate my function and invoke the decorated function CheckedFunction0<String> checkedSupplier = CircuitBreaker.decorateCheckedSupplier(circuitBreaker, () -> { throw new RuntimeException("BAM!"); }); Try<String> result = Try.of(checkedSupplier) .recover(throwable -> "Hello Recovery"); // Then the function should be a success, because the exception could be recovered assertThat(result.isSuccess()).isTrue(); // and the result must match the result of the recovery function. assertThat(result.get()).isEqualTo("Hello Recovery"); // end::shouldInvokeRecoverFunction[] }
@Test public void shouldReturnFailureWithRuntimeException() { // Given CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName"); assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0); assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(0); //When CheckedRunnable checkedRunnable = CircuitBreaker.decorateCheckedRunnable(circuitBreaker, () -> { throw new RuntimeException("BAM!"); }); Try result = Try.run(checkedRunnable); //Then assertThat(result.isFailure()).isTrue(); assertThat(result.failed().get()).isInstanceOf(RuntimeException.class); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1); }
@Test public void shouldInvokeMap() { // tag::shouldInvokeMap[] // Given CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName"); // When I decorate my function CheckedFunction0<String> decoratedSupplier = CircuitBreaker .decorateCheckedSupplier(circuitBreaker, () -> "This can be any method which returns: 'Hello"); // and chain an other function with map Try<String> result = Try.of(decoratedSupplier) .map(value -> value + " world'"); // Then the Try Monad returns a Success<String>, if all functions ran successfully. assertThat(result.isSuccess()).isTrue(); assertThat(result.get()).isEqualTo("This can be any method which returns: 'Hello world'"); // end::shouldInvokeMap[] }
@Test public void shouldExecuteCompletionStageAndReturnWithSuccess() throws ExecutionException, InterruptedException { // Given CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("backendName"); assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED); // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello"); // When CompletionStage<String> decoratedCompletionStage = circuitBreaker .executeCompletionStage(() -> CompletableFuture.supplyAsync(helloWorldService::returnHelloWorld)) .thenApply(value -> value + " world"); // Then the helloWorldService should be invoked 1 time assertThat(decoratedCompletionStage.toCompletableFuture().get()).isEqualTo("Hello world"); BDDMockito.then(helloWorldService).should(Mockito.times(1)).returnHelloWorld(); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(0); }
@Test public void shouldDecorateCompletionStageAndReturnWithExceptionAtSyncStage() throws ExecutionException, InterruptedException { // Given CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("backendName"); assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED); // Given the HelloWorldService throws an exception // When Supplier<CompletionStage<String>> completionStageSupplier = () -> { throw new WebServiceException("BAM! At sync stage"); }; Supplier<CompletionStage<String>> decoratedCompletionStageSupplier = CircuitBreaker.decorateCompletionStage(circuitBreaker, completionStageSupplier); Try<CompletionStage<String>> result = Try.of(decoratedCompletionStageSupplier::get); assertThat(result.isFailure()).isEqualTo(true); assertThat(result.failed().get()).isInstanceOf(WebServiceException.class); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1); }
@Test public void shouldExecuteFutureAndReturnWithSuccess() { // Given CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName"); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0); // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn(Future.succeededFuture("Hello world")); //When Future<String> future = VertxCircuitBreaker.executeFuture(circuitBreaker, helloWorldService::returnHelloWorld); //Then assertThat(future.succeeded()).isTrue(); assertThat(future.result()).isEqualTo("Hello world"); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(0); assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1); // Then the helloWorldService should be invoked 1 time BDDMockito.then(helloWorldService).should(Mockito.times(1)).returnHelloWorld(); }
@Test public void shouldExecuteCallableAndReturnWithSuccess() throws Throwable { // Given CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName"); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0); // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorldWithException()).willReturn("Hello world"); //When String result = circuitBreaker.executeCallable(helloWorldService::returnHelloWorldWithException); //Then assertThat(result).isEqualTo("Hello world"); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(0); assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1); // Then the helloWorldService should be invoked 1 time BDDMockito.then(helloWorldService).should(Mockito.times(1)).returnHelloWorldWithException(); }
@Test public void shouldExecuteSupplierAndReturnWithSuccess() { // Given CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName"); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0); // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world"); //When String result = circuitBreaker.executeSupplier(helloWorldService::returnHelloWorld); //Then assertThat(result).isEqualTo("Hello world"); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1); assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(0); assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1); // Then the helloWorldService should be invoked 1 time BDDMockito.then(helloWorldService).should(Mockito.times(1)).returnHelloWorld(); }
@Test public void testWithCircuitBreaker() throws Throwable { final CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("test"); final CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); final FeignDecorators testSubject = FeignDecorators.builder().withCircuitBreaker(circuitBreaker).build(); final Object result = testSubject.decorate(args -> args[0], null, null, null).apply(new Object[] {"test01"}); assertThat(result) .describedAs("Returned result is correct") .isEqualTo("test01"); assertThat(metrics.getNumberOfSuccessfulCalls()) .describedAs("Successful Calls") .isEqualTo(1); }
@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 testExecuteConsumer() throws ExecutionException, InterruptedException { // Given the HelloWorldService returns Hello world CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); Decorators.ofConsumer((String input) -> helloWorldService.sayHelloWorldWithName(input)) .withCircuitBreaker(circuitBreaker) .withBulkhead(Bulkhead.ofDefaults("testName")) .withRateLimiter(RateLimiter.ofDefaults("testName")) .accept("test"); 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)).sayHelloWorldWithName("test"); }
@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 testDecorateSupplier() { // Given the HelloWorldService returns Hello world given(helloWorldService.returnHelloWorld()).willReturn("Hello world"); CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); Supplier<String> decoratedSupplier = Decorators.ofSupplier(() -> helloWorldService.returnHelloWorld()) .withCircuitBreaker(circuitBreaker) .withRetry(Retry.ofDefaults("id")) .withRateLimiter(RateLimiter.ofDefaults("testName")) .withBulkhead(Bulkhead.ofDefaults("testName")) .decorate(); String result = 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)).returnHelloWorld(); }
@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 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(); }