Search in sources :

Example 1 with RateLimiterConfigurationProperties

use of io.github.resilience4j.ratelimiter.configure.RateLimiterConfigurationProperties in project resilience4j by resilience4j.

the class RateLimitersHealthIndicatorTest method healthIndicatorMaxImpactCanBeOverridden.

@Test
public void healthIndicatorMaxImpactCanBeOverridden() throws Exception {
    // given
    RateLimiterConfig config = mock(RateLimiterConfig.class);
    AtomicRateLimiter.AtomicRateLimiterMetrics metrics = mock(AtomicRateLimiter.AtomicRateLimiterMetrics.class);
    AtomicRateLimiter rateLimiter = mock(AtomicRateLimiter.class);
    RateLimiterRegistry rateLimiterRegistry = mock(RateLimiterRegistry.class);
    io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties instanceProperties = mock(io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties.class);
    RateLimiterConfigurationProperties rateLimiterProperties = mock(RateLimiterConfigurationProperties.class);
    // when
    when(rateLimiter.getRateLimiterConfig()).thenReturn(config);
    when(rateLimiter.getName()).thenReturn("test");
    when(rateLimiterProperties.findRateLimiterProperties("test")).thenReturn(Optional.of(instanceProperties));
    when(instanceProperties.getRegisterHealthIndicator()).thenReturn(true);
    // do not allow health indicator to fail
    boolean allowHealthIndicatorToFail = false;
    when(instanceProperties.getAllowHealthIndicatorToFail()).thenReturn(allowHealthIndicatorToFail);
    when(rateLimiter.getMetrics()).thenReturn(metrics);
    when(rateLimiter.getDetailedMetrics()).thenReturn(metrics);
    when(rateLimiterRegistry.getAllRateLimiters()).thenReturn(Set.of(rateLimiter));
    when(config.getTimeoutDuration()).thenReturn(Duration.ofNanos(30L));
    when(metrics.getAvailablePermissions()).thenReturn(-2);
    when(metrics.getNumberOfWaitingThreads()).thenReturn(2);
    when(metrics.getNanosToWait()).thenReturn(40L);
    // then
    RateLimitersHealthIndicator healthIndicator = new RateLimitersHealthIndicator(rateLimiterRegistry, rateLimiterProperties, new SimpleStatusAggregator());
    Health health = healthIndicator.health();
    then(health.getStatus()).isEqualTo(Status.UNKNOWN);
    then(((Health) health.getDetails().get("test")).getStatus()).isEqualTo(new Status("RATE_LIMITED"));
    then(health.getDetails().get("test")).isInstanceOf(Health.class);
    then(((Health) health.getDetails().get("test")).getDetails()).contains(entry("availablePermissions", -2), entry("numberOfWaitingThreads", 2));
}
Also used : Status(org.springframework.boot.actuate.health.Status) RateLimiterConfigurationProperties(io.github.resilience4j.ratelimiter.configure.RateLimiterConfigurationProperties) SimpleStatusAggregator(org.springframework.boot.actuate.health.SimpleStatusAggregator) Health(org.springframework.boot.actuate.health.Health) RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig) RateLimiterRegistry(io.github.resilience4j.ratelimiter.RateLimiterRegistry) AtomicRateLimiter(io.github.resilience4j.ratelimiter.internal.AtomicRateLimiter) Test(org.junit.Test)

Example 2 with RateLimiterConfigurationProperties

use of io.github.resilience4j.ratelimiter.configure.RateLimiterConfigurationProperties in project resilience4j by resilience4j.

the class RateLimitersHealthIndicatorTest method health.

@Test
public void health() throws Exception {
    // given
    RateLimiterConfig config = mock(RateLimiterConfig.class);
    AtomicRateLimiter.AtomicRateLimiterMetrics metrics = mock(AtomicRateLimiter.AtomicRateLimiterMetrics.class);
    AtomicRateLimiter rateLimiter = mock(AtomicRateLimiter.class);
    RateLimiterRegistry rateLimiterRegistry = mock(RateLimiterRegistry.class);
    io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties instanceProperties = mock(io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties.class);
    RateLimiterConfigurationProperties rateLimiterProperties = mock(RateLimiterConfigurationProperties.class);
    // when
    when(rateLimiter.getRateLimiterConfig()).thenReturn(config);
    when(rateLimiter.getName()).thenReturn("test");
    when(rateLimiterProperties.findRateLimiterProperties("test")).thenReturn(Optional.of(instanceProperties));
    when(instanceProperties.getRegisterHealthIndicator()).thenReturn(true);
    when(instanceProperties.getAllowHealthIndicatorToFail()).thenReturn(true);
    when(rateLimiter.getMetrics()).thenReturn(metrics);
    when(rateLimiter.getDetailedMetrics()).thenReturn(metrics);
    when(rateLimiterRegistry.getAllRateLimiters()).thenReturn(Set.of(rateLimiter));
    when(config.getTimeoutDuration()).thenReturn(Duration.ofNanos(30L));
    when(metrics.getAvailablePermissions()).thenReturn(5, -1, -2);
    when(metrics.getNumberOfWaitingThreads()).thenReturn(0, 1, 2);
    when(metrics.getNanosToWait()).thenReturn(20L, 40L);
    // then
    RateLimitersHealthIndicator healthIndicator = new RateLimitersHealthIndicator(rateLimiterRegistry, rateLimiterProperties, new SimpleStatusAggregator());
    Health health = healthIndicator.health();
    then(health.getStatus()).isEqualTo(Status.UP);
    health = healthIndicator.health();
    then(health.getStatus()).isEqualTo(Status.UNKNOWN);
    health = healthIndicator.health();
    then(health.getStatus()).isEqualTo(Status.DOWN);
    then(health.getDetails().get("test")).isInstanceOf(Health.class);
    then(((Health) health.getDetails().get("test")).getDetails()).contains(entry("availablePermissions", -2), entry("numberOfWaitingThreads", 2));
}
Also used : RateLimiterConfigurationProperties(io.github.resilience4j.ratelimiter.configure.RateLimiterConfigurationProperties) SimpleStatusAggregator(org.springframework.boot.actuate.health.SimpleStatusAggregator) Health(org.springframework.boot.actuate.health.Health) RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig) RateLimiterRegistry(io.github.resilience4j.ratelimiter.RateLimiterRegistry) AtomicRateLimiter(io.github.resilience4j.ratelimiter.internal.AtomicRateLimiter) Test(org.junit.Test)

Example 3 with RateLimiterConfigurationProperties

use of io.github.resilience4j.ratelimiter.configure.RateLimiterConfigurationProperties in project resilience4j by resilience4j.

the class RefreshScopedRateLimiterConfigurationTest method testRateLimiterCloudCommonConfig.

@Test
public void testRateLimiterCloudCommonConfig() {
    RateLimiterConfig rateLimiterConfig = new RateLimiterConfig();
    assertThat(rateLimiterConfig.rateLimiterRegistry(new RateLimiterConfigurationProperties(), new DefaultEventConsumerRegistry<>(), new CompositeRegistryEventConsumer<>(emptyList()), new CompositeCustomizer<>(Collections.emptyList()))).isNotNull();
}
Also used : RateLimiterConfigurationProperties(io.github.resilience4j.ratelimiter.configure.RateLimiterConfigurationProperties) Test(org.junit.Test)

Example 4 with RateLimiterConfigurationProperties

use of io.github.resilience4j.ratelimiter.configure.RateLimiterConfigurationProperties in project resilience4j by resilience4j.

the class SpringBootCommonTest method testRateLimiterCommonConfig.

@Test
public void testRateLimiterCommonConfig() {
    RateLimiterConfigurationOnMissingBean rateLimiterConfigurationOnMissingBean = new RateLimiterConfigurationOnMissingBean();
    assertThat(rateLimiterConfigurationOnMissingBean.reactorRateLimiterAspectExt()).isNotNull();
    assertThat(rateLimiterConfigurationOnMissingBean.rxJava2RateLimiterAspectExt()).isNotNull();
    assertThat(rateLimiterConfigurationOnMissingBean.rateLimiterRegistry(new RateLimiterConfigurationProperties(), new DefaultEventConsumerRegistry<>(), new CompositeRegistryEventConsumer<>(Collections.emptyList()), new CompositeCustomizer<>(Collections.emptyList()))).isNotNull();
    final FallbackDecorators fallbackDecorators = new FallbackDecorators(Arrays.asList(new CompletionStageFallbackDecorator()));
    final DefaultSpelResolver spelResolver = new DefaultSpelResolver(new SpelExpressionParser(), new StandardReflectionParameterNameDiscoverer(), new GenericApplicationContext());
    assertThat(rateLimiterConfigurationOnMissingBean.rateLimiterAspect(new RateLimiterConfigurationProperties(), RateLimiterRegistry.ofDefaults(), Collections.emptyList(), new FallbackExecutor(spelResolver, fallbackDecorators), spelResolver)).isNotNull();
    assertThat(rateLimiterConfigurationOnMissingBean.rateLimiterRegistryEventConsumer(Optional.empty())).isNotNull();
}
Also used : RateLimiterConfigurationProperties(io.github.resilience4j.ratelimiter.configure.RateLimiterConfigurationProperties) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardReflectionParameterNameDiscoverer(org.springframework.core.StandardReflectionParameterNameDiscoverer) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) CompletionStageFallbackDecorator(io.github.resilience4j.fallback.CompletionStageFallbackDecorator) AbstractRateLimiterConfigurationOnMissingBean(io.github.resilience4j.ratelimiter.autoconfigure.AbstractRateLimiterConfigurationOnMissingBean) DefaultSpelResolver(io.github.resilience4j.spelresolver.DefaultSpelResolver) FallbackDecorators(io.github.resilience4j.fallback.FallbackDecorators) FallbackExecutor(io.github.resilience4j.fallback.FallbackExecutor) Test(org.junit.Test)

Aggregations

RateLimiterConfigurationProperties (io.github.resilience4j.ratelimiter.configure.RateLimiterConfigurationProperties)4 Test (org.junit.Test)4 RateLimiterConfig (io.github.resilience4j.ratelimiter.RateLimiterConfig)2 RateLimiterRegistry (io.github.resilience4j.ratelimiter.RateLimiterRegistry)2 AtomicRateLimiter (io.github.resilience4j.ratelimiter.internal.AtomicRateLimiter)2 Health (org.springframework.boot.actuate.health.Health)2 SimpleStatusAggregator (org.springframework.boot.actuate.health.SimpleStatusAggregator)2 CompletionStageFallbackDecorator (io.github.resilience4j.fallback.CompletionStageFallbackDecorator)1 FallbackDecorators (io.github.resilience4j.fallback.FallbackDecorators)1 FallbackExecutor (io.github.resilience4j.fallback.FallbackExecutor)1 AbstractRateLimiterConfigurationOnMissingBean (io.github.resilience4j.ratelimiter.autoconfigure.AbstractRateLimiterConfigurationOnMissingBean)1 DefaultSpelResolver (io.github.resilience4j.spelresolver.DefaultSpelResolver)1 Status (org.springframework.boot.actuate.health.Status)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1 StandardReflectionParameterNameDiscoverer (org.springframework.core.StandardReflectionParameterNameDiscoverer)1 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)1