Search in sources :

Example 1 with TestThreadLocalContextPropagatorWithHolder

use of io.github.resilience4j.test.TestContextPropagators.TestThreadLocalContextPropagatorWithHolder in project resilience4j by resilience4j.

the class DecoratorsTest method testDecorateCompletionStagePropagatesContextWithRetryAsync.

@Test
public void testDecorateCompletionStagePropagatesContextWithRetryAsync() {
    given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    TestThreadLocalContextPropagatorWithHolder propagator = new TestThreadLocalContextPropagatorWithHolder();
    TestThreadLocalContextHolder.put("ValueShouldCrossThreadBoundary");
    ContextAwareScheduledThreadPoolExecutor scheduledThreadPool = ContextAwareScheduledThreadPoolExecutor.newScheduledThreadPool().corePoolSize(1).contextPropagators(propagator).build();
    CompletableFuture<String> failedFuture = new CompletableFuture<>();
    failedFuture.completeExceptionally(new HelloWorldException());
    given(asyncHelloWorldService.returnHelloWorld()).willReturn(failedFuture);
    CompletionStage<String> completionStage = Decorators.ofCompletionStage(() -> asyncHelloWorldService.returnHelloWorld()).withCircuitBreaker(circuitBreaker).withRetry(Retry.ofDefaults("id"), scheduledThreadPool).withBulkhead(Bulkhead.ofDefaults("testName")).get();
    final CompletableFuture<String> completableFuture = completionStage.toCompletableFuture().exceptionally(throwable -> {
        if (throwable != null) {
            assertThat(Thread.currentThread().getName()).contains("ContextAwareScheduledThreadPool");
            assertThat(TestThreadLocalContextHolder.get().get()).isEqualTo("ValueShouldCrossThreadBoundary");
            return (String) TestThreadLocalContextHolder.get().orElse(null);
        }
        return null;
    });
    waitAtMost(2, TimeUnit.SECONDS).until(matches(() -> assertThat(completableFuture).isCompletedWithValue("ValueShouldCrossThreadBoundary")));
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(3);
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(3);
    then(asyncHelloWorldService).should(times(3)).returnHelloWorld();
}
Also used : CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) HelloWorldException(io.github.resilience4j.test.HelloWorldException) ContextAwareScheduledThreadPoolExecutor(io.github.resilience4j.core.ContextAwareScheduledThreadPoolExecutor) TestThreadLocalContextPropagatorWithHolder(io.github.resilience4j.test.TestContextPropagators.TestThreadLocalContextPropagatorWithHolder) Test(org.junit.Test)

Example 2 with TestThreadLocalContextPropagatorWithHolder

use of io.github.resilience4j.test.TestContextPropagators.TestThreadLocalContextPropagatorWithHolder in project resilience4j by resilience4j.

the class DecoratorsTest method shouldThrowTimeoutExceptionAndPropagateContext.

@Test
public void shouldThrowTimeoutExceptionAndPropagateContext() {
    TimeLimiter timeLimiter = TimeLimiter.of("helloBackend", TimeLimiterConfig.custom().timeoutDuration(Duration.ofMillis(100)).build());
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    ThreadPoolBulkhead bulkhead = ThreadPoolBulkhead.ofDefaults("helloBackend");
    TestThreadLocalContextPropagatorWithHolder propagator = new TestThreadLocalContextPropagatorWithHolder();
    TestThreadLocalContextHolder.put("ValueShouldCrossThreadBoundary");
    ContextAwareScheduledThreadPoolExecutor scheduledThreadPool = ContextAwareScheduledThreadPoolExecutor.newScheduledThreadPool().corePoolSize(1).contextPropagators(propagator).build();
    CompletionStage<String> completionStage = Decorators.ofCallable(() -> {
        assertThat(Thread.currentThread().getName()).isEqualTo("bulkhead-helloBackend-1");
        Thread.sleep(1000);
        return "Bla";
    }).withThreadPoolBulkhead(bulkhead).withTimeLimiter(timeLimiter, scheduledThreadPool).withCircuitBreaker(circuitBreaker).get();
    final CompletableFuture<String> completableFuture = completionStage.toCompletableFuture().exceptionally(throwable -> {
        if (throwable != null) {
            assertThat(Thread.currentThread().getName()).isEqualTo("ContextAwareScheduledThreadPool-1");
            assertThat(TestThreadLocalContextHolder.get().get()).isEqualTo("ValueShouldCrossThreadBoundary");
            return (String) TestThreadLocalContextHolder.get().orElse(null);
        }
        return null;
    });
    waitAtMost(2, TimeUnit.SECONDS).until(matches(() -> assertThat(completableFuture).isCompletedWithValue("ValueShouldCrossThreadBoundary")));
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) ThreadPoolBulkhead(io.github.resilience4j.bulkhead.ThreadPoolBulkhead) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) ContextAwareScheduledThreadPoolExecutor(io.github.resilience4j.core.ContextAwareScheduledThreadPoolExecutor) TestThreadLocalContextPropagatorWithHolder(io.github.resilience4j.test.TestContextPropagators.TestThreadLocalContextPropagatorWithHolder) Test(org.junit.Test)

Aggregations

CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)2 ContextAwareScheduledThreadPoolExecutor (io.github.resilience4j.core.ContextAwareScheduledThreadPoolExecutor)2 TestThreadLocalContextPropagatorWithHolder (io.github.resilience4j.test.TestContextPropagators.TestThreadLocalContextPropagatorWithHolder)2 Test (org.junit.Test)2 ThreadPoolBulkhead (io.github.resilience4j.bulkhead.ThreadPoolBulkhead)1 HelloWorldException (io.github.resilience4j.test.HelloWorldException)1 TimeLimiter (io.github.resilience4j.timelimiter.TimeLimiter)1