protected void assertSingleSuccessfulCall() { assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(1); assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(1); assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(0); }
protected void assertSingleSuccessfulCall() { assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(1); assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(1); assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(0); }
protected void assertNoRegisteredCall() { assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(0); assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(0); assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(0); } }
protected void assertSingleFailedCall() { assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(1); assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(0); assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(1); }
protected void assertNoRegisteredCall() { assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(0); assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(0); assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(0); } }
protected void assertSingleFailedCall() { assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(1); assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(0); assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(1); }
@Test public void shouldDecorateConsumerAndReturnWithSuccess() throws Throwable { // Given CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults(); CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName"); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0); //When Consumer<String> consumer = CircuitBreaker.decorateConsumer(circuitBreaker, helloWorldService::sayHelloWorldWithName); //Then consumer.accept("Tom"); 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)).sayHelloWorldWithName("Tom"); }
@Test public void shouldExecuteRunnableAndReturnWithSuccess() throws Throwable { // Given CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults(); CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName"); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0); //When circuitBreaker.executeRunnable(helloWorldService::sayHelloWorld); //Then 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)).sayHelloWorld(); }
@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 shouldDecorateCheckedRunnableAndReturnWithSuccess() throws Throwable { // Given CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults(); CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName"); CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(0); //When CheckedRunnable checkedRunnable = CircuitBreaker.decorateCheckedRunnable(circuitBreaker, helloWorldService::sayHelloWorldWithException); //Then checkedRunnable.run(); 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)).sayHelloWorldWithException(); }
@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 testSuccessfulCall() throws Exception { final CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); setupStub(200); testService.greeting(); verify(1, getRequestedFor(urlPathEqualTo("/greeting"))); assertThat(metrics.getNumberOfSuccessfulCalls()) .describedAs("Successful Calls") .isEqualTo(1); }
@Test public void decorateUnsuccessfulCall() throws Exception { stubFor(get(urlPathEqualTo("/greeting")) .willReturn(aResponse() .withStatus(500) .withHeader("Content-Type", "text/plain"))); final Response<String> response = service.greeting().execute(); assertThat(response.code()) .describedAs("Response code") .isEqualTo(500); final CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1); }
@Test public void decorateUnsuccessfulEnqueuedCall() throws Throwable { stubFor(get(urlPathEqualTo("/greeting")) .willReturn(aResponse() .withStatus(500) .withHeader("Content-Type", "text/plain"))); final Response<String> response = EnqueueDecorator.enqueue(service.greeting()); assertThat(response.code()) .describedAs("Response code") .isEqualTo(500); final CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1); }
private Health.Builder addDetails(Health.Builder builder, CircuitBreaker circuitBreaker) { CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); CircuitBreakerConfig config = circuitBreaker.getCircuitBreakerConfig(); builder.withDetail(FAILURE_RATE, metrics.getFailureRate() + "%") .withDetail(FAILURE_RATE_THRESHOLD, config.getFailureRateThreshold() + "%") .withDetail(MAX_BUFFERED_CALLS, metrics.getMaxNumberOfBufferedCalls()) .withDetail(BUFFERED_CALLS, metrics.getNumberOfBufferedCalls()) .withDetail(FAILED_CALLS, metrics.getNumberOfFailedCalls()) .withDetail(NOT_PERMITTED, metrics.getNumberOfNotPermittedCalls()); return builder; } }
@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(); }
private Health.Builder addDetails(Health.Builder builder, CircuitBreaker circuitBreaker) { CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics(); CircuitBreakerConfig config = circuitBreaker.getCircuitBreakerConfig(); builder.withDetail(FAILURE_RATE, metrics.getFailureRate() + "%") .withDetail(FAILURE_RATE_THRESHOLD, config.getFailureRateThreshold() + "%") .withDetail(MAX_BUFFERED_CALLS, metrics.getMaxNumberOfBufferedCalls()) .withDetail(BUFFERED_CALLS, metrics.getNumberOfBufferedCalls()) .withDetail(FAILED_CALLS, metrics.getNumberOfFailedCalls()) .withDetail(NOT_PERMITTED, metrics.getNumberOfNotPermittedCalls()); return builder; } }
@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(); }