Skip to content

Commit

Permalink
Merge pull request ReactiveX#132 from resilience4j/additional_tests
Browse files Browse the repository at this point in the history
Additional tests
  • Loading branch information
storozhukBM authored May 11, 2017
2 parents bba575e + ad29b28 commit 9bed32d
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ private State(final long activeCycle, final int activePermissions, final long na
/**
* Enhanced {@link Metrics} with some implementation specific details
*/
public final class AtomicRateLimiterMetrics implements Metrics {
public class AtomicRateLimiterMetrics implements Metrics {

private AtomicRateLimiterMetrics() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Health health() {
AtomicRateLimiter atomicRateLimiter = (AtomicRateLimiter) this.rateLimiter;
AtomicRateLimiter.AtomicRateLimiterMetrics detailedMetrics = atomicRateLimiter.getDetailedMetrics();
if (detailedMetrics.getNanosToWait() > timeoutInNanos) {
rateLimiterHealth(Status.DOWN, availablePermissions, numberOfWaitingThreads);
return rateLimiterHealth(Status.DOWN, availablePermissions, numberOfWaitingThreads);
}
}
return rateLimiterHealth(Status.UNKNOWN, availablePermissions, numberOfWaitingThreads);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.github.resilience4j.circuitbreaker.monitoring.health;

import static org.assertj.core.api.BDDAssertions.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static io.github.resilience4j.circuitbreaker.CircuitBreaker.State.CLOSED;
import static io.github.resilience4j.circuitbreaker.CircuitBreaker.State.HALF_OPEN;
import static io.github.resilience4j.circuitbreaker.CircuitBreaker.State.OPEN;

import org.junit.Test;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;

import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;

import java.util.AbstractMap.SimpleEntry;

/**
* @author bstorozhuk
*/
public class CircuitBreakerHealthIndicatorTest {
@Test
public void health() throws Exception {
// given
CircuitBreakerConfig config = mock(CircuitBreakerConfig.class);
CircuitBreaker.Metrics metrics = mock(CircuitBreaker.Metrics.class);
CircuitBreaker circuitBreaker = mock(CircuitBreaker.class);
CircuitBreakerHealthIndicator healthIndicator = new CircuitBreakerHealthIndicator(circuitBreaker);

//when
when(config.getFailureRateThreshold()).thenReturn(0.3f);

when(metrics.getFailureRate()).thenReturn(0.2f);
when(metrics.getMaxNumberOfBufferedCalls()).thenReturn(100);
when(metrics.getNumberOfBufferedCalls()).thenReturn(100);
when(metrics.getNumberOfFailedCalls()).thenReturn(20);
when(metrics.getNumberOfNotPermittedCalls()).thenReturn(0L);


when(circuitBreaker.getCircuitBreakerConfig()).thenReturn(config);
when(circuitBreaker.getMetrics()).thenReturn(metrics);
when(circuitBreaker.getState()).thenReturn(CLOSED, OPEN, HALF_OPEN, CLOSED);

// then
Health health = healthIndicator.health();
then(health.getStatus()).isEqualTo(Status.UP);

health = healthIndicator.health();
then(health.getStatus()).isEqualTo(Status.DOWN);

health = healthIndicator.health();
then(health.getStatus()).isEqualTo(Status.UNKNOWN);

health = healthIndicator.health();
then(health.getStatus()).isEqualTo(Status.UP);

then(health.getDetails())
.contains(
entry("failureRate", "0.2%"),
entry("failureRateThreshold", "0.3%"),
entry("bufferedCalls", 100),
entry("failedCalls", 20),
entry("notPermittedCalls", 0L),
entry("maxBufferedCalls", 100)
);
}

private SimpleEntry<String, ?> entry(String key, Object value) {
return new SimpleEntry<>(key, value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.github.resilience4j.ratelimiter.monitoring.health;

import static org.assertj.core.api.BDDAssertions.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.Test;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;

import io.github.resilience4j.ratelimiter.RateLimiterConfig;
import io.github.resilience4j.ratelimiter.internal.AtomicRateLimiter;

import java.time.Duration;
import java.util.AbstractMap.SimpleEntry;

/**
* @author bstorozhuk
*/
public class RateLimiterHealthIndicatorTest {
@Test
public void health() throws Exception {
// given
RateLimiterConfig config = mock(RateLimiterConfig.class);
AtomicRateLimiter.AtomicRateLimiterMetrics metrics = mock(AtomicRateLimiter.AtomicRateLimiterMetrics.class);
AtomicRateLimiter rateLimiter = mock(AtomicRateLimiter.class);

//when

when(rateLimiter.getRateLimiterConfig()).thenReturn(config);
when(rateLimiter.getMetrics()).thenReturn(metrics);
when(rateLimiter.getDetailedMetrics()).thenReturn(metrics);

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
RateLimiterHealthIndicator healthIndicator = new RateLimiterHealthIndicator(rateLimiter);

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())
.contains(
entry("availablePermissions", -2),
entry("numberOfWaitingThreads", 2)
);


}

private SimpleEntry<String, ?> entry(String key, Object value) {
return new SimpleEntry<>(key, value);
}
}

0 comments on commit 9bed32d

Please sign in to comment.