|
4 | 4 | This ensures our wrapper maintains compatibility and doesn't break any functionality. |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import os |
7 | 8 | import uuid |
| 9 | +import warnings |
8 | 10 |
|
9 | 11 | import pytest |
10 | 12 | from cassandra.cluster import Cluster as SyncCluster |
| 13 | +from cassandra.policies import DCAwareRoundRobinPolicy |
11 | 14 | from cassandra.query import BatchStatement, BatchType, dict_factory |
12 | 15 |
|
13 | 16 |
|
14 | 17 | @pytest.mark.integration |
| 18 | +@pytest.mark.sync_driver # Allow filtering these tests: pytest -m "not sync_driver" |
15 | 19 | class TestDriverCompatibility: |
16 | 20 | """Test async wrapper compatibility with raw driver features.""" |
17 | 21 |
|
18 | 22 | @pytest.fixture |
19 | 23 | 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() |
24 | 55 |
|
25 | 56 | @pytest.fixture |
26 | 57 | 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() |
38 | 111 |
|
39 | 112 | @pytest.mark.asyncio |
40 | 113 | async def test_basic_query_compatibility(self, sync_session, session_with_keyspace): |
|
0 commit comments