Search in sources :

Example 6 with TimeLimiter

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

the class AbstractTimeLimiterMetricsTest method shouldRegisterMetrics.

@Test
public void shouldRegisterMetrics() throws Exception {
    TimeLimiter timeLimiter = given(metricRegistry);
    String expectedPrefix = "resilience4j.timelimiter.testLimit.";
    Supplier<CompletableFuture<String>> futureSupplier = () -> CompletableFuture.completedFuture("Hello world");
    String result = timeLimiter.decorateFutureSupplier(futureSupplier).call();
    then(result).isEqualTo("Hello world");
    assertThat(metricRegistry).hasMetricsSize(3);
    assertThat(metricRegistry).counter(expectedPrefix + SUCCESSFUL).hasValue(1L);
    assertThat(metricRegistry).counter(expectedPrefix + FAILED).hasValue(0L);
    assertThat(metricRegistry).counter(expectedPrefix + TIMEOUT).hasValue(0L);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test)

Example 7 with TimeLimiter

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

the class TaggedTimeLimiterMetricsPublisherTest method shouldAddMetricsForANewlyCreatedTimeLimiter.

@Test
public void shouldAddMetricsForANewlyCreatedTimeLimiter() {
    TimeLimiter newTimeLimiter = timeLimiterRegistry.timeLimiter("backendB");
    newTimeLimiter.onSuccess();
    assertThat(taggedTimeLimiterMetricsPublisher.meterIdMap).containsKeys("backendA", "backendB");
    assertThat(taggedTimeLimiterMetricsPublisher.meterIdMap.get("backendA")).hasSize(3);
    assertThat(taggedTimeLimiterMetricsPublisher.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 8 with TimeLimiter

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

the class TaggedTimeLimiterMetricsPublisherTest method testReplaceNewMeter.

@Test
public void testReplaceNewMeter() {
    TimeLimiter oldOne = TimeLimiter.of("backendC", TimeLimiterConfig.ofDefaults());
    // add meters of old
    taggedTimeLimiterMetricsPublisher.addMetrics(meterRegistry, oldOne);
    // one success call
    oldOne.onSuccess();
    assertThat(taggedTimeLimiterMetricsPublisher.meterIdMap).containsKeys("backendC");
    assertThat(taggedTimeLimiterMetricsPublisher.meterIdMap.get("backendC")).hasSize(3);
    Collection<Counter> counters = meterRegistry.get(DEFAULT_TIME_LIMITER_CALLS).counters();
    Optional<Counter> successful = findMeterByKindAndNameTags(counters, "successful", oldOne.getName());
    assertThat(successful).map(Counter::count).contains(1d);
    TimeLimiter newOne = TimeLimiter.of("backendC", TimeLimiterConfig.ofDefaults());
    // add meters of new
    taggedTimeLimiterMetricsPublisher.addMetrics(meterRegistry, newOne);
    // three success call
    newOne.onSuccess();
    newOne.onSuccess();
    newOne.onSuccess();
    assertThat(taggedTimeLimiterMetricsPublisher.meterIdMap).containsKeys("backendC");
    assertThat(taggedTimeLimiterMetricsPublisher.meterIdMap.get("backendC")).hasSize(3);
    counters = meterRegistry.get(DEFAULT_TIME_LIMITER_CALLS).counters();
    successful = findMeterByKindAndNameTags(counters, "successful", newOne.getName());
    assertThat(successful).map(Counter::count).contains(3d);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) Counter(io.micrometer.core.instrument.Counter) Test(org.junit.Test)

Example 9 with TimeLimiter

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

the class TaggedTimeLimiterMetrics method bindTo.

@Override
public void bindTo(MeterRegistry registry) {
    for (TimeLimiter timeLimiter : timeLimiterRegistry.getAllTimeLimiters()) {
        addMetrics(registry, timeLimiter);
    }
    timeLimiterRegistry.getEventPublisher().onEntryAdded(event -> addMetrics(registry, event.getAddedEntry()));
    timeLimiterRegistry.getEventPublisher().onEntryRemoved(event -> removeMetrics(registry, event.getRemovedEntry().getName()));
    timeLimiterRegistry.getEventPublisher().onEntryReplaced(event -> {
        removeMetrics(registry, event.getOldEntry().getName());
        addMetrics(registry, event.getNewEntry());
    });
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter)

Example 10 with TimeLimiter

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

the class InMemoryTimeLimiterRegistryTest method timeLimiterPositive.

@Test
public void timeLimiterPositive() {
    TimeLimiterRegistry registry = TimeLimiterRegistry.of(config);
    TimeLimiter firstTimeLimiter = registry.timeLimiter("test");
    TimeLimiter anotherLimit = registry.timeLimiter("test1");
    TimeLimiter sameAsFirst = registry.timeLimiter("test");
    then(firstTimeLimiter).isEqualTo(sameAsFirst);
    then(firstTimeLimiter).isNotEqualTo(anotherLimit);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) TimeLimiterRegistry(io.github.resilience4j.timelimiter.TimeLimiterRegistry) 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