Skip to content

Commit 37fc3c4

Browse files
committed
tests: replace medium-priority time.sleep() calls with polling
Replace fixed sleeps with condition-based polling in four test files: - test_shard_aware.py: replace 25s of sleeps (5+10+5+5) with wait_until_not_raised polling for reconnection after shard connection close and iptables blocking - test_metrics.py: replace 15s of sleeps (5+5+5) with polling for cluster recovery and node-down detection - test_tablets.py: replace 13s of sleeps (3+10) with polling for metadata refresh and decommission completion - simulacron/test_connection.py: replace 20s of sleeps (10+10) with polling for quiescent pool state Total potential saving: ~73s of unconditional waiting.
1 parent abaa1c7 commit 37fc3c4

4 files changed

Lines changed: 43 additions & 20 deletions

File tree

tests/integration/simulacron/test_connection.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from cassandra.policies import HostStateListener, RoundRobinPolicy, WhiteListRoundRobinPolicy
2424

2525
from tests import connection_class, thread_pool_executor_class
26-
from tests.util import late
26+
from tests.util import late, wait_until_not_raised
2727
from tests.integration import requiressimulacron, libevtest
2828
from tests.integration.util import assert_quiescent_pool_state
2929
# important to import the patch PROTOCOL_VERSION from the simulacron module
@@ -356,13 +356,15 @@ def test_retry_after_defunct(self):
356356
for _ in range(10):
357357
session.execute(query_to_prime)
358358

359-
# Might take some time to close the previous connections and reconnect
360-
time.sleep(10)
361-
assert_quiescent_pool_state(cluster)
359+
# Wait for previous connections to close and pool to stabilize
360+
wait_until_not_raised(
361+
lambda: assert_quiescent_pool_state(cluster),
362+
delay=1, max_attempts=30)
362363
clear_queries()
363364

364-
time.sleep(10)
365-
assert_quiescent_pool_state(cluster)
365+
wait_until_not_raised(
366+
lambda: assert_quiescent_pool_state(cluster),
367+
delay=1, max_attempts=30)
366368

