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);
}
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();
}
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);
}
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);
}
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");
}
Aggregations