io.github.resilience4j.circuitbreaker.CallNotPermittedException Java Examples

The following examples show how to use io.github.resilience4j.circuitbreaker.CallNotPermittedException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RetrofitCircuitBreakerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotCallServiceOnEnqueueWhenOpen() throws Throwable {
    stubFor(get(urlPathEqualTo("/greeting"))
        .willReturn(aResponse()
            .withStatus(200)
            .withHeader("Content-Type", "text/plain")
            .withBody("hello world")));

    circuitBreaker.transitionToOpenState();

    try {
        EnqueueDecorator.enqueue(service.greeting());
        fail("CallNotPermittedException was expected");
    } catch (CallNotPermittedException ignore) {

    }

    ensureAllRequestsAreExecuted(Duration.ofSeconds(1));
    verify(0, getRequestedFor(urlPathEqualTo("/greeting")));
}
 
Example #2
Source File: MonoCircuitBreakerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotSubscribeToMonoFromCallable() {
    given(circuitBreaker.tryAcquirePermission()).willReturn(false);
    given(helloWorldService.returnHelloWorld()).willReturn("Hello World");

    StepVerifier.create(
        Mono.fromCallable(() -> helloWorldService.returnHelloWorld())
            .flatMap(Mono::just)
            .transformDeferred(CircuitBreakerOperator.of(circuitBreaker)))
        .expectError(CallNotPermittedException.class)
        .verify();

    then(helloWorldService).should(never()).returnHelloWorld();
    verify(circuitBreaker, never()).onSuccess(anyLong(), any(TimeUnit.class));
    verify(circuitBreaker, never())
        .onError(anyLong(), any(TimeUnit.class), any(Throwable.class));
}
 
Example #3
Source File: DecoratorsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecorateSupplierWithFallback() {
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    circuitBreaker.transitionToOpenState();

    Supplier<String> decoratedSupplier = Decorators
        .ofSupplier(() -> helloWorldService.returnHelloWorld())
        .withCircuitBreaker(circuitBreaker)
        .withFallback(asList(IOException.class, CallNotPermittedException.class), (e) -> "Fallback")
        .decorate();

    String result = decoratedSupplier.get();

    assertThat(result).isEqualTo("Fallback");
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfNotPermittedCalls()).isEqualTo(1);
    then(helloWorldService).should(never()).returnHelloWorld();
}
 
Example #4
Source File: SingleCircuitBreakerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotSubscribeToSingleFromCallable() {
    given(circuitBreaker.tryAcquirePermission()).willReturn(false);
    given(helloWorldService.returnHelloWorld()).willReturn("Hello World");

    Single.fromCallable(() -> helloWorldService.returnHelloWorld())
        .compose(CircuitBreakerOperator.of(circuitBreaker))
        .test()
        .assertError(CallNotPermittedException.class)
        .assertNotComplete();

    then(helloWorldService).should(never()).returnHelloWorld();
    then(circuitBreaker).should(never()).onSuccess(anyLong(), any(TimeUnit.class));
    then(circuitBreaker).should(never())
        .onError(anyLong(), any(TimeUnit.class), any(Throwable.class));
}
 
Example #5
Source File: DecoratorsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecorateCheckedSupplierWithFallback() throws Throwable {
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    circuitBreaker.transitionToOpenState();

    CheckedFunction0<String> checkedSupplier = Decorators
        .ofCheckedSupplier(() -> helloWorldService.returnHelloWorldWithException())
        .withCircuitBreaker(circuitBreaker)
        .withFallback(CallNotPermittedException.class, e -> "Fallback")
        .decorate();

    String result = checkedSupplier.apply();

    assertThat(result).isEqualTo("Fallback");
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfNotPermittedCalls()).isEqualTo(1);
    then(helloWorldService).should(never()).returnHelloWorld();
}
 
