Search in sources :

Example 26 with TimeLimiter

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);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) Counter(io.micrometer.core.instrument.Counter) Test(org.junit.Test)

Example 27 with TimeLimiter

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();
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) Test(org.junit.Test)

Example 28 with TimeLimiter

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);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) TimeLimiterRegistry(io.github.resilience4j.timelimiter.TimeLimiterRegistry) TimeLimiterConfig(io.github.resilience4j.timelimiter.TimeLimiterConfig) Test(org.junit.Test)

Example 29 with TimeLimiter

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);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) Duration(java.time.Duration) Test(org.junit.Test)

Example 30 with TimeLimiter

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);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) Duration(java.time.Duration) 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