use of io.github.resilience4j.timelimiter.TimeLimiter in project resilience4j by resilience4j.
the class TaggedTimeLimiterMetricsTest method shouldAddMetricsForANewlyCreatedTimeLimiter.
@Test
public void shouldAddMetricsForANewlyCreatedTimeLimiter() {
TimeLimiter newTimeLimiter = timeLimiterRegistry.timeLimiter("backendB");
newTimeLimiter.onSuccess();
assertThat(taggedTimeLimiterMetrics.meterIdMap).containsKeys("backendA", "backendB");
assertThat(taggedTimeLimiterMetrics.meterIdMap.get("backendA")).hasSize(3);
assertThat(taggedTimeLimiterMetrics.meterIdMap.get("backendB")).hasSize(3);
assertThat(meterRegistry.getMeters()).hasSize(6);
Collection<Counter> counters = meterRegistry.get(DEFAULT_TIME_LIMITER_CALLS).counters();
Optional<Counter> successful = findMeterByKindAndNameTags(counters, "successful", newTimeLimiter.getName());
assertThat(successful).map(Counter::count).contains(1d);
}
use of io.github.resilience4j.timelimiter.TimeLimiter in project resilience4j by resilience4j.
the class RxJava2TimeLimiterAspectExtTest method testRxJava2Types.
@Test
public void testRxJava2Types() throws Throwable {
TimeLimiter timeLimiter = TimeLimiter.ofDefaults("test");
when(proceedingJoinPoint.proceed()).thenReturn(Single.just("Test"));
assertThat(rxJava2TimeLimiterAspectExt.handle(proceedingJoinPoint, timeLimiter, "testMethod")).isNotNull();
when(proceedingJoinPoint.proceed()).thenReturn(Flowable.just("Test"));
assertThat(rxJava2TimeLimiterAspectExt.handle(proceedingJoinPoint, timeLimiter, "testMethod")).isNotNull();
when(proceedingJoinPoint.proceed()).thenReturn(Observable.just("Test"));
assertThat(rxJava2TimeLimiterAspectExt.handle(proceedingJoinPoint, timeLimiter, "testMethod")).isNotNull();
when(proceedingJoinPoint.proceed()).thenReturn(Completable.complete());
assertThat(rxJava2TimeLimiterAspectExt.handle(proceedingJoinPoint, timeLimiter, "testMethod")).isNotNull();
when(proceedingJoinPoint.proceed()).thenReturn(Maybe.just("Test"));
assertThat(rxJava2TimeLimiterAspectExt.handle(proceedingJoinPoint, timeLimiter, "testMethod")).isNotNull();
}
use of io.github.resilience4j.timelimiter.TimeLimiter in project resilience4j by resilience4j.
the class InMemoryTimeLimiterRegistryTest method timeLimiterPositiveWithSupplier.
@Test
@SuppressWarnings("unchecked")
public void timeLimiterPositiveWithSupplier() {
TimeLimiterRegistry registry = new InMemoryTimeLimiterRegistry(config);
Supplier<TimeLimiterConfig> timeLimiterConfigSupplier = mock(Supplier.class);
given(timeLimiterConfigSupplier.get()).willReturn(config);
TimeLimiter firstTimeLimiter = registry.timeLimiter("test", timeLimiterConfigSupplier);
verify(timeLimiterConfigSupplier, times(1)).get();
TimeLimiter sameAsFirst = registry.timeLimiter("test", timeLimiterConfigSupplier);
verify(timeLimiterConfigSupplier, times(1)).get();
TimeLimiter anotherLimit = registry.timeLimiter("test1", timeLimiterConfigSupplier);
verify(timeLimiterConfigSupplier, times(2)).get();
then(firstTimeLimiter).isEqualTo(sameAsFirst);
then(firstTimeLimiter).isNotEqualTo(anotherLimit);
}
use of io.github.resilience4j.timelimiter.TimeLimiter in project resilience4j by resilience4j.
the class TimeLimiterTest method shouldThrowTimeoutExceptionWithCompletionStage.
@Test
public void shouldThrowTimeoutExceptionWithCompletionStage() throws Exception {
Duration timeoutDuration = Duration.ofMillis(300);
TimeLimiter timeLimiter = TimeLimiter.of(timeoutDuration);
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Supplier<CompletionStage<Integer>> supplier = () -> CompletableFuture.supplyAsync(() -> {
try {
// sleep for timeout.
Thread.sleep(500);
} catch (InterruptedException e) {
// nothing
}
return 0;
});
CompletionStage<Integer> decorated = TimeLimiter.decorateCompletionStage(timeLimiter, scheduler, supplier).get();
Try<Integer> decoratedResult = Try.ofCallable(() -> decorated.toCompletableFuture().get());
assertThat(decoratedResult.isFailure()).isTrue();
assertThat(decoratedResult.getCause()).isInstanceOf(ExecutionException.class).hasCauseExactlyInstanceOf(TimeoutException.class);
}
use of io.github.resilience4j.timelimiter.TimeLimiter in project resilience4j by resilience4j.
the class TimeLimiterTest method shouldReturnResult.
@Test
public void shouldReturnResult() throws Exception {
Duration timeoutDuration = Duration.ofSeconds(1);
TimeLimiter timeLimiter = TimeLimiter.of(timeoutDuration);
@SuppressWarnings("unchecked") Future<Integer> mockFuture = (Future<Integer>) mock(Future.class);
Supplier<Future<Integer>> supplier = () -> mockFuture;
given(mockFuture.get(timeoutDuration.toMillis(), TimeUnit.MILLISECONDS)).willReturn(42);
int result = timeLimiter.executeFutureSupplier(supplier);
assertThat(result).isEqualTo(42);
int result2 = timeLimiter.decorateFutureSupplier(supplier).call();
assertThat(result2).isEqualTo(42);
}
Aggregations