diff --git a/pom.xml b/pom.xml index 74e0848be..3b7805062 100644 --- a/pom.xml +++ b/pom.xml @@ -50,6 +50,7 @@ 1.8 2.0.1.BUILD-SNAPSHOT 2.0.1.BUILD-SNAPSHOT + 0.6 @@ -111,6 +112,11 @@ spring-boot-devtools ${spring-boot.version} + + com.github.kstyrc + embedded-redis + ${embedded-redis.version} + diff --git a/spring-cloud-gateway-core/pom.xml b/spring-cloud-gateway-core/pom.xml index 9ee9fa14a..a1caba5bf 100644 --- a/spring-cloud-gateway-core/pom.xml +++ b/spring-cloud-gateway-core/pom.xml @@ -116,6 +116,11 @@ jackson-module-kotlin test + + com.github.kstyrc + embedded-redis + test + diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterTests.java index 92e4114be..771fd1353 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ratelimit/RedisRateLimiterTests.java @@ -2,6 +2,7 @@ package org.springframework.cloud.gateway.filter.ratelimit; import java.util.UUID; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -10,6 +11,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.cloud.gateway.filter.ratelimit.RateLimiter.Response; import org.springframework.cloud.gateway.test.BaseWebClientTests; +import org.springframework.cloud.gateway.test.support.redis.RedisRule; import org.springframework.context.annotation.Import; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; @@ -29,6 +31,9 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen @DirtiesContext public class RedisRateLimiterTests extends BaseWebClientTests { + @Rule + public final RedisRule redis = RedisRule.bindToPort(6379); + @Autowired private RedisRateLimiter rateLimiter; diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/support/redis/RedisRule.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/support/redis/RedisRule.java new file mode 100644 index 000000000..3d0fcbb9c --- /dev/null +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/support/redis/RedisRule.java @@ -0,0 +1,62 @@ +package org.springframework.cloud.gateway.test.support.redis; + +import static java.lang.String.format; +import static java.util.stream.IntStream.range; + +import java.io.IOException; +import java.net.ServerSocket; +import org.junit.rules.ExternalResource; +import redis.embedded.RedisServer; + +public class RedisRule extends ExternalResource { + + public static RedisRule bindToPort(final int port) { + return new RedisRule(port); + } + + public static RedisRule bindToFirstOpenPort(final int startInclusive, final int endExclusive) { + return new RedisRule(findOpenPort(startInclusive, endExclusive)); + } + + private static int findOpenPort(final int startInclusive, final int endExclusive) { + return range(startInclusive, endExclusive) + .filter(RedisRule::testPort) + .findFirst() + .orElseThrow(() ->new IllegalStateException(format( + "No open port found in the range [%d, %d]", startInclusive, endExclusive))); + } + + private static boolean testPort(int port) { + try { + new ServerSocket(port).close(); + return true; + } catch (final IOException ex) { + return false; + } + } + + private final int port; + + private RedisServer redisServer; + + private RedisRule(final int port) { + this.port = port; + } + + @Override + protected void before() { + try { + redisServer = RedisServer.builder().port(port).setting("maxmemory 16MB").build(); + redisServer.start(); + } catch (final Exception e) { + throw new RuntimeException(format("Error while initializing the Redis server" + + " on port %d", port), e); + } + } + + @Override + protected void after() { + redisServer.stop(); + } + +} \ No newline at end of file