@Test public void shouldReturnSuccess() { // Given CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults(); CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName"); assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED); // When Supplier<String> checkedSupplier = CircuitBreaker.decorateSupplier(() -> "Hello world", circuitBreaker); // Then assertThat(checkedSupplier.get()).isEqualTo("Hello world"); }
@Test public void shouldReturnFailureWithRuntimeException() { // Given CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults(); CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName"); assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED); // When Try.CheckedRunnable checkedRunnable = CircuitBreaker.decorateCheckedRunnable( () -> { throw new RuntimeException("BAM!"); }, circuitBreaker); Try result = Try.run(checkedRunnable); // Then assertThat(result.isFailure()).isTrue(); assertThat(result.failed().get()).isInstanceOf(RuntimeException.class); }
@Test public void shouldInvokeAsyncApply() throws ExecutionException, InterruptedException { // tag::shouldInvokeAsyncApply[] // Given CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults(); CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName"); assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED); // When Supplier<String> decoratedSupplier = CircuitBreaker.decorateSupplier( () -> "This can be any method which returns: 'Hello", circuitBreaker); 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[] }
@Test public void shouldInvokeMap() { // tag::shouldInvokeMap[] // Given CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults(); CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName"); // When I decorate my function Try.CheckedSupplier<String> decoratedSupplier = CircuitBreaker.decorateCheckedSupplier( () -> "This can be any method which returns: 'Hello", circuitBreaker); // 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 shouldInvokeRecoverFunction() { // tag::shouldInvokeRecoverFunction[] // Given CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults(); CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName"); // When I decorate my function and invoke the decorated function Try.CheckedSupplier<String> checkedSupplier = CircuitBreaker.decorateCheckedSupplier( () -> { throw new RuntimeException("BAM!"); }, circuitBreaker); 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 shouldChainDecoratedFunctions() throws ExecutionException, InterruptedException { // tag::shouldChainDecoratedFunctions[] // Given CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults(); CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName"); CircuitBreaker anotherCircuitBreaker = circuitBreakerRegistry.circuitBreaker("anotherTestName"); // When I create a Supplier and a Function which are decorated by different CircuitBreakers Try.CheckedSupplier<String> decoratedSupplier = CircuitBreaker.decorateCheckedSupplier(() -> "Hello", circuitBreaker); Try.CheckedFunction<String, String> decoratedFunction = CircuitBreaker.decorateCheckedFunction((input) -> input + " world", anotherCircuitBreaker); // and I chain a function with map Try<String> result = Try.of(decoratedSupplier).mapTry(decoratedFunction::apply); // Then assertThat(result.isSuccess()).isTrue(); assertThat(result.get()).isEqualTo("Hello world"); // end::shouldChainDecoratedFunctions[] }