Reuse Redis clusters for the duration of a test run

This commit is contained in:
Jon Chambers 2025-07-03 11:14:56 -04:00 committed by ravi-signal
parent da6ed94443
commit b81cd9ec61
2 changed files with 21 additions and 16 deletions

View File

@ -34,8 +34,8 @@ public class LocalFaultTolerantRedisClusterFactory implements FaultTolerantRedis
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
redisClusterExtension.afterEach(null);
redisClusterExtension.afterAll(null);
} catch (Exception e) {
redisClusterExtension.close();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}));

View File

@ -21,7 +21,6 @@ import java.net.ServerSocket;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
@ -32,8 +31,8 @@ import org.whispersystems.textsecuregcm.util.RedisClusterUtil;
import redis.embedded.RedisServer;
import redis.embedded.exceptions.EmbeddedRedisException;
public class RedisClusterExtension implements BeforeAllCallback, BeforeEachCallback, AfterAllCallback,
AfterEachCallback {
public class RedisClusterExtension implements BeforeAllCallback, BeforeEachCallback, AfterEachCallback,
ExtensionContext.Store.CloseableResource {
private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(2);
private static final int NODE_COUNT = 2;
@ -56,12 +55,16 @@ public class RedisClusterExtension implements BeforeAllCallback, BeforeEachCallb
}
@Override
public void afterAll(final ExtensionContext context) throws Exception {
redisClientResources.shutdown().get();
public void close() throws Throwable {
if (redisClientResources != null) {
redisClientResources.shutdown().get();
for (final RedisServer node : CLUSTER_NODES) {
node.stop();
for (final RedisServer node : CLUSTER_NODES) {
node.stop();
}
}
redisClientResources = null;
}
@Override
@ -73,15 +76,17 @@ public class RedisClusterExtension implements BeforeAllCallback, BeforeEachCallb
public void beforeAll(final ExtensionContext context) throws Exception {
assumeFalse(System.getProperty("os.name").equalsIgnoreCase("windows"));
redisClientResources = ClientResources.builder().build();
if (redisClientResources == null) {
redisClientResources = ClientResources.builder().build();
for (int i = 0; i < NODE_COUNT; i++) {
// We're occasionally seeing redis server startup failing due to the bind address being already in use.
// To mitigate that, we're going to just retry a couple of times before failing the test.
CLUSTER_NODES[i] = startWithRetries(3);
for (int i = 0; i < NODE_COUNT; i++) {
// We're occasionally seeing redis server startup failing due to the bind address being already in use.
// To mitigate that, we're going to just retry a couple of times before failing the test.
CLUSTER_NODES[i] = startWithRetries(3);
}
assembleCluster(CLUSTER_NODES);
}
assembleCluster(CLUSTER_NODES);
}
@Override