@@ -28,96 +28,71 @@ class CassandraContainer:
2828
2929 def __init__ (self , runtime : str = None ):
3030 self .runtime = runtime or get_container_runtime ()
31- self .container_name = f"test -cassandra-{ os . getpid () } "
31+ self .container_name = "async -cassandra-test "
3232 self .container_id : Optional [str ] = None
3333
3434 def start (self ):
3535 """Start the Cassandra container."""
36- # First check if port 9042 is already in use
37- port_check = subprocess .run (
38- [self .runtime , "ps" , "--format" , "{{.Names}} {{.Ports}}" ],
36+ # Stop and remove any existing container with our name
37+ print (f"Cleaning up any existing container named { self .container_name } ..." )
38+ subprocess .run (
39+ [self .runtime , "stop" , self .container_name ],
3940 capture_output = True ,
40- text = True ,
41+ stderr = subprocess .DEVNULL ,
42+ )
43+ subprocess .run (
44+ [self .runtime , "rm" , "-f" , self .container_name ],
45+ capture_output = True ,
46+ stderr = subprocess .DEVNULL ,
4147 )
4248
43- if port_check .stdout .strip ():
44- # Check each running container for port 9042
45- for line in port_check .stdout .strip ().split ("\n " ):
46- if "9042" in line :
47- # Extract container name
48- existing_container = line .split ()[0 ]
49- if existing_container != self .container_name :
50- print (f"Using existing Cassandra container: { existing_container } " )
51- self .container_id = existing_container
52- self .container_name = existing_container
53- # Verify it's ready
54- self ._wait_for_cassandra ()
55- return
56-
57- # Check if our container already exists
49+ # Create new container with proper resources
50+ print (f"Starting fresh Cassandra container: { self .container_name } " )
5851 result = subprocess .run (
5952 [
6053 self .runtime ,
61- "ps" ,
62- "-a" ,
63- "--filter" ,
64- f"name={ self .container_name } " ,
65- "--format" ,
66- "{{.ID}}" ,
54+ "run" ,
55+ "-d" ,
56+ "--name" ,
57+ self .container_name ,
58+ "-p" ,
59+ "9042:9042" ,
60+ "-e" ,
61+ "CASSANDRA_CLUSTER_NAME=TestCluster" ,
62+ "-e" ,
63+ "CASSANDRA_DC=datacenter1" ,
64+ "-e" ,
65+ "CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch" ,
66+ "-e" ,
67+ "HEAP_NEWSIZE=512M" ,
68+ "-e" ,
69+ "MAX_HEAP_SIZE=3G" ,
70+ "-e" ,
71+ "JVM_OPTS=-XX:+UseG1GC -XX:G1RSetUpdatingPauseTimePercent=5 -XX:MaxGCPauseMillis=300" ,
72+ "--memory=4g" ,
73+ "--memory-swap=4g" ,
74+ "cassandra:5" ,
6775 ],
6876 capture_output = True ,
6977 text = True ,
78+ check = True ,
7079 )
71-
72- if result .stdout .strip ():
73- # Container exists, start it
74- self .container_id = result .stdout .strip ()
75- subprocess .run ([self .runtime , "start" , self .container_id ], check = True )
76- else :
77- # Create new container with proper resources
78- result = subprocess .run (
79- [
80- self .runtime ,
81- "run" ,
82- "-d" ,
83- "--name" ,
84- self .container_name ,
85- "-p" ,
86- "9042:9042" ,
87- "-e" ,
88- "CASSANDRA_CLUSTER_NAME=TestCluster" ,
89- "-e" ,
90- "CASSANDRA_DC=datacenter1" ,
91- "-e" ,
92- "CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch" ,
93- "-e" ,
94- "HEAP_NEWSIZE=3G" ,
95- "-e" ,
96- "MAX_HEAP_SIZE=12G" ,
97- "-e" ,
98- "JVM_OPTS=-XX:+UseG1GC -XX:G1RSetUpdatingPauseTimePercent=5 -XX:MaxGCPauseMillis=300" ,
99- "--memory=16g" ,
100- "--memory-reservation=16g" ,
101- "cassandra:5.0" ,
102- ],
103- capture_output = True ,
104- text = True ,
105- check = True ,
106- )
107- self .container_id = result .stdout .strip ()
80+ self .container_id = result .stdout .strip ()
10881
10982 # Wait for Cassandra to be ready
11083 self ._wait_for_cassandra ()
11184
11285 def stop (self ):
11386 """Stop the Cassandra container."""
114- if self .container_id :
115- subprocess .run ([self .runtime , "stop" , self .container_id ], capture_output = True )
87+ if self .container_id or self .container_name :
88+ container_ref = self .container_id or self .container_name
89+ subprocess .run ([self .runtime , "stop" , container_ref ], capture_output = True )
11690
11791 def remove (self ):
11892 """Remove the Cassandra container."""
119- if self .container_id :
120- subprocess .run ([self .runtime , "rm" , "-f" , self .container_id ], capture_output = True )
93+ if self .container_id or self .container_name :
94+ container_ref = self .container_id or self .container_name
95+ subprocess .run ([self .runtime , "rm" , "-f" , container_ref ], capture_output = True )
12196
12297 def _wait_for_cassandra (self , timeout : int = 90 ):
12398 """Wait for Cassandra to be ready to accept connections."""
0 commit comments