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(() -> { Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try { try {
redisClusterExtension.afterEach(null); redisClusterExtension.afterEach(null);
redisClusterExtension.afterAll(null); redisClusterExtension.close();
} catch (Exception e) { } catch (Throwable e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
})); }));

View File

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