@Test public void testCreateWithDefaults() { // when Bulkhead bulkhead = Bulkhead.ofDefaults("test"); // then assertThat(bulkhead).isNotNull(); assertThat(bulkhead.getBulkheadConfig()).isNotNull(); }
@Test(expected = NullPointerException.class) public void testConstructorWithNullName() { BulkheadExports.ofSupplier(null, () -> singleton(Bulkhead.ofDefaults("foo"))); }
@Test public void testConstructors() { final BulkheadRegistry registry = new InMemoryBulkheadRegistry(BulkheadConfig.ofDefaults()); BulkheadExports.ofIterable("boo_bulkheads", singleton(Bulkhead.ofDefaults("foo"))); BulkheadExports.ofBulkheadRegistry("boo_bulkheads", registry); BulkheadExports.ofSupplier("boo_bulkheads", () -> singleton(Bulkhead.ofDefaults("foo"))); BulkheadExports.ofIterable(singleton(Bulkhead.ofDefaults("foo"))); BulkheadExports.ofBulkheadRegistry(registry); BulkheadExports.ofSupplier(() -> singleton(Bulkhead.ofDefaults("foo"))); }
@Override public Subscriber<Integer> createSubscriber(WhiteboxSubscriberProbe<Integer> probe) { return new io.github.resilience4j.reactor.bulkhead.operator.BulkheadSubscriber<Integer>(Bulkhead.ofDefaults("verification"), MonoProcessor.create()) { @Override public void hookOnSubscribe(Subscription subscription) {
@Test public void testDecorateCompletionStage() throws ExecutionException, InterruptedException { // Given the HelloWorldService returns Hello world given(helloWorldService.returnHelloWorld()).willReturn("Hello world"); CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend"); Supplier<CompletionStage<String>> completionStageSupplier = () -> CompletableFuture.supplyAsync(helloWorldService::returnHelloWorld); CompletionStage<String> completionStage = Decorators.ofCompletionStage(completionStageSupplier) .withCircuitBreaker(circuitBreaker) .withRetry(AsyncRetry.ofDefaults("id"), Executors.newSingleThreadScheduledExecutor()) .withBulkhead(Bulkhead.ofDefaults("testName")) .get(); String value = completionStage.toCompletableFuture().get(); assertThat(value).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 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 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 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 testExportsCircuitBreakerStates() { // Given final CollectorRegistry registry = new CollectorRegistry(); final Bulkhead bulkhead = Bulkhead.ofDefaults("foo"); BulkheadExports.ofIterable("boo_bulkhead", singletonList(bulkhead)).register(registry); final Supplier<Map<String, Double>> values = () -> HashSet .of("available_concurrent_calls") .map(param -> Tuple.of(param, registry.getSampleValue( "boo_bulkhead", new String[]{"name", "param"}, new String[]{"foo", param}))) .toMap(t -> t); // When final Map<String, Double> initialValues = values.get(); // Then assertThat(initialValues).isEqualTo(HashMap.of("available_concurrent_calls", 25.0)); }
@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 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 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 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(); }