Skip to content

Commit d1ae102

Browse files
committed
Fix test_idle_heartbeat: wait for all shard connections before testing
wait_for_all_pools only waits for the first connection per host. Shard- aware connections to remaining shards are opened asynchronously in the background. When these background connections complete during the test's sleep interval, they replace existing connections causing KeyError (old connection ID not in snapshot) or stale idle state assertions. Add a helper that polls until all shard connections are established, then call it after connect(). This stabilizes the connection set before snapshotting request IDs, eliminating both failure modes and allowing the original stronger assertions to be restored.
1 parent 929fd31 commit d1ae102

1 file changed

Lines changed: 23 additions & 4 deletions

File tree

tests/integration/standard/test_cluster.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -725,12 +725,34 @@ def _warning_are_issued_when_auth(self, auth_provider):
725725
assert auth_warning >= 4
726726
assert auth_warning == mock_handler.get_message_count("debug", "Got ReadyMessage on new connection")
727727

728+
def _wait_for_all_shard_connections(self, cluster, timeout=30):
729+
"""Wait until all shard-aware connections are fully established."""
730+
from cassandra.pool import HostConnection
731+
deadline = time.time() + timeout
732+
while time.time() < deadline:
733+
all_connected = True
734+
for holder in cluster.get_connection_holders():
735+
if not isinstance(holder, HostConnection):
736+
continue
737+
if holder.host.sharding_info and len(holder._connections) < holder.host.sharding_info.shards_count:
738+
all_connected = False
739+
break
740+
if all_connected:
741+
return
742+
time.sleep(0.1)
743+
raise RuntimeError("Timed out waiting for all shard connections to be established")
744+
728745
def test_idle_heartbeat(self):
729746
interval = 2
730747
cluster = TestCluster(idle_heartbeat_interval=interval,
731748
monitor_reporting_enabled=False)
732749
session = cluster.connect(wait_for_all_pools=True)
733750

751+
# wait_for_all_pools only waits for the first connection per host;
752+
# shard-aware connections to remaining shards are opened in background.
753+
# Wait for them to stabilize so they don't get replaced during the test.
754+
self._wait_for_all_shard_connections(cluster)
755+
734756
# This test relies on impl details of connection req id management to see if heartbeats
735757
# are being sent. May need update if impl is changed
736758
connection_request_ids = {}
@@ -746,11 +768,8 @@ def test_idle_heartbeat(self):
746768

747769
connections = [c for holders in cluster.get_connection_holders() for c in holders.get_connections()]
748770

749-
# make sure requests were sent on all connections that existed before the sleep
750-
# (shard-aware reconnection may replace connections during the sleep interval)
771+
# make sure requests were sent on all connections
751772
for c in connections:
752-
if id(c) not in connection_request_ids:
753-
continue
754773
expected_ids = connection_request_ids[id(c)]
755774
expected_ids.rotate(-1)
756775
with c.lock:

0 commit comments

Comments
 (0)