Skip to content

Commit a8a6293

Browse files
committed
Replace Thread.sleep with Awaitility in integration tests
Replace fixed Thread.sleep calls with Awaitility's polling-based waiting in MetricsITBase, DefaultMetadataTabletMapIT, and MockResolverIT. This eliminates wasted wait time by polling for the expected condition instead of sleeping for a fixed duration. DriverBlockHoundIntegrationIT is left unchanged as its sleep is intentional for BlockHound testing.
1 parent 12e6acb commit a8a6293

4 files changed

Lines changed: 35 additions & 92 deletions

File tree

integration-tests/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@
223223
<artifactId>tools</artifactId>
224224
<scope>test</scope>
225225
</dependency>
226+
<dependency>
227+
<groupId>org.awaitility</groupId>
228+
<artifactId>awaitility</artifactId>
229+
<scope>test</scope>
230+
</dependency>
226231
</dependencies>
227232
<build>
228233
<plugins>

integration-tests/src/test/java/com/datastax/oss/driver/core/metadata/DefaultMetadataTabletMapIT.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.datastax.oss.driver.core.metadata;
22

3+
import static org.awaitility.Awaitility.await;
4+
35
import com.datastax.oss.driver.api.core.CqlIdentifier;
46
import com.datastax.oss.driver.api.core.CqlSession;
57
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
@@ -28,6 +30,7 @@
2830
import java.util.concurrent.ConcurrentSkipListSet;
2931
import java.util.function.Function;
3032
import java.util.function.Supplier;
33+
import org.awaitility.core.ConditionTimeoutException;
3134
import org.junit.Assert;
3235
import org.junit.BeforeClass;
3336
import org.junit.ClassRule;
@@ -340,16 +343,15 @@ public void should_receive_each_tablet_exactly_once() {
340343
}
341344

342345
private static boolean waitSessionLearnedTabletInfo(CqlSession session) {
343-
if (isSessionLearnedTabletInfo(session)) {
344-
return true;
345-
}
346-
// Wait till tablet update, which is async, is completed
347346
try {
348-
Thread.sleep(200);
349-
} catch (InterruptedException e) {
350-
Thread.currentThread().interrupt();
347+
await()
348+
.atMost(Duration.ofSeconds(5))
349+
.pollInterval(Duration.ofMillis(50))
350+
.until(() -> isSessionLearnedTabletInfo(session));
351+
return true;
352+
} catch (ConditionTimeoutException e) {
353+
return false;
351354
}
352-
return isSessionLearnedTabletInfo(session);
353355
}
354356