367369
def test_idle_connection_is_not_closed(self):
368370
"""

tests/integration/standard/test_metrics.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from cassandra.cluster import NoHostAvailable, ExecutionProfile, EXEC_PROFILE_DEFAULT
2727
from tests.integration import get_cluster, get_node, use_singledc, execute_until_pass, TestCluster
28+
from tests.util import wait_until, wait_until_not_raised
2829
from cassandra import metrics
2930
from tests.integration import BasicSharedKeyspaceUnitTestCaseRF3WM, BasicExistingKeyspaceUnitTestCase, local
3031

@@ -75,8 +76,10 @@ def test_connection_error(self):
7576
self.session.execute(query)
7677
finally:
7778
get_cluster().start(wait_for_binary_proto=True, wait_other_notice=True)
78-
# Give some time for the cluster to come back up, for the next test
79-
time.sleep(5)
79+
# Wait for the cluster to come back up for the next test
80+
wait_until_not_raised(
81+
lambda: self.session.execute("SELECT key FROM system.local WHERE key='local'"),
82+
delay=0.5, max_attempts=30)
8083

8184
assert self.cluster.metrics.stats.connection_errors > 0
8285

@@ -156,7 +159,10 @@ def test_unavailable(self):
156159
# Sometimes this commands continues with the other nodes having not noticed
157160
# 1 is down, and a Timeout error is returned instead of an Unavailable
158161
get_node(1).stop(wait=True, wait_other_notice=True)
159-
time.sleep(5)
162+
wait_until(
163+
lambda: not self.cluster.metadata.get_host('127.0.0.1') or
164+
not self.cluster.metadata.get_host('127.0.0.1').is_up,
165+
delay=0.5, max_attempts=30)
160166
try:
161167
# Test write
162168
query = SimpleStatement("INSERT INTO test (k, v) VALUES (2, 2)", consistency_level=ConsistencyLevel.ALL)
@@ -171,8 +177,10 @@ def test_unavailable(self):
171177
assert self.cluster.metrics.stats.unavailables == 2
172178
finally:
173179
get_node(1).start(wait_other_notice=True, wait_for_binary_proto=True)
174-
# Give some time for the cluster to come back up, for the next test
175-
time.sleep(5)
180+
# Wait for the cluster to come back up for the next test
181+
wait_until_not_raised(
182+
lambda: self.session.execute("SELECT key FROM system.local WHERE key='local'"),
183+
delay=0.5, max_attempts=30)
176184

177185
self.cluster.shutdown()
178186

tests/integration/standard/test_shard_aware.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from cassandra import OperationTimedOut, ConsistencyLevel
2828

2929
from tests.integration import use_cluster, get_node, PROTOCOL_VERSION
30+
from tests.util import wait_until_not_raised
3031

3132
LOGGER = logging.getLogger(__name__)
3233

@@ -178,11 +179,13 @@ def test_closing_connections(self):
178179
continue
179180
shard_id = random.choice(list(pool._connections.keys()))
180181
pool._connections.get(shard_id).close()
181-
time.sleep(5)
182-
self.query_data(self.session, verify_in_tracing=False)
182+
wait_until_not_raised(
183+
lambda: self.query_data(self.session, verify_in_tracing=False),
184+
delay=0.5, max_attempts=30)
183185

184-
time.sleep(10)
185-
self.query_data(self.session)
186+
wait_until_not_raised(
187+
lambda: self.query_data(self.session),
188+
delay=0.5, max_attempts=60)
186189

187190
@pytest.mark.skip
188191
def test_blocking_connections(self):
@@ -212,13 +215,14 @@ def remove_iptables():
212215
'--destination {node1_ip_address}/32 -j REJECT --reject-with icmp-port-unreachable'
213216
).format(node1_ip_address=node1_ip_address, node1_port=node1_port).split(' ')
214217
)
215-
time.sleep(5)
218+
time.sleep(2) # allow iptables rule to take effect
216219
try:
217220
self.query_data(self.session, verify_in_tracing=False)
218221
except OperationTimedOut:
219222
pass
220223
remove_iptables()
221-
time.sleep(5)
222-
self.query_data(self.session, verify_in_tracing=False)
224+
wait_until_not_raised(
225+
lambda: self.query_data(self.session, verify_in_tracing=False),
226+
delay=0.5, max_attempts=30)
223227

224228
self.query_data(self.session)

tests/integration/standard/test_tablets.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from cassandra.policies import ConstantReconnectionPolicy, RoundRobinPolicy, TokenAwarePolicy
77

88
from tests.integration import PROTOCOL_VERSION, use_cluster, get_cluster
9+
from tests.util import wait_until
910
from tests.unit.test_host_connection_pool import LOGGER
1011

1112

@@ -212,7 +213,10 @@ def test_tablets_invalidation_drop_ks(self):
212213
def drop_ks(_):
213214
# Drop and recreate ks and table to trigger tablets invalidation
214215
self.create_ks_and_cf(self.cluster.connect())
215-
time.sleep(3)
216+
# Wait for tablet metadata to be refreshed
217+
wait_until(
218+
lambda: 'test1' in self.cluster.metadata.keyspaces,
219+
delay=0.5, max_attempts=20)
216220

217221
self.run_tablets_invalidation_test(drop_ks)
218222

@@ -233,7 +237,12 @@ def decommission_non_cc_node(rec):
233237
break
234238
else:
235239
assert False, "failed to find node to decommission"
236-
time.sleep(10)
240+
# Wait for decommission to complete and metadata to update
241+
wait_until(
242+
lambda: len([h for h in self.cluster.metadata.all_hosts() if h.is_up]) < 3,
243+
delay=1, max_attempts=60)
244+
# Allow additional time for tablet metadata invalidation to propagate
245+
time.sleep(2)
237246

238247
self.run_tablets_invalidation_test(decommission_non_cc_node)
239248

0 commit comments

Comments
 (0)