Skip to content

Commit c63ff9d

Browse files
committed
trying to fix flakey integration test on CI
1 parent f468390 commit c63ff9d

File tree

2 files changed

+89
-15
lines changed

2 files changed

+89
-15
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ markers = [
116116
# Special markers
117117
"flaky: Known flaky tests (quarantined)",
118118
"wip: Work in progress tests",
119+
"sync_driver: Tests that use synchronous cassandra driver (may be unstable in CI)",
119120

120121
# Legacy markers (kept for compatibility)
121122
"stress: marks tests as stress tests for high load scenarios",

tests/integration/test_driver_compatibility.py

Lines changed: 88 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,110 @@
44
This ensures our wrapper maintains compatibility and doesn't break any functionality.
55
"""
66

7+
import os
78
import uuid
9+
import warnings
810

911
import pytest
1012
from cassandra.cluster import Cluster as SyncCluster
13+
from cassandra.policies import DCAwareRoundRobinPolicy
1114
from cassandra.query import BatchStatement, BatchType, dict_factory
1215

1316

1417
@pytest.mark.integration
18+
@pytest.mark.sync_driver # Allow filtering these tests: pytest -m "not sync_driver"
1519
class TestDriverCompatibility:
1620
"""Test async wrapper compatibility with raw driver features."""
1721

1822
@pytest.fixture
1923
def sync_cluster(self):
20-
"""Create a synchronous cluster for comparison."""
21-
cluster = SyncCluster(["127.0.0.1"])
22-
yield cluster
23-
cluster.shutdown()
24+
"""Create a synchronous cluster for comparison with stability improvements."""
25+
is_ci = os.environ.get("CI") == "true" or os.environ.get("GITHUB_ACTIONS") == "true"
26+
27+
# Strategy 1: Increase connection timeout for CI environments
28+
connect_timeout = 30.0 if is_ci else 10.0
29+
30+
# Strategy 2: Explicit configuration to reduce startup delays
31+
cluster = SyncCluster(
32+
contact_points=["127.0.0.1"],
33+
port=9042,
34+
connect_timeout=connect_timeout,
35+
# Always use default connection class
36+
load_balancing_policy=DCAwareRoundRobinPolicy(local_dc="datacenter1"),
37+
protocol_version=5, # We support protocol version 5
38+
idle_heartbeat_interval=30, # Keep connections alive in CI
39+
schema_event_refresh_window=10, # Reduce schema refresh overhead
40+
)
41+
42+
# Strategy 3: Adjust settings for CI stability
43+
if is_ci:
44+
# Reduce executor threads to minimize resource usage
45+
cluster.executor_threads = 1
46+
# Increase control connection timeout
47+
cluster.control_connection_timeout = 30.0
48+
# Suppress known warnings
49+
warnings.filterwarnings("ignore", category=DeprecationWarning)
50+
51+
try:
52+
yield cluster
53+
finally:
54+
cluster.shutdown()
2455

2556
@pytest.fixture
2657
def sync_session(self, sync_cluster, unique_keyspace):
27-
"""Create a synchronous session."""
28-
session = sync_cluster.connect()
29-
session.execute(
30-
f"""
31-
CREATE KEYSPACE IF NOT EXISTS {unique_keyspace}
32-
WITH REPLICATION = {{'class': 'SimpleStrategy', 'replication_factor': 1}}
33-
"""
34-
)
35-
session.set_keyspace(unique_keyspace)
36-
yield session
37-
session.shutdown()
58+
"""Create a synchronous session with retry logic for CI stability."""
59+
is_ci = os.environ.get("CI") == "true" or os.environ.get("GITHUB_ACTIONS") == "true"
60+
61+
# Add retry logic for connection in CI
62+
max_retries = 3 if is_ci else 1
63+
retry_delay = 2.0
64+
65+
session = None
66+
last_error = None
67+
68+
for attempt in range(max_retries):
69+
try:
70+
session = sync_cluster.connect()
71+
# Verify connection is working
72+
session.execute("SELECT release_version FROM system.local")
73+
break
74+
except Exception as e:
75+
last_error = e
76+
if attempt < max_retries - 1:
77+
import time
78+
79+
if is_ci:
80+
print(f"Connection attempt {attempt + 1} failed: {e}, retrying...")
81+
time.sleep(retry_delay)
82+
continue
83+
raise e
84+
85+
if session is None:
86+
raise last_error or Exception("Failed to connect")
87+
88+
# Create keyspace with retry for schema agreement
89+
for attempt in range(max_retries):
90+
try:
91+
session.execute(
92+
f"""
93+
CREATE KEYSPACE IF NOT EXISTS {unique_keyspace}
94+
WITH REPLICATION = {{'class': 'SimpleStrategy', 'replication_factor': 1}}
95+
"""
96+
)
97+
session.set_keyspace(unique_keyspace)
98+
break
99+
except Exception as e:
100+
if attempt < max_retries - 1 and is_ci:
101+
import time
102+
103+
time.sleep(1)
104+
continue
105+
raise e
106+
107+
try:
108+
yield session
109+
finally:
110+
session.shutdown()
38111

39112
@pytest.mark.asyncio
40113
async def test_basic_query_compatibility(self, sync_session, session_with_keyspace):

0 commit comments

Comments
 (0)