@@ -28,7 +28,7 @@ def setup_class(cls):
2828 cls ._rlist = None
2929
3030 #
31- # ------------------- test_insert_cancel.py ----------------
31+ # ------------------- common helpers ----------------
3232 #
3333 def get_pids_by_name (self , process_name ):
3434 pids = []
@@ -37,94 +37,117 @@ def get_pids_by_name(self, process_name):
3737 pids .append (proc .pid )
3838 return pids
3939
40+ def wait_for_benchmark (self , timeout = 30 , interval = 0.5 ):
41+ """Poll until taosBenchmark process appears, return pids."""
42+ elapsed = 0
43+ while elapsed < timeout :
44+ pids = self .get_pids_by_name ("taosBenchmark" )
45+ if pids :
46+ tdLog .info (f"Found taosBenchmark with PID: { pids } " )
47+ return pids
48+ time .sleep (interval )
49+ elapsed += interval
50+ tdLog .exit ("Timeout waiting for taosBenchmark process to start." )
51+ return []
52+
53+ #
54+ # ------------------- test_insert_cancel ----------------
55+ #
4056 def stopThread (self , isForceExit ):
41- tdLog .info ("dnodeNodeStopThread start" )
42- time . sleep ( 10 )
43- pids = self . get_pids_by_name ( "taosBenchmark" )
44- if pids :
45- tdLog . info ( f"Find a process named taosBbenchmark with PID: { pids } " )
46- else :
47- tdLog . exit ( "No process named taosBbenchmark was found." )
57+ tdLog .info (f"stopThread start, force= { isForceExit } " )
58+ pids = self . wait_for_benchmark ( )
59+ if not pids :
60+ tdLog . exit ( "No process named taosBenchmark was found." )
61+
62+ # Wait for benchmark to enter active insert phase
63+ time . sleep ( 3 )
4864
4965 os .kill (pids [0 ], signal .SIGINT )
5066 if isForceExit :
67+ # Send second SIGINT immediately (no delay!) to trigger forced exit
5168 os .kill (pids [0 ], signal .SIGINT )
5269
70+ # Wait for benchmark to finish and write output
5371 time .sleep (10 )
54-
72+
5573 if self ._rlist :
5674 tdLog .info (self ._rlist )
5775 if isForceExit :
5876 self .checkListString (self ._rlist , "Benchmark process forced exit!" )
59- else :
77+ else :
6078 self .checkListString (self ._rlist , "Receive SIGINT or other signal, quit benchmark" )
6179 else :
6280 tdLog .exit ("The benchmark process has not stopped!" )
6381
64-
65- def dbInsertThread (self ):
66- tdLog .info (f"dbInsertThread start" )
67- # taosBenchmark run
68- cmd = "-d db -t 10000 -n 10000 -T 8 -I stmt -y"
82+ def dbInsertThread (self , cmd ):
83+ tdLog .info (f"dbInsertThread start, cmd: { cmd } " )
6984 self ._rlist = self .benchmark (cmd , checkRun = False )
7085
7186 # run
7287 def do_insert_cancel (self ):
73- tdLog .info (f"start to excute { __file__ } " )
74- t1 = threading .Thread (target = self .dbInsertThread )
88+ tdLog .info (f"start to execute { __file__ } - insert cancel" )
89+
90+ # Use enough tables so benchmark is still running when SIGINT arrives
91+ cmd = "-d db -t 10000 -n 10000 -T 8 -I stmt -y"
92+
93+ # Test 1: graceful cancel (single SIGINT)
94+ self ._rlist = None
95+ t1 = threading .Thread (target = self .dbInsertThread , args = (cmd ,))
7596 t2 = threading .Thread (target = self .stopThread , args = (False ,))
7697 t1 .start ()
7798 t2 .start ()
78-
7999 t1 .join ()
80100 t2 .join ()
81101
82- t1 = threading .Thread (target = self .dbInsertThread )
102+ # Test 2: forced exit (double SIGINT)
103+ self ._rlist = None
104+ t1 = threading .Thread (target = self .dbInsertThread , args = (cmd ,))
83105 t2 = threading .Thread (target = self .stopThread , args = (True ,))
84106 t1 .start ()
85107 t2 .start ()
86-
87108 t1 .join ()
88109 t2 .join ()
89110
90- print ("do insert canceled .................... [passed]" )
111+ tdLog . info ("do insert canceled .................... [passed]" )
91112
92113 #
93- # ------------------- test_insert_error_exit.py ----------------
114+ # ------------------- test_insert_error_exit ----------------
94115 #
95116 def dnodeNodeStopThread (self ):
96117 tdLog .info ("dnodeNodeStopThread start" )
118+ pids = self .wait_for_benchmark ()
119+ if not pids :
120+ tdLog .exit ("No process named taosBenchmark was found." )
121+
122+ # Wait for benchmark to enter active insert phase
123+ time .sleep (3 )
124+ tdLog .info ("stopping dnode 1 ..." )
125+ sc .dnodeStop (1 )
126+
127+ # Wait for benchmark to detect error and exit
97128 time .sleep (10 )
98- sc .dnodeStop (2 )
99- time .sleep (10 )
129+
100130 if self ._rlist :
101131 tdLog .info (self ._rlist )
102132 self .checkListString (self ._rlist , "failed to execute insert statement. reason: Vnode stopped" )
103133 else :
104134 tdLog .exit ("The benchmark process has not stopped!" )
105135
106-
107- def dbInsertThread (self ):
108- tdLog .info (f"dbInsertThread start" )
109- # taosBenchmark run
110- cmd = "-d db -t 10000 -n 10000 -T 4 -I stmt -y"
111- self ._rlist = self .benchmark (cmd , checkRun = False )
112-
113136 # run
114137 def do_insert_error_exit (self ):
115- # init
116138 self ._rlist = None
117-
118- tdLog .info (f"start to excute { __file__ } " )
119- t1 = threading .Thread (target = self .dbInsertThread )
139+ tdLog .info (f"start to execute { __file__ } - insert error exit" )
140+
141+ # Use enough tables so benchmark is still running when dnode stops
142+ cmd = "-d db -t 10000 -n 10000 -T 4 -I stmt -y"
143+ t1 = threading .Thread (target = self .dbInsertThread , args = (cmd ,))
120144 t2 = threading .Thread (target = self .dnodeNodeStopThread )
121145 t1 .start ()
122146 t2 .start ()
123-
124147 t1 .join ()
125148 t2 .join ()
126149
127- print ("do insert error exit .................. [passed]" )
150+ tdLog . info ("do insert error exit .................. [passed]" )
128151
129152
130153 #
0 commit comments