1
1
package redis .clients .jedis .providers ;
2
2
3
3
import io .github .resilience4j .circuitbreaker .CircuitBreaker ;
4
+
5
+ import org .awaitility .Awaitility ;
6
+ import org .awaitility .Durations ;
7
+ import org .junit .jupiter .api .AfterEach ;
4
8
import org .junit .jupiter .api .BeforeEach ;
5
9
import org .junit .jupiter .api .Test ;
6
10
import redis .clients .jedis .*;
7
11
import redis .clients .jedis .MultiClusterClientConfig .ClusterConfig ;
8
12
import redis .clients .jedis .exceptions .JedisConnectionException ;
9
13
import redis .clients .jedis .exceptions .JedisValidationException ;
10
14
import redis .clients .jedis .mcf .Endpoint ;
15
+ import redis .clients .jedis .providers .MultiClusterPooledConnectionProvider .Cluster ;
11
16
17
+ import java .util .Arrays ;
12
18
import java .util .concurrent .atomic .AtomicBoolean ;
13
19
14
20
import static org .junit .jupiter .api .Assertions .*;
@@ -38,6 +44,12 @@ public void setUp() {
38
44
new MultiClusterClientConfig .Builder (clusterConfigs ).build ());
39
45
}
40
46
47
+ @ AfterEach
48
+ public void destroy () {
49
+ provider .close ();
50
+ provider = null ;
51
+ }
52
+
41
53
@ Test
42
54
public void testCircuitBreakerForcedTransitions () {
43
55
@@ -55,34 +67,47 @@ public void testCircuitBreakerForcedTransitions() {
55
67
}
56
68
57
69
@ Test
58
- public void testIterateActiveCluster () {
70
+ public void testIterateActiveCluster () throws InterruptedException {
71
+ waitForClustersToGetHealthy (provider .getCluster (endpointStandalone0 .getHostAndPort ()),
72
+ provider .getCluster (endpointStandalone1 .getHostAndPort ()));
73
+
59
74
Endpoint e2 = provider .iterateActiveCluster ();
60
75
assertEquals (endpointStandalone1 .getHostAndPort (), e2 );
61
76
}
62
77
63
78
@ Test
64
79
public void testIterateActiveClusterOutOfRange () {
80
+ waitForClustersToGetHealthy (provider .getCluster (endpointStandalone0 .getHostAndPort ()),
81
+ provider .getCluster (endpointStandalone1 .getHostAndPort ()));
82
+
65
83
provider .setActiveCluster (endpointStandalone0 .getHostAndPort ());
66
84
provider .getCluster ().setDisabled (true );
67
85
68
86
Endpoint e2 = provider .iterateActiveCluster ();
69
87
provider .getCluster ().setDisabled (true );
70
88
71
89
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 ());
75
92
}
76
93
77
94
@ Test
78
95
public void testCanIterateOnceMore () {
96
+ waitForClustersToGetHealthy (provider .getCluster (endpointStandalone0 .getHostAndPort ()),
97
+ provider .getCluster (endpointStandalone1 .getHostAndPort ()));
98
+
79
99
provider .setActiveCluster (endpointStandalone0 .getHostAndPort ());
80
100
provider .getCluster ().setDisabled (true );
81
101
provider .iterateActiveCluster ();
82
102
83
103
assertFalse (provider .canIterateOnceMore ());
84
104
}
85
105
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
+
86
111
@ Test
87
112
public void testRunClusterFailoverPostProcessor () {
88
113
ClusterConfig [] clusterConfigs = new ClusterConfig [2 ];
0 commit comments