Search in sources :

Example 16 with TimeLimiter

use of io.github.resilience4j.timelimiter.TimeLimiter in project resilience4j by resilience4j.

the class TimeLimiterConfigurationTest method testTimeLimiterRegistry.

@Test
public void testTimeLimiterRegistry() {
    // Given
    io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties instanceProperties1 = new io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties();
    instanceProperties1.setTimeoutDuration(Duration.ofSeconds(3));
    io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties instanceProperties2 = new io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties();
    instanceProperties2.setTimeoutDuration(Duration.ofSeconds(2));
    TimeLimiterConfigurationProperties timeLimiterConfigurationProperties = new TimeLimiterConfigurationProperties();
    timeLimiterConfigurationProperties.getInstances().put("backend1", instanceProperties1);
    timeLimiterConfigurationProperties.getInstances().put("backend2", instanceProperties2);
    timeLimiterConfigurationProperties.setTimeLimiterAspectOrder(200);
    TimeLimiterConfiguration timeLimiterConfiguration = new TimeLimiterConfiguration();
    DefaultEventConsumerRegistry<TimeLimiterEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
    // When
    TimeLimiterRegistry timeLimiterRegistry = timeLimiterConfiguration.timeLimiterRegistry(timeLimiterConfigurationProperties, eventConsumerRegistry, new CompositeRegistryEventConsumer<>(emptyList()), compositeTimeLimiterCustomizerTestInstance());
    // Then
    assertThat(timeLimiterConfigurationProperties.getTimeLimiterAspectOrder()).isEqualTo(200);
    assertThat(timeLimiterRegistry.getAllTimeLimiters().size()).isEqualTo(2);
    TimeLimiter timeLimiter1 = timeLimiterRegistry.timeLimiter("backend1");
    assertThat(timeLimiter1).isNotNull();
    assertThat(timeLimiter1.getTimeLimiterConfig().getTimeoutDuration()).isEqualTo(Duration.ofSeconds(3));
    TimeLimiter timeLimiter2 = timeLimiterRegistry.timeLimiter("backend2");
    assertThat(timeLimiter2).isNotNull();
    assertThat(timeLimiter2.getTimeLimiterConfig().getTimeoutDuration()).isEqualTo(Duration.ofSeconds(2));
    assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(2);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) DefaultEventConsumerRegistry(io.github.resilience4j.consumer.DefaultEventConsumerRegistry) TimeLimiterRegistry(io.github.resilience4j.timelimiter.TimeLimiterRegistry) TimeLimiterEvent(io.github.resilience4j.timelimiter.event.TimeLimiterEvent) Test(org.junit.Test)

Example 17 with TimeLimiter

use of io.github.resilience4j.timelimiter.TimeLimiter in project resilience4j by resilience4j.

the class TimeLimiterConfigurationTest method testCreateTimeLimiterRegistryWithSharedConfigs.

@Test
public void testCreateTimeLimiterRegistryWithSharedConfigs() {
    // Given
    io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties defaultProperties = new io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties();
    defaultProperties.setTimeoutDuration(Duration.ofSeconds(3));
    defaultProperties.setCancelRunningFuture(true);
    io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties sharedProperties = new io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties();
    sharedProperties.setTimeoutDuration(Duration.ofSeconds(2));
    sharedProperties.setCancelRunningFuture(false);
    io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties backendWithDefaultConfig = new io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties();
    backendWithDefaultConfig.setBaseConfig("default");
    backendWithDefaultConfig.setTimeoutDuration(Duration.ofSeconds(5));
    io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties backendWithSharedConfig = new io.github.resilience4j.common.timelimiter.configuration.TimeLimiterConfigurationProperties.InstanceProperties();
    backendWithSharedConfig.setBaseConfig("sharedConfig");
    backendWithSharedConfig.setCancelRunningFuture(true);
    TimeLimiterConfigurationProperties timeLimiterConfigurationProperties = new TimeLimiterConfigurationProperties();
    timeLimiterConfigurationProperties.getConfigs().put("default", defaultProperties);
    timeLimiterConfigurationProperties.getConfigs().put("sharedConfig", sharedProperties);
    timeLimiterConfigurationProperties.getInstances().put("backendWithDefaultConfig", backendWithDefaultConfig);
    timeLimiterConfigurationProperties.getInstances().put("backendWithSharedConfig", backendWithSharedConfig);
    TimeLimiterConfiguration timeLimiterConfiguration = new TimeLimiterConfiguration();
    DefaultEventConsumerRegistry<TimeLimiterEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
    // When
    TimeLimiterRegistry timeLimiterRegistry = timeLimiterConfiguration.timeLimiterRegistry(timeLimiterConfigurationProperties, eventConsumerRegistry, new CompositeRegistryEventConsumer<>(emptyList()), compositeTimeLimiterCustomizerTestInstance());
    // Then
    assertThat(timeLimiterRegistry.getAllTimeLimiters().size()).isEqualTo(2);
    // Should get default config and overwrite timeout duration
    TimeLimiter timeLimiter1 = timeLimiterRegistry.timeLimiter("backendWithDefaultConfig");
    assertThat(timeLimiter1).isNotNull();
    assertThat(timeLimiter1.getTimeLimiterConfig().getTimeoutDuration()).isEqualTo(Duration.ofSeconds(5));
    assertThat(timeLimiter1.getTimeLimiterConfig().shouldCancelRunningFuture()).isEqualTo(true);
    // Should get shared config and overwrite cancelRunningFuture
    TimeLimiter timeLimiter2 = timeLimiterRegistry.timeLimiter("backendWithSharedConfig");
    assertThat(timeLimiter2).isNotNull();
    assertThat(timeLimiter2.getTimeLimiterConfig().getTimeoutDuration()).isEqualTo(Duration.ofSeconds(2));
    assertThat(timeLimiter2.getTimeLimiterConfig().shouldCancelRunningFuture()).isEqualTo(true);
    // Unknown backend should get default config of Registry
    TimeLimiter timeLimiter3 = timeLimiterRegistry.timeLimiter("unknownBackend");
    assertThat(timeLimiter3).isNotNull();
    assertThat(timeLimiter3.getTimeLimiterConfig().getTimeoutDuration()).isEqualTo(Duration.ofSeconds(3));
    assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(3);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) DefaultEventConsumerRegistry(io.github.resilience4j.consumer.DefaultEventConsumerRegistry) TimeLimiterRegistry(io.github.resilience4j.timelimiter.TimeLimiterRegistry) TimeLimiterEvent(io.github.resilience4j.timelimiter.event.TimeLimiterEvent) Test(org.junit.Test)