Example #6
Source File: DecoratorsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecorateCallableWithFallback() throws Throwable {
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    circuitBreaker.transitionToOpenState();

    Callable<String> callable = Decorators
        .ofCallable(() -> helloWorldService.returnHelloWorldWithException())
        .withCircuitBreaker(circuitBreaker)
        .withFallback(CallNotPermittedException.class, e -> "Fallback")
        .decorate();

    String result = callable.call();

    assertThat(result).isEqualTo("Fallback");
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfNotPermittedCalls()).isEqualTo(1);
    then(helloWorldService).should(never()).returnHelloWorld();
}
 
Example #7
Source File: DecoratorsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecorateCompletionStageWithCallNotPermittedExceptionFallback() throws ExecutionException, InterruptedException {
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    circuitBreaker.transitionToOpenState();
    ThreadPoolBulkhead bulkhead = ThreadPoolBulkhead.ofDefaults("helloBackend");
    CompletionStage<String> completionStage = Decorators
        .ofSupplier(() -> helloWorldService.returnHelloWorld())
        .withThreadPoolBulkhead(bulkhead)
        .withCircuitBreaker(circuitBreaker)
        .withFallback(CallNotPermittedException.class, (e) -> "Fallback")
        .get();

    String result = completionStage.toCompletableFuture().get();

    assertThat(result).isEqualTo("Fallback");
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfNotPermittedCalls()).isEqualTo(1);
}
 
Example #8
Source File: SingleCircuitBreakerTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotSubscribeToSingleFromCallable() {
    given(circuitBreaker.tryAcquirePermission()).willReturn(false);
    given(helloWorldService.returnHelloWorld()).willReturn("Hello World");

    Single.fromCallable(() -> helloWorldService.returnHelloWorld())
        .compose(CircuitBreakerOperator.of(circuitBreaker))
        .test()
        .assertSubscribed()
        .assertError(CallNotPermittedException.class)
        .assertNotComplete();

    then(helloWorldService).should(never()).returnHelloWorld();
    then(circuitBreaker).should(never()).onSuccess(anyLong(), any(TimeUnit.class));
    then(circuitBreaker).should(never())
        .onError(anyLong(), any(TimeUnit.class), any(Throwable.class));
}
 
Example #9
Source File: Resilience4jFeignFallbackTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testFallbackExceptionFilterNotCalled() throws Exception {
    final TestService testServiceExceptionFallback = mock(TestService.class);
    when(testServiceExceptionFallback.greeting()).thenReturn("exception fallback");

    final FeignDecorators decorators = FeignDecorators.builder()
        .withFallback(testServiceExceptionFallback, CallNotPermittedException.class)
        .withFallback(testServiceFallback)
        .build();

    testService = Resilience4jFeign.builder(decorators).target(TestService.class, MOCK_URL);
    setupStub(400);

    final String result = testService.greeting();

    assertThat(result).describedAs("Result").isNotEqualTo("Hello, world!");
    assertThat(result).describedAs("Result").isEqualTo("fallback");
    verify(testServiceFallback, times(1)).greeting();
    verify(testServiceExceptionFallback, times(0)).greeting();
    verify(1, getRequestedFor(urlPathEqualTo("/greeting")));
}
 
Example #10
Source File: CompletableCircuitBreakerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithCallNotPermittedException() {
    given(circuitBreaker.tryAcquirePermission()).willReturn(false);

    Completable.complete()
        .compose(CircuitBreakerOperator.of(circuitBreaker))
        .test()
        .assertError(CallNotPermittedException.class)
        .assertNotComplete();

    then(circuitBreaker).should(never()).onSuccess(anyLong(), any(TimeUnit.class));
    then(circuitBreaker).should(never())
        .onError(anyLong(), any(TimeUnit.class), any(Throwable.class));
}
 
Example #11
Source File: ObserverCircuitBreakerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithCallNotPermittedException() {
    given(circuitBreaker.tryAcquirePermission()).willReturn(false);

    Observable.just("Event 1", "Event 2")
        .compose(CircuitBreakerOperator.of(circuitBreaker))
        .test()
        .assertError(CallNotPermittedException.class)
        .assertNotComplete();

    then(circuitBreaker).should(never()).onSuccess(anyLong(), any(TimeUnit.class));
    then(circuitBreaker).should(never())
        .onError(anyLong(), any(TimeUnit.class), any(Throwable.class));
}
 
