Don't fail redis rule if redis is already running.
This commit is contained in:
@@ -1,20 +1,31 @@
|
||||
package org.springframework.cloud.gateway.test.support.redis;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.rules.ExternalResource;
|
||||
import redis.embedded.RedisServer;
|
||||
|
||||
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) {
|
||||
private Log log = LogFactory.getLog(getClass());
|
||||
|
||||
public static final int DEFAULT_REDIS_PORT = 6379;
|
||||
|
||||
public static RedisRule bindToDefaultPort() {
|
||||
return new RedisRule(DEFAULT_REDIS_PORT, true);
|
||||
}
|
||||
|
||||
public static RedisRule bindToDefaultPort(int port) {
|
||||
return new RedisRule(port);
|
||||
}
|
||||
|
||||
public static RedisRule bindToFirstOpenPort(final int startInclusive, final int endExclusive) {
|
||||
public static RedisRule bindToFirstOpenPort(int startInclusive, int endExclusive) {
|
||||
return new RedisRule(findOpenPort(startInclusive, endExclusive));
|
||||
}
|
||||
|
||||
@@ -36,11 +47,17 @@ public class RedisRule extends ExternalResource {
|
||||
}
|
||||
|
||||
private final int port;
|
||||
private final boolean ignoreDefaultPortFailure;
|
||||
|
||||
private RedisServer redisServer;
|
||||
|
||||
private RedisRule(final int port) {
|
||||
private RedisRule(int port) {
|
||||
this(port, false);
|
||||
}
|
||||
|
||||
private RedisRule(int port, boolean ignoreDefaultPortFailure) {
|
||||
this.port = port;
|
||||
this.ignoreDefaultPortFailure = ignoreDefaultPortFailure;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -49,8 +66,12 @@ public class RedisRule extends ExternalResource {
|
||||
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);
|
||||
if (port == DEFAULT_REDIS_PORT && ignoreDefaultPortFailure) {
|
||||
log.info("Unable to start embedded Redis on default port. Ignoring error. Assuming redis is already running.");
|
||||
} else {
|
||||
throw new RuntimeException(format("Error while initializing the Redis server"
|
||||
+ " on port %d", port), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user