Example 18 with TimeLimiter

use of io.github.resilience4j.timelimiter.TimeLimiter in project resilience4j by resilience4j.

the class RxJava2TimeLimiterAspectExtTest method shouldThrowIllegalArgumentExceptionWithNotRxJava2Type.

@Test
public void shouldThrowIllegalArgumentExceptionWithNotRxJava2Type() throws Throwable {
    TimeLimiter timeLimiter = TimeLimiter.ofDefaults("test");
    when(proceedingJoinPoint.proceed()).thenReturn("NOT RXJAVA2 TYPE");
    try {
        rxJava2TimeLimiterAspectExt.handle(proceedingJoinPoint, timeLimiter, "testMethod");
        fail("exception missed");
    } catch (Throwable e) {
        assertThat(e).isInstanceOf(IllegalReturnTypeException.class).hasMessage("java.lang.String testMethod has unsupported by @TimeLimiter return type. RxJava2 expects Flowable/Single/...");
    }
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) Test(org.junit.Test)

Example 19 with TimeLimiter

use of io.github.resilience4j.timelimiter.TimeLimiter 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)

Example 20 with TimeLimiter

use of io.github.resilience4j.timelimiter.TimeLimiter in project resilience4j by resilience4j.

the class DecoratorsTest method shouldThrowTimeoutException.

@Test
public void shouldThrowTimeoutException() {
    TimeLimiter timeLimiter = TimeLimiter.of("helloBackend", TimeLimiterConfig.custom().timeoutDuration(Duration.ofMillis(100)).build());
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    ThreadPoolBulkhead bulkhead = ThreadPoolBulkhead.ofDefaults("helloBackend");
    CompletionStage<String> completionStage = Decorators.ofCallable(() -> {
        Thread.sleep(1000);
        return "Bla";
    }).withThreadPoolBulkhead(bulkhead).withTimeLimiter(timeLimiter, Executors.newSingleThreadScheduledExecutor()).withCircuitBreaker(circuitBreaker).get();
    assertThatThrownBy(() -> completionStage.toCompletableFuture().get()).hasCauseInstanceOf(TimeoutException.class);
    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) Test(org.junit.Test)

Aggregations

TimeLimiter (io.github.resilience4j.timelimiter.TimeLimiter)37 Test (org.junit.Test)31 TimeLimiterRegistry (io.github.resilience4j.timelimiter.TimeLimiterRegistry)9 Duration (java.time.Duration)7 ThreadPoolBulkhead (io.github.resilience4j.bulkhead.ThreadPoolBulkhead)4 CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)4 Counter (io.micrometer.core.instrument.Counter)4 ContextAwareScheduledThreadPoolExecutor (io.github.resilience4j.core.ContextAwareScheduledThreadPoolExecutor)3 TimeLimiterConfig (io.github.resilience4j.timelimiter.TimeLimiterConfig)3 TimeLimiterEvent (io.github.resilience4j.timelimiter.event.TimeLimiterEvent)3 DefaultEventConsumerRegistry (io.github.resilience4j.consumer.DefaultEventConsumerRegistry)2 TestThreadLocalContextPropagatorWithHolder (io.github.resilience4j.test.TestContextPropagators.TestThreadLocalContextPropagatorWithHolder)2 List (java.util.List)2 TimeoutException (java.util.concurrent.TimeoutException)2 Awaitility.matches (com.jayway.awaitility.Awaitility.matches)1 Awaitility.waitAtMost (com.jayway.awaitility.Awaitility.waitAtMost)1 Bulkhead (io.github.resilience4j.bulkhead.Bulkhead)1 BulkheadFullException (io.github.resilience4j.bulkhead.BulkheadFullException)1 Cache (io.github.resilience4j.cache.Cache)1 CallNotPermittedException (io.github.resilience4j.circuitbreaker.CallNotPermittedException)1