Example #12
Source File: FlowableCircuitBreakerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithCallNotPermittedException() {
    given(circuitBreaker.tryAcquirePermission()).willReturn(false);

    Flowable.just("Event 1", "Event 2")
        .compose(CircuitBreakerOperator.of(circuitBreaker))
        .test()
        .assertError(CallNotPermittedException.class)
        .assertNotComplete();

    then(circuitBreaker).should(never()).onSuccess(anyLong(), any(TimeUnit.class));
    then(circuitBreaker).should(never())
        .onError(anyLong(), any(TimeUnit.class), any(Throwable.class));
}
 
Example #13
Source File: SingleCircuitBreakerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithCallNotPermittedException() {
    given(circuitBreaker.tryAcquirePermission()).willReturn(false);

    Single.just(1)
        .compose(CircuitBreakerOperator.of(circuitBreaker))
        .test()
        .assertSubscribed()
        .assertError(CallNotPermittedException.class)
        .assertNotComplete();

    then(circuitBreaker).should(never()).onSuccess(anyLong(), any(TimeUnit.class));
    then(circuitBreaker).should(never())
        .onError(anyLong(), any(TimeUnit.class), any(Throwable.class));
}
 
Example #14
Source File: ObserverCircuitBreakerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithCallNotPermittedException() {
    given(circuitBreaker.tryAcquirePermission()).willReturn(false);

    Observable.just("Event 1", "Event 2")
        .compose(CircuitBreakerOperator.of(circuitBreaker))
        .test()
        .assertSubscribed()
        .assertError(CallNotPermittedException.class)
        .assertNotComplete();

    then(circuitBreaker).should(never()).onSuccess(anyLong(), any(TimeUnit.class));
    then(circuitBreaker).should(never())
        .onError(anyLong(), any(TimeUnit.class), any(Throwable.class));
}
 
Example #15
Source File: MaybeCircuitBreakerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithCallNotPermittedException() {
    given(circuitBreaker.tryAcquirePermission()).willReturn(false);

    Maybe.just(1)
        .compose(CircuitBreakerOperator.of(circuitBreaker))
        .test()
        .assertSubscribed()
        .assertError(CallNotPermittedException.class)
        .assertNotComplete();

    then(circuitBreaker).should(never()).onSuccess(anyLong(), any(TimeUnit.class));
    then(circuitBreaker).should(never())
        .onError(anyLong(), any(TimeUnit.class), any(Throwable.class));
}
 
Example #16
Source File: CompletableCircuitBreakerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithCallNotPermittedException() {
    given(circuitBreaker.tryAcquirePermission()).willReturn(false);

    Completable.complete()
        .compose(CircuitBreakerOperator.of(circuitBreaker))
        .test()
        .assertSubscribed()
        .assertError(CallNotPermittedException.class)
        .assertNotComplete();

    then(circuitBreaker).should(never()).onSuccess(anyLong(), any(TimeUnit.class));
    then(circuitBreaker).should(never())
        .onError(anyLong(), any(TimeUnit.class), any(Throwable.class));
}
 
Example #17
Source File: FlowableCircuitBreakerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithCallNotPermittedException() {
    given(circuitBreaker.tryAcquirePermission()).willReturn(false);

    Flowable.just("Event 1", "Event 2")
        .compose(CircuitBreakerOperator.of(circuitBreaker))
        .test()
        .assertSubscribed()
        .assertError(CallNotPermittedException.class)
        .assertNotComplete();

    then(circuitBreaker).should(never()).onSuccess(anyLong(), any(TimeUnit.class));
    then(circuitBreaker).should(never())
        .onError(anyLong(), any(TimeUnit.class), any(Throwable.class));
}
 
