Search in sources :

Example 11 with TimeLimiter

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

the class InMemoryTimeLimiterRegistryTest method timeLimiterGetAllTimeLimiters.

@Test
public void timeLimiterGetAllTimeLimiters() {
    TimeLimiterRegistry registry = new InMemoryTimeLimiterRegistry(config);
    final TimeLimiter timeLimiter = registry.timeLimiter("foo");
    then(registry.getAllTimeLimiters().size()).isEqualTo(1);
    then(registry.getAllTimeLimiters()).contains(timeLimiter);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) TimeLimiterRegistry(io.github.resilience4j.timelimiter.TimeLimiterRegistry) Test(org.junit.Test)

Example 12 with TimeLimiter

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

the class TimeLimiterTest method unwrapExecutionException.

@Test
public void unwrapExecutionException() {
    TimeLimiter timeLimiter = TimeLimiter.ofDefaults();
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    Supplier<Future<Integer>> supplier = () -> executorService.submit(() -> {
        throw new RuntimeException();
    });
    Callable<Integer> decorated = TimeLimiter.decorateFutureSupplier(timeLimiter, supplier);
    Try<Integer> decoratedResult = Try.ofCallable(decorated);
    assertThat(decoratedResult.getCause() instanceof RuntimeException).isTrue();
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) Test(org.junit.Test)

Example 13 with TimeLimiter

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

the class TimeLimiterTest method shouldThrowTimeoutExceptionAndInvokeCancel.

@Test
public void shouldThrowTimeoutExceptionAndInvokeCancel() throws Exception {
    Duration timeoutDuration = Duration.ofSeconds(1);
    TimeLimiterConfig timeLimiterConfig = TimeLimiterConfig.custom().timeoutDuration(timeoutDuration).build();
    TimeLimiter timeLimiter = TimeLimiter.of(TIME_LIMITER_NAME, timeLimiterConfig);
    @SuppressWarnings("unchecked") Future<Integer> mockFuture = (Future<Integer>) mock(Future.class);
    Supplier<Future<Integer>> supplier = () -> mockFuture;
    given(mockFuture.get(timeoutDuration.toMillis(), TimeUnit.MILLISECONDS)).willThrow(new TimeoutException());
    Callable<Integer> decorated = TimeLimiter.decorateFutureSupplier(timeLimiter, supplier);
    Try<Integer> decoratedResult = Try.ofCallable(decorated);
    assertThat(decoratedResult.isFailure()).isTrue();
    assertThat(decoratedResult.getCause()).isInstanceOf(TimeoutException.class);
    assertThat(decoratedResult.getCause()).hasMessage(TimeLimiter.createdTimeoutExceptionWithName(TIME_LIMITER_NAME, null).getMessage());
    then(mockFuture).should().cancel(true);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) Duration(java.time.Duration) TimeLimiterConfig(io.github.resilience4j.timelimiter.TimeLimiterConfig) Test(org.junit.Test)

Example 14 with TimeLimiter

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

the class TimeLimiterTest method shouldThrowTimeoutExceptionAndNotInvokeCancel.

@Test
public void shouldThrowTimeoutExceptionAndNotInvokeCancel() throws Exception {
    Duration timeoutDuration = Duration.ofSeconds(1);
    TimeLimiter timeLimiter = TimeLimiter.of(TimeLimiterConfig.custom().timeoutDuration(timeoutDuration).cancelRunningFuture(false).build());
    @SuppressWarnings("unchecked") Future<Integer> mockFuture = (Future<Integer>) mock(Future.class);
    Supplier<Future<Integer>> supplier = () -> mockFuture;
    given(mockFuture.get(timeoutDuration.toMillis(), TimeUnit.MILLISECONDS)).willThrow(new TimeoutException());
    Callable<Integer> decorated = TimeLimiter.decorateFutureSupplier(timeLimiter, supplier);
    Try<Integer> decoratedResult = Try.ofCallable(decorated);
    assertThat(decoratedResult.isFailure()).isTrue();
    assertThat(decoratedResult.getCause()).isInstanceOf(TimeoutException.class);
    then(mockFuture).should(never()).cancel(true);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) Duration(java.time.Duration) Test(org.junit.Test)

Example 15 with TimeLimiter

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

the class TimeLimiterTest method shouldSetGivenName.

@Test
public void shouldSetGivenName() {
    TimeLimiter timeLimiter = TimeLimiter.ofDefaults("TEST");
    assertThat(timeLimiter.getName()).isEqualTo("TEST");
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) 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