4747import java .util .Iterator ;
4848import java .util .List ;
4949import java .util .Set ;
50+ import java .util .concurrent .TimeUnit ;
5051import java .util .stream .Collectors ;
52+ import org .awaitility .Awaitility ;
5153import org .junit .Test ;
5254import org .junit .experimental .categories .Category ;
5355import org .slf4j .Logger ;
@@ -59,7 +61,19 @@ public class MockResolverIT {
5961 private static final Logger LOG = LoggerFactory .getLogger (MockResolverIT .class );
6062
6163 private static final int CLUSTER_WAIT_SECONDS =
62- 60 ; // Maximal wait time for cluster nodes to get up
64+ 20 ; // Maximal wait time for cluster nodes to get up
65+
66+ private static void waitForAllNodesUp (CqlSession session , int expectedNodes ) {
67+ Awaitility .await ()
68+ .atMost (CLUSTER_WAIT_SECONDS , TimeUnit .SECONDS )
69+ .pollInterval (500 , TimeUnit .MILLISECONDS )
70+ .until (
71+ () -> {
72+ Collection <Node > nodes = session .getMetadata ().getNodes ().values ();
73+ long upCount = nodes .stream ().filter (n -> n .getUpSinceMillis () > 0 ).count ();
74+ return upCount == expectedNodes ;
75+ });
76+ }
6377
6478 @ Test
6579 public void should_connect_with_mocked_hostname () {
@@ -130,33 +144,7 @@ public void replace_cluster_test() {
130144 ccmBridge .create ();
131145 ccmBridge .start ();
132146 session = builder .build ();
133- boolean allNodesUp = false ;
134- int nodesUp = 0 ;
135- for (int i = 0 ; i < CLUSTER_WAIT_SECONDS ; i ++) {
136- try {
137- Collection <Node > nodes = session .getMetadata ().getNodes ().values ();
138- nodesUp = 0 ;
139- for (Node node : nodes ) {
140- if (node .getUpSinceMillis () > 0 ) {
141- nodesUp ++;
142- }
143- }
144- if (nodesUp == numberOfNodes ) {
145- allNodesUp = true ;
146- break ;
147- }
148- Thread .sleep (1000 );
149- } catch (InterruptedException e ) {
150- break ;
151- }
152- }
153- if (!allNodesUp ) {
154- LOG .error (
155- "Driver sees only {} nodes UP instead of {} after waiting {}s" ,
156- nodesUp ,
157- numberOfNodes ,
158- CLUSTER_WAIT_SECONDS );
159- }
147+ waitForAllNodesUp (session , numberOfNodes );
160148 ResultSet rs = session .execute ("select * from system.local where key='local'" );
161149 assertThat (rs ).isNotNull ();
162150 Row row = rs .one ();
@@ -178,33 +166,7 @@ public void replace_cluster_test() {
178166 CcmBridge .builder ().withNodes (numberOfNodes ).withIpPrefix ("127.0.1." ).build ()) {
179167 ccmBridge .create ();
180168 ccmBridge .start ();
181- boolean allNodesUp = false ;
182- int nodesUp = 0 ;
183- for (int i = 0 ; i < CLUSTER_WAIT_SECONDS ; i ++) {
184- try {
185- Collection <Node > nodes = session .getMetadata ().getNodes ().values ();
186- nodesUp = 0 ;
187- for (Node node : nodes ) {
188- if (node .getUpSinceMillis () > 0 ) {
189- nodesUp ++;
190- }
191- }
192- if (nodesUp == numberOfNodes ) {
193- allNodesUp = true ;
194- break ;
195- }
196- Thread .sleep (1000 );
197- } catch (InterruptedException e ) {
198- break ;
199- }
200- }
201- if (!allNodesUp ) {
202- LOG .error (
203- "Driver sees only {} nodes UP instead of {} after waiting {}s" ,
204- nodesUp ,
205- numberOfNodes ,
206- CLUSTER_WAIT_SECONDS );
207- }
169+ waitForAllNodesUp (session , numberOfNodes );
208170 ResultSet rs = session .execute ("select * from system.local where key='local'" );
209171 assertThat (rs ).isNotNull ();
210172 Row row = rs .one ();
0 commit comments