Example #18
Source File: FluxCircuitBreakerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithCircuitBreakerOpenException() {
    given(circuitBreaker.tryAcquirePermission()).willReturn(false);

    StepVerifier.create(
        Flux.just("Event 1", "Event 2")
            .transformDeferred(CircuitBreakerOperator.of(circuitBreaker)))
        .expectError(CallNotPermittedException.class)
        .verify(Duration.ofSeconds(1));

    verify(circuitBreaker, never())
        .onError(anyLong(), any(TimeUnit.class), any(Throwable.class));
    verify(circuitBreaker, never()).onSuccess(anyLong(), any(TimeUnit.class));
}
 
Example #19
Source File: FluxCircuitBreakerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitCircuitBreakerOpenExceptionEvenWhenErrorNotOnSubscribe() {
    given(circuitBreaker.tryAcquirePermission()).willReturn(false);

    StepVerifier.create(
        Flux.error(new IOException("BAM!"), true)
            .transformDeferred(CircuitBreakerOperator.of(circuitBreaker)))
        .expectError(CallNotPermittedException.class)
        .verify(Duration.ofSeconds(1));

    verify(circuitBreaker, never())
        .onError(anyLong(), any(TimeUnit.class), any(Throwable.class));
    verify(circuitBreaker, never()).onSuccess(anyLong(), any(TimeUnit.class));
}
 
Example #20
Source File: FluxCircuitBreakerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitCircuitBreakerOpenExceptionEvenWhenErrorDuringSubscribe() {
    given(circuitBreaker.tryAcquirePermission()).willReturn(false);

    StepVerifier.create(
        Flux.error(new IOException("BAM!"))
            .transformDeferred(CircuitBreakerOperator.of(circuitBreaker)))
        .expectError(CallNotPermittedException.class)
        .verify(Duration.ofSeconds(1));

    verify(circuitBreaker, never())
        .onError(anyLong(), any(TimeUnit.class), any(Throwable.class));
    verify(circuitBreaker, never()).onSuccess(anyLong(), any(TimeUnit.class));
}
 
Example #21
Source File: MonoCircuitBreakerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitCallNotPermittedExceptionEvenWhenErrorDuringSubscribe() {
    given(circuitBreaker.tryAcquirePermission()).willReturn(false);

    StepVerifier.create(
        Mono.error(new IOException("BAM!"))
            .transformDeferred(CircuitBreakerOperator.of(circuitBreaker)))
        .expectError(CallNotPermittedException.class)
        .verify(Duration.ofSeconds(1));

    verify(circuitBreaker, never())
        .onError(anyLong(), any(TimeUnit.class), any(Throwable.class));
    verify(circuitBreaker, never()).onSuccess(anyLong(), any(TimeUnit.class));
}
 
Example #22
Source File: MonoCircuitBreakerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithCircuitBreakerOpenException() {
    given(circuitBreaker.tryAcquirePermission()).willReturn(false);

    StepVerifier.create(
        Mono.just("Event")
            .transformDeferred(CircuitBreakerOperator.of(circuitBreaker)))
        .expectError(CallNotPermittedException.class)
        .verify(Duration.ofSeconds(1));

    verify(circuitBreaker, never())
        .onError(anyLong(), any(TimeUnit.class), any(Throwable.class));
    verify(circuitBreaker, never()).onSuccess(anyLong(), any(TimeUnit.class));
}
 
Example #23
Source File: CombinedOperatorsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithCircuitBreakerOpenExceptionEvenWhenErrorDuringSubscribe() {
    circuitBreaker.transitionToOpenState();
    StepVerifier.create(
        Flux.error(new IOException("BAM!"))
            .transformDeferred(CircuitBreakerOperator.of(circuitBreaker))
            .transformDeferred(BulkheadOperator.of(bulkhead))
            .transformDeferred(RateLimiterOperator.of(rateLimiter))
    ).expectError(CallNotPermittedException.class)
        .verify(Duration.ofSeconds(1));
}
 
Example #24
Source File: CombinedOperatorsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithCircuitBreakerOpenExceptionEvenWhenErrorNotOnSubscribe() {
    circuitBreaker.transitionToOpenState();
    StepVerifier.create(
        Flux.error(new IOException("BAM!"), true)
            .transformDeferred(CircuitBreakerOperator.of(circuitBreaker))
            .transformDeferred(BulkheadOperator.of(bulkhead))
            .transformDeferred(RateLimiterOperator.of(rateLimiter))
    ).expectError(CallNotPermittedException.class)
        .verify(Duration.ofSeconds(1));
}
 
Example #25
Source File: App.java    From Design-Patterns-and-SOLID-Principles-with-Java with MIT License 5 votes vote down vote up
protected Collection<Customer> getCustomers() throws WarehouseException {
    try {
        return Decorators.ofCheckedSupplier(warehouse::getCustomers)
            .withCache(Cache.of(CACHE_MANAGER.getCache("customers")))
            .withCircuitBreaker(CIRCUIT_BREAKER)
            .apply(0);
    } catch (WarehouseException | CallNotPermittedException ex) {
        throw ex;
    } catch (Throwable t) {
        throw new IllegalStateException(t);
    }
}
 
Example #26
Source File: BackendBController.java    From resilience4j-spring-boot2-demo with Apache License 2.0 5 votes vote down vote up
private <T> Mono<T> executeWithFallback(Mono<T> publisher, Function<Throwable, Mono<T>> fallback){
    return publisher
            .transform(TimeLimiterOperator.of(timeLimiter))
            .transform(BulkheadOperator.of(bulkhead))
            .transform(CircuitBreakerOperator.of(circuitBreaker))
            .onErrorResume(TimeoutException.class, fallback)
            .onErrorResume(CallNotPermittedException.class, fallback)
            .onErrorResume(BulkheadFullException.class, fallback);
}
 
Example #27
Source File: BackendBController.java    From resilience4j-spring-boot2-demo with Apache License 2.0 5 votes vote down vote up
private <T> Flux<T> executeWithFallback(Flux<T> publisher, Function<Throwable, Flux<T>> fallback){
    return publisher
            .transform(TimeLimiterOperator.of(timeLimiter))
            .transform(BulkheadOperator.of(bulkhead))
            .transform(CircuitBreakerOperator.of(circuitBreaker))
            .onErrorResume(TimeoutException.class, fallback)
            .onErrorResume(CallNotPermittedException.class, fallback)
            .onErrorResume(BulkheadFullException.class, fallback);
}
 
Example #28
Source File: BackendBController.java    From resilience4j-spring-boot2-demo with Apache License 2.0 5 votes vote down vote up
private <T> CompletableFuture<T> executeAsyncWithFallback(Supplier<T> supplier, Function<Throwable, T> fallback){
    return Decorators.ofSupplier(supplier)
            .withThreadPoolBulkhead(threadPoolBulkhead)
            .withTimeLimiter(timeLimiter, scheduledExecutorService)
            .withCircuitBreaker(circuitBreaker)
            .withFallback(asList(TimeoutException.class, CallNotPermittedException.class, BulkheadFullException.class),
                    fallback)
            .get().toCompletableFuture();
}
 
Example #29
Source File: CommonCircuitBreaker.java    From charon-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
R executeFallback(CallNotPermittedException ex) {
    if (fallback == null) {
        throw ex;
    }
    getLog().debug("Circuit breaker call not permitted, executing fallback");
    return fallback.apply(ex);
}
 
Example #30
Source File: CircuitBreaker.java    From charon-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<HttpResponse> forward(HttpRequest request, HttpRequestExecution execution) {
    logStart(execution.getMappingName());
    io.github.resilience4j.circuitbreaker.CircuitBreaker circuitBreaker = getRegistry().circuitBreaker(execution.getMappingName());
    setupMetrics(registry -> createMetrics(registry, execution.getMappingName()));
    return execution.execute(request)
            .transform(of(circuitBreaker))
            .onErrorResume(CallNotPermittedException.class, ex -> just(executeFallback(ex)))
            .doOnSuccess(response -> logEnd(execution.getMappingName()));
}