public DecorateSupplier<T> withBulkhead(Bulkhead bulkhead) { supplier = Bulkhead.decorateSupplier(bulkhead, supplier); return this; }
/** * Decorates and executes the decorated Supplier. * * @param supplier the original Supplier * @param <T> the type of results supplied by this supplier * @return the result of the decorated Supplier. */ default <T> T executeSupplier(Supplier<T> supplier){ return decorateSupplier(this, supplier).get(); }
@Test public void shouldInvokeAsyncApply() throws ExecutionException, InterruptedException { // tag::shouldInvokeAsyncApply[] // Given Bulkhead bulkhead = Bulkhead.of("test", config); // When Supplier<String> decoratedSupplier = Bulkhead.decorateSupplier(bulkhead, () -> "This can be any method which returns: 'Hello"); CompletableFuture<String> future = CompletableFuture.supplyAsync(decoratedSupplier) .thenApply(value -> value + " world'"); String result = future.get(); // Then assertThat(result).isEqualTo("This can be any method which returns: 'Hello world'"); assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1); // end::shouldInvokeAsyncApply[] }
@Test public void shouldDecorateSupplierAndReturnWithSuccess() { // Given Bulkhead bulkhead = Bulkhead.of("test", config); BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world"); // When Supplier<String> supplier = Bulkhead.decorateSupplier(bulkhead, helloWorldService::returnHelloWorld); // Then assertThat(supplier.get()).isEqualTo("Hello world"); assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1); BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld(); }
@Test public void shouldDecorateSupplierAndReturnWithException() { // Given Bulkhead bulkhead = Bulkhead.of("test", config); BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new RuntimeException("BAM!")); // When Supplier<String> supplier = Bulkhead.decorateSupplier(bulkhead, helloWorldService::returnHelloWorld); Try<String> result = Try.of(supplier::get); //Then assertThat(result.isFailure()).isTrue(); assertThat(result.failed().get()).isInstanceOf(RuntimeException.class); assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1); BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld(); }
@Test public void shouldConsumeOnCallFinishedEventWhenExecutionIsFinished() throws Exception { // Given Bulkhead bulkhead = Bulkhead.of("test", config); // When bulkhead.getEventPublisher() .onCallFinished(event -> logger.info(event.getEventType().toString())); Try.ofSupplier(Bulkhead.decorateSupplier(bulkhead,helloWorldService::returnHelloWorld)); // Then then(logger).should(times(1)).info("CALL_FINISHED"); }
@Test public void shouldConsumeOnCallRejectedEvent() { // Given Bulkhead bulkhead = Bulkhead.of("test", config); // When bulkhead.getEventPublisher() .onCallRejected(event -> logger.info(event.getEventType().toString())); bulkhead.isCallPermitted(); Try.ofSupplier(Bulkhead.decorateSupplier(bulkhead,helloWorldService::returnHelloWorld)); // Then then(logger).should(times(1)).info("CALL_REJECTED"); }