Skip to content

Commit

Permalink
Issue ReactiveX#515: Aspects ordering update (ReactiveX#534)
Browse files Browse the repository at this point in the history
* ReactiveX#515  ordering update

* ReactiveX#515 aspects ordering update

* ReactiveX#515 unit test update

* ReactiveX#515 test coverage increase
  • Loading branch information
Romeh authored and RobWin committed Jul 9, 2019
1 parent 2bbe53c commit bf2d962
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.core.Ordered;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

Expand Down Expand Up @@ -119,7 +118,7 @@ public void testCircuitBreakerAutoConfiguration() throws IOException {
// setRecordExceptions evaluates to false.
assertThat(circuitBreaker.getCircuitBreakerConfig().getRecordFailurePredicate().test(new Exception())).isFalse();

assertThat(circuitBreakerAspect.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE - 2);
assertThat(circuitBreakerAspect.getOrder()).isEqualTo(400);

// expect all shared configs share the same values and are from the application.yml file
CircuitBreaker sharedA = circuitBreakerRegistry.circuitBreaker("backendSharedA");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.core.Ordered;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

Expand Down Expand Up @@ -129,6 +128,6 @@ public void testRateLimiterAutoConfiguration() throws IOException {
.until(() -> rateLimiter.getMetrics().getAvailablePermissions() == 10);


assertThat(rateLimiterAspect.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE - 1);
assertThat(rateLimiterAspect.getOrder()).isEqualTo(401);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.core.Ordered;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

Expand Down Expand Up @@ -99,6 +98,6 @@ public void testRetryAutoConfiguration() throws IOException {
assertThat(retry.getRetryConfig().getExceptionPredicate().test(new IgnoredException())).isFalse();

// expect aspect configured as defined in application.yml
assertThat(retryAspect.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE - 3);
assertThat(retryAspect.getOrder()).isEqualTo(399);
}
}
3 changes: 3 additions & 0 deletions resilience4j-spring-boot/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
resilience4j.retry:
retryAspectOrder: 399
backends:
retryBackendA:
maxRetryAttempts: 3
Expand All @@ -17,6 +18,7 @@ resilience4j.retry:
ignoreExceptions:
- io.github.resilience4j.circuitbreaker.IgnoredException
resilience4j.circuitbreaker:
circuitBreakerAspectOrder: 400
configs:
default:
ringBufferSizeInClosedState: 100
Expand Down Expand Up @@ -52,6 +54,7 @@ resilience4j.circuitbreaker:
baseConfig: default

resilience4j.ratelimiter:
rateLimiterAspectOrder: 401
limiters:
backendA:
limitForPeriod: 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.core.Ordered;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Expand Down Expand Up @@ -125,7 +124,7 @@ public void testCircuitBreakerAutoConfiguration() throws IOException {
// setRecordExceptions evaluates to false.
assertThat(circuitBreaker.getCircuitBreakerConfig().getRecordFailurePredicate().test(new Exception())).isFalse();

assertThat(circuitBreakerAspect.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE - 2);
assertThat(circuitBreakerAspect.getOrder()).isEqualTo(400);

// expect all shared configs share the same values and are from the application.yml file
CircuitBreaker sharedA = circuitBreakerRegistry.circuitBreaker("backendSharedA");
Expand Down Expand Up @@ -246,7 +245,7 @@ public void testCircuitBreakerAutoConfigurationReactive() throws IOException {
assertThat(healthResponse.getBody().getDetails().get("backendACircuitBreaker")).isNotNull();
assertThat(healthResponse.getBody().getDetails().get("backendBCircuitBreaker")).isNull();

assertThat(circuitBreakerAspect.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE - 2);
assertThat(circuitBreakerAspect.getOrder()).isEqualTo(400);
assertThat(circuitBreaker.getMetrics().getNumberOfBufferedCalls()).isEqualTo(2);
assertThat(circuitBreaker.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(1);
assertThat(circuitBreaker.getMetrics().getNumberOfFailedCalls()).isEqualTo(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.core.Ordered;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

Expand Down Expand Up @@ -119,6 +118,6 @@ public void testRateLimiterAutoConfiguration() throws IOException {
.until(() -> rateLimiter.getMetrics().getAvailablePermissions() == 10);


assertThat(rateLimiterAspect.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE - 1);
assertThat(rateLimiterAspect.getOrder()).isEqualTo(401);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.core.Ordered;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Expand Down Expand Up @@ -111,7 +110,7 @@ public void testRetryAutoConfigurationAsync() throws Throwable {
assertThat(retry.getMetrics().getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(1);
assertThat(retry.getMetrics().getNumberOfSuccessfulCallsWithoutRetryAttempt()).isEqualTo(1);
assertThat(retry.getMetrics().getNumberOfSuccessfulCallsWithRetryAttempt()).isEqualTo(0);
assertThat(retryAspect.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE - 3);
assertThat(retryAspect.getOrder()).isEqualTo(399);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.core.Ordered;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Expand Down Expand Up @@ -101,7 +100,7 @@ public void testRetryAutoConfigurationReactor() throws IOException {
assertThat(retry.getRetryConfig().getExceptionPredicate().test(new IllegalArgumentException())).isTrue();
assertThat(retry.getRetryConfig().getExceptionPredicate().test(new IgnoredException())).isFalse();

assertThat(retryAspect.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE - 3);
assertThat(retryAspect.getOrder()).isEqualTo(399);

assertThat(retry.getMetrics().getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
assertThat(retry.getMetrics().getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.core.Ordered;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Expand Down Expand Up @@ -101,7 +100,7 @@ public void testRetryAutoConfigurationRxJava2() throws IOException {
assertThat(retry.getRetryConfig().getExceptionPredicate().test(new IllegalArgumentException())).isTrue();
assertThat(retry.getRetryConfig().getExceptionPredicate().test(new IgnoredException())).isFalse();

assertThat(retryAspect.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE - 3);
assertThat(retryAspect.getOrder()).isEqualTo(399);

assertThat(retry.getMetrics().getNumberOfFailedCallsWithoutRetryAttempt()).isEqualTo(0);
assertThat(retry.getMetrics().getNumberOfFailedCallsWithRetryAttempt()).isEqualTo(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.core.Ordered;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Expand Down Expand Up @@ -104,6 +103,6 @@ public void testRetryAutoConfiguration() throws IOException {

assertThat(retry.getRetryConfig().getExceptionPredicate().test(new IOException())).isTrue();
assertThat(retry.getRetryConfig().getExceptionPredicate().test(new IgnoredException())).isFalse();
assertThat(retryAspect.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE - 3);
assertThat(retryAspect.getOrder()).isEqualTo(399);
}
}
3 changes: 3 additions & 0 deletions resilience4j-spring-boot2/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
resilience4j.retry:
retryAspectOrder: 399
configs:
default:
maxRetryAttempts: 3
Expand All @@ -23,6 +24,7 @@ resilience4j.retry:
- io.github.resilience4j.circuitbreaker.IgnoredException

resilience4j.circuitbreaker:
circuitBreakerAspectOrder: 400
configs:
default:
ringBufferSizeInClosedState: 100
Expand Down Expand Up @@ -57,6 +59,7 @@ resilience4j.circuitbreaker:
baseConfig: default

resilience4j.ratelimiter:
rateLimiterAspectOrder: 401
configs:
default:
limitForPeriod: 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class BulkheadConfigurationProperties extends io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties {

/**
* As of release 0.16.0 as we set an implicit spring aspect order now which is retry then circuit breaker then rate limiter then bulkhead
* As of release 0.16.0 as we set an implicit spring aspect order for bulkhead to cover the async case of threadPool bulkhead
*/
public int getBulkheadAspectOrder() {
return Ordered.LOWEST_PRECEDENCE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@

@Configuration
public class CircuitBreakerConfigurationProperties extends io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties {

private int circuitBreakerAspectOrder = Ordered.LOWEST_PRECEDENCE - 2;
/**
* As of release 0.16.0 as we set an implicit spring aspect order now which is retry then circuit breaker then rate limiter then bulkhead
* As of release 0.16.0 as we set an implicit spring aspect order now which is retry then circuit breaker then rate limiter then bulkhead but
* user can override it still if he has different use case but bulkhead will be first aspect all the time due to the implicit order we have it for bulkhead
*/
public int getCircuitBreakerAspectOrder() {
return Ordered.LOWEST_PRECEDENCE - 2;
return circuitBreakerAspectOrder;
}

/**
* @param circuitBreakerAspectOrder circuit breaker aspect order
*/
public void setCircuitBreakerAspectOrder(int circuitBreakerAspectOrder) {
this.circuitBreakerAspectOrder = circuitBreakerAspectOrder;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,21 @@
import org.springframework.core.Ordered;

public class RateLimiterConfigurationProperties extends io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties {

private int rateLimiterAspectOrder = Ordered.LOWEST_PRECEDENCE - 1;
/**
* As of release 0.16.0 as we set an implicit spring aspect order now which is retry then circuit breaker then rate limiter then bulkhead
* As of release 0.16.0 as we set an implicit spring aspect order now which is retry then circuit breaker then rate limiter then bulkhead but
* user can override it still if he has different use case but bulkhead will be first aspect all the time due to the implicit order we have it for bulkhead
*/
public int getRateLimiterAspectOrder() {
return Ordered.LOWEST_PRECEDENCE - 1;
return rateLimiterAspectOrder;
}

/**
* set rate limiter aspect order
*
* @param rateLimiterAspectOrder the aspect order
*/
public void setRateLimiterAspectOrder(int rateLimiterAspectOrder) {
this.rateLimiterAspectOrder = rateLimiterAspectOrder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,22 @@
* Main spring properties for retry configuration
*/
public class RetryConfigurationProperties extends io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties {

private int retryAspectOrder = Ordered.LOWEST_PRECEDENCE - 3;
/**
* As of release 0.16.0 as we set an implicit spring aspect order now which is retry then circuit breaker then rate limiter then bulkhead
* but the user can override it still if he has different use case but bulkhead will be first aspect all the time due to the implicit order we have it for bulkhead
*/
public int getRetryAspectOrder() {
return Ordered.LOWEST_PRECEDENCE - 3;
return retryAspectOrder;
}

/**
* set retry aspect order
*
* @param retryAspectOrder retry aspect target order
*/
public void setRetryAspectOrder(int retryAspectOrder) {
this.retryAspectOrder = retryAspectOrder;
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package io.github.resilience4j.circuitbreaker.configure;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
import io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent;
import io.github.resilience4j.consumer.DefaultEventConsumerRegistry;
import io.github.resilience4j.core.ConfigurationNotFoundException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
* test custom init of circuit breaker registry
Expand All @@ -28,6 +29,7 @@ public void testCreateCircuitBreakerRegistry() {
instanceProperties2.setRingBufferSizeInClosedState(1337);

CircuitBreakerConfigurationProperties circuitBreakerConfigurationProperties = new CircuitBreakerConfigurationProperties();
circuitBreakerConfigurationProperties.setCircuitBreakerAspectOrder(400);
circuitBreakerConfigurationProperties.getInstances().put("backend1", instanceProperties1);
circuitBreakerConfigurationProperties.getInstances().put("backend2", instanceProperties2);

Expand All @@ -38,6 +40,7 @@ public void testCreateCircuitBreakerRegistry() {
CircuitBreakerRegistry circuitBreakerRegistry = circuitBreakerConfiguration.circuitBreakerRegistry(eventConsumerRegistry);

//Then
assertThat(circuitBreakerConfigurationProperties.getCircuitBreakerAspectOrder()).isEqualTo(400);
assertThat(circuitBreakerRegistry.getAllCircuitBreakers().size()).isEqualTo(2);
CircuitBreaker circuitBreaker1 = circuitBreakerRegistry.circuitBreaker("backend1");
assertThat(circuitBreaker1).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package io.github.resilience4j.ratelimiter.configure;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.time.Duration;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import io.github.resilience4j.consumer.DefaultEventConsumerRegistry;
import io.github.resilience4j.core.ConfigurationNotFoundException;
import io.github.resilience4j.ratelimiter.RateLimiter;
import io.github.resilience4j.ratelimiter.RateLimiterRegistry;
import io.github.resilience4j.ratelimiter.event.RateLimiterEvent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import java.time.Duration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
* test custom init of rate limiter configuration
Expand All @@ -34,6 +35,7 @@ public void testRateLimiterRegistry() {
RateLimiterConfigurationProperties rateLimiterConfigurationProperties = new RateLimiterConfigurationProperties();
rateLimiterConfigurationProperties.getInstances().put("backend1", instanceProperties1);
rateLimiterConfigurationProperties.getInstances().put("backend2", instanceProperties2);
rateLimiterConfigurationProperties.setRateLimiterAspectOrder(300);

RateLimiterConfiguration rateLimiterConfiguration = new RateLimiterConfiguration();
DefaultEventConsumerRegistry<RateLimiterEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
Expand All @@ -42,6 +44,7 @@ public void testRateLimiterRegistry() {
RateLimiterRegistry rateLimiterRegistry = rateLimiterConfiguration.rateLimiterRegistry(rateLimiterConfigurationProperties, eventConsumerRegistry);

//Then
assertThat(rateLimiterConfigurationProperties.getRateLimiterAspectOrder()).isEqualTo(300);
assertThat(rateLimiterRegistry.getAllRateLimiters().size()).isEqualTo(2);
RateLimiter rateLimiter = rateLimiterRegistry.rateLimiter("backend1");
assertThat(rateLimiter).isNotNull();
Expand Down
Loading

0 comments on commit bf2d962

Please sign in to comment.