355357
private static boolean checkIfRoutedProperly(CqlSession session, Statement stmt) {

integration-tests/src/test/java/com/datastax/oss/driver/core/metrics/MetricsITBase.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,12 @@ public void should_evict_down_node_metrics_when_timeout_fires() throws Exception
174174
// trigger node1 UP -> DOWN
175175
eventBus.fire(NodeStateEvent.changed(NodeState.UP, NodeState.DOWN, node1));
176176

177-
Thread.sleep(expireAfter.toMillis());
178-
179177
// then node-level metrics should be evicted from node1, but
180178
// node2 and node3 metrics should not have been evicted
181-
await().untilAsserted(() -> assertNodeMetricsEvicted(session, node1));
179+
await()
180+
.atMost(expireAfter.plusSeconds(5))
181+
.pollInterval(Duration.ofMillis(100))
182+
.untilAsserted(() -> assertNodeMetricsEvicted(session, node1));
182183
assertNodeMetricsNotEvicted(session, node2);
183184
assertNodeMetricsNotEvicted(session, node3);
184185

@@ -219,19 +220,25 @@ public void should_not_evict_down_node_metrics_when_node_is_back_up_before_timeo
219220
eventBus.fire(NodeStateEvent.changed(NodeState.UP, NodeState.FORCED_DOWN, node2));
220221
eventBus.fire(NodeStateEvent.removed(node3));
221222

222-
Thread.sleep(500);
223+
// Wait for half the expiry window before bringing nodes back up
224+
await().pollDelay(Duration.ofMillis(500)).atMost(Duration.ofSeconds(5)).until(() -> true);
223225

224226
// trigger nodes DOWN -> UP, should cancel the timeouts
225227
eventBus.fire(NodeStateEvent.changed(NodeState.DOWN, NodeState.UP, node1));
226228
eventBus.fire(NodeStateEvent.changed(NodeState.FORCED_DOWN, NodeState.UP, node2));
227229
eventBus.fire(NodeStateEvent.added(node3));
228230

229-
Thread.sleep(expireAfter.toMillis());
230-
231-
// then no node-level metrics should be evicted
232-
assertNodeMetricsNotEvicted(session, node1);
233-
assertNodeMetricsNotEvicted(session, node2);
234-
assertNodeMetricsNotEvicted(session, node3);
231+
// Wait for the full expiry duration and verify metrics are never evicted
232+
await()
233+
.during(expireAfter)
234+
.atMost(expireAfter.plusSeconds(5))
235+
.pollInterval(Duration.ofMillis(200))
236+
.untilAsserted(
237+
() -> {
238+
assertNodeMetricsNotEvicted(session, node1);
239+
assertNodeMetricsNotEvicted(session, node2);
240+
assertNodeMetricsNotEvicted(session, node3);
241+
});
235242

236243
} finally {
237244
AbstractMetricUpdater.MIN_EXPIRE_AFTER = Duration.ofMinutes(5);

integration-tests/src/test/java/com/datastax/oss/driver/core/resolver/MockResolverIT.java

Lines changed: 3 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,11 @@
3434
import com.datastax.oss.driver.api.core.config.TypedDriverOption;
3535
import com.datastax.oss.driver.api.core.cql.ResultSet;
3636
import com.datastax.oss.driver.api.core.cql.Row;
37-
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
38-
import com.datastax.oss.driver.api.core.cql.SimpleStatementBuilder;
3937
import com.datastax.oss.driver.api.core.metadata.Node;
4038
import com.datastax.oss.driver.api.testinfra.ccm.CcmBridge;
4139
import com.datastax.oss.driver.categories.IsolatedTests;
4240
import com.datastax.oss.driver.internal.core.config.typesafe.DefaultProgrammaticDriverConfigLoaderBuilder;
4341
import java.net.InetSocketAddress;
44-
import java.time.Duration;
4542
import java.util.Collection;
4643
import java.util.Collections;
4744
import java.util.Iterator;
@@ -231,30 +228,7 @@ public void cannot_reconnect_with_resolved_socket() {
231228
ccmBridge.create();
232229
ccmBridge.start();
233230
session = builder.build();
234-
long endTime = System.currentTimeMillis() + CLUSTER_WAIT_SECONDS * 1000;
235-
while (System.currentTimeMillis() < endTime) {
236-
try {
237-
nodes = session.getMetadata().getNodes().values();
238-
int upNodes = 0;
239-
for (Node node : nodes) {
240-
if (node.getUpSinceMillis() > 0) {
241-
upNodes++;
242-
}
243-
}
244-
if (upNodes == 3) {
245-
break;
246-
}
247-
// session.refreshSchema();
248-
SimpleStatement statement =
249-
new SimpleStatementBuilder("select * from system.local where key='local'")
250-
.setTimeout(Duration.ofSeconds(3))
251-
.build();
252-
session.executeAsync(statement);
253-
Thread.sleep(3000);
254-
} catch (InterruptedException e) {
255-
break;
256-
}
257-
}
231+
waitForAllNodesUp(session, 3);
258232
ResultSet rs = session.execute("select * from system.local where key='local'");
259233
assertThat(rs).isNotNull();
260234
Row row = rs.one();
@@ -291,29 +265,7 @@ public void cannot_reconnect_with_resolved_socket() {
291265
"test.cluster.fake", ccmBridge.getNodeIpAddress(3));
292266
ccmBridge.create();
293267
ccmBridge.start();
294-
long endTime = System.currentTimeMillis() + CLUSTER_WAIT_SECONDS * 1000;
295-
while (System.currentTimeMillis() < endTime) {
296-
try {
297-
nodes = session.getMetadata().getNodes().values();
298-
int upNodes = 0;
299-
for (Node node : nodes) {
300-
if (node.getUpSinceMillis() > 0) {
301-
upNodes++;
302-
}
303-
}
304-
if (upNodes == 3) {
305-
break;
306-
}
307-
SimpleStatement statement =
308-
new SimpleStatementBuilder("select * from system.local where key='local'")
309-
.setTimeout(Duration.ofSeconds(3))
310-
.build();
311-
session.executeAsync(statement);
312-
Thread.sleep(3000);
313-
} catch (InterruptedException e) {
314-
break;
315-
}
316-
}
268+
waitForAllNodesUp(session, 3);
317269
nodes = session.getMetadata().getNodes().values();
318270
assertThat(nodes).hasSize(3);
319271
Iterator<Node> iterator = nodes.iterator();
@@ -346,30 +298,7 @@ public void cannot_reconnect_with_resolved_socket() {
346298
// Now the driver should fail to reconnect since unresolved hostname is gone.
347299
ccmBridge.create();
348300
ccmBridge.start();
349-
long endTime = System.currentTimeMillis() + CLUSTER_WAIT_SECONDS * 1000;
350-
while (System.currentTimeMillis() < endTime) {
351-
try {
352-
nodes = session.getMetadata().getNodes().values();
353-
int upNodes = 0;
354-
for (Node node : nodes) {
355-
if (node.getUpSinceMillis() > 0) {
356-
upNodes++;
357-
}
358-
}
359-
if (upNodes == 3) {
360-
break;
361-
}
362-
// session.refreshSchema();
363-
SimpleStatement statement =
364-
new SimpleStatementBuilder("select * from system.local where key='local'")
365-
.setTimeout(Duration.ofSeconds(3))
366-
.build();
367-
session.executeAsync(statement);
368-
Thread.sleep(3000);
369-
} catch (InterruptedException e) {
370-
break;
371-
}
372-
}
301+
waitForAllNodesUp(session, 3);
373302
session.execute("select * from system.local where key='local'");
374303
}
375304
session.close();

0 commit comments

Comments
 (0)