Skip to content

Commit 405101e

Browse files
committed
- fix failing tests by waiting on clusters to get healthy
1 parent 975ab78 commit 405101e

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/test/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProviderTest.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package redis.clients.jedis.providers;
22

33
import io.github.resilience4j.circuitbreaker.CircuitBreaker;
4+
5+
import org.awaitility.Awaitility;
6+
import org.awaitility.Durations;
7+
import org.junit.jupiter.api.AfterEach;
48
import org.junit.jupiter.api.BeforeEach;
59
import org.junit.jupiter.api.Test;
610
import redis.clients.jedis.*;
711
import redis.clients.jedis.MultiClusterClientConfig.ClusterConfig;
812
import redis.clients.jedis.exceptions.JedisConnectionException;
913
import redis.clients.jedis.exceptions.JedisValidationException;
1014
import redis.clients.jedis.mcf.Endpoint;
15+
import redis.clients.jedis.providers.MultiClusterPooledConnectionProvider.Cluster;
1116

17+
import java.util.Arrays;
1218
import java.util.concurrent.atomic.AtomicBoolean;
1319

1420
import static org.junit.jupiter.api.Assertions.*;
@@ -38,6 +44,12 @@ public void setUp() {
3844
new MultiClusterClientConfig.Builder(clusterConfigs).build());
3945
}
4046

47+
@AfterEach
48+
public void destroy() {
49+
provider.close();
50+
provider = null;
51+
}
52+
4153
@Test
4254
public void testCircuitBreakerForcedTransitions() {
4355

@@ -55,34 +67,47 @@ public void testCircuitBreakerForcedTransitions() {
5567
}
5668

5769
@Test
58-
public void testIterateActiveCluster() {
70+
public void testIterateActiveCluster() throws InterruptedException {
71+
waitForClustersToGetHealthy(provider.getCluster(endpointStandalone0.getHostAndPort()),
72+
provider.getCluster(endpointStandalone1.getHostAndPort()));
73+
5974
Endpoint e2 = provider.iterateActiveCluster();
6075
assertEquals(endpointStandalone1.getHostAndPort(), e2);
6176
}
6277

6378
@Test
6479
public void testIterateActiveClusterOutOfRange() {
80+
waitForClustersToGetHealthy(provider.getCluster(endpointStandalone0.getHostAndPort()),
81+
provider.getCluster(endpointStandalone1.getHostAndPort()));
82+
6583
provider.setActiveCluster(endpointStandalone0.getHostAndPort());
6684
provider.getCluster().setDisabled(true);
6785

6886
Endpoint e2 = provider.iterateActiveCluster();
6987
provider.getCluster().setDisabled(true);
7088

7189
assertEquals(endpointStandalone1.getHostAndPort(), e2);
72-
73-
assertThrows(JedisConnectionException.class, () -> provider.iterateActiveCluster()); // Should throw an
74-
// exception
90+
// Should throw an exception
91+
assertThrows(JedisConnectionException.class, () -> provider.iterateActiveCluster());
7592
}
7693

7794
@Test
7895
public void testCanIterateOnceMore() {
96+
waitForClustersToGetHealthy(provider.getCluster(endpointStandalone0.getHostAndPort()),
97+
provider.getCluster(endpointStandalone1.getHostAndPort()));
98+
7999
provider.setActiveCluster(endpointStandalone0.getHostAndPort());
80100
provider.getCluster().setDisabled(true);
81101
provider.iterateActiveCluster();
82102

83103
assertFalse(provider.canIterateOnceMore());
84104
}
85105

106+
private void waitForClustersToGetHealthy(Cluster... clusters) {
107+
Awaitility.await().pollInterval(Durations.ONE_HUNDRED_MILLISECONDS).atMost(Durations.TWO_SECONDS)
108+
.until(() -> Arrays.stream(clusters).allMatch(Cluster::isHealthy));
109+
}
110+
86111
@Test
87112
public void testRunClusterFailoverPostProcessor() {
88113
ClusterConfig[] clusterConfigs = new ClusterConfig[2];

0 commit comments

Comments
 (0)