107107max_memory_total = 0
108108min_memory_base = 1000 * 1000 * 1000
109109
110+ watchdog_timer = 0
111+ exit_watchdog = False
112+ watchdog_triggered = False
113+
110114# Ensure Simulator is still running despite Reset requests via OCPP or the rmt_ctrl interface
111115run_simulator = False
112116exit_run_simulator_process = False
@@ -115,6 +119,7 @@ def run_simulator_process():
115119
116120 global run_simulator
117121 global exit_run_simulator_process
122+ global watchdog_triggered
118123
119124 track_run_simulator = run_simulator
120125 iterations_since_last_check = 0
@@ -129,7 +134,7 @@ def run_simulator_process():
129134
130135 iterations_since_last_check = 0
131136
132- if not track_run_simulator and run_simulator :
137+ if not track_run_simulator and run_simulator and not watchdog_triggered :
133138 print ('Starting simulator' )
134139 os .system (os .path .join ('MicroOcppSimulator' , 'build' , 'mo_simulator' ) + ' &' )
135140 track_run_simulator = True
@@ -190,11 +195,44 @@ def setup_simulator():
190195
191196 print (' - done' )
192197
198+ def cleanup_test_driver ():
199+ try :
200+ print ("Stop Test Driver" )
201+ response = requests .post (os .environ ['TEST_DRIVER_URL' ] + '/session/stop' ,
202+ headers = {'Authorization' : 'Bearer ' + os .environ ['TEST_DRIVER_KEY' ]})
203+ print (f'Test Driver /session/stop:\n > { response .status_code } ' )
204+ #print(json.dumps(response.json(), indent=4))
205+ except :
206+ print ('Error stopping Test Driver' )
207+ traceback .print_exc ()
208+
209+ def watchdog_thread ():
210+
211+ global watchdog_timer
212+ global exit_watchdog
213+ global watchdog_triggered
214+
215+ while not exit_watchdog and watchdog_timer < 120 :
216+ watchdog_timer += 1
217+ time .sleep (1 )
218+
219+ if exit_watchdog :
220+ # watchdog has been exited gracefully
221+ return
222+
223+ watchdog_triggered = True
224+ cleanup_test_driver ()
225+ cleanup_simulator ()
226+ print ("\n Test execution timeout - terminate" )
227+ os ._exit (1 )
228+
193229def run_measurements ():
194230
195231 global max_memory_total
196232 global min_memory_base
197233 global run_simulator
234+ global watchdog_timer
235+ global watchdog_triggered
198236
199237 print ("Fetch TCs from Test Driver" )
200238
@@ -249,6 +287,11 @@ def run_measurements():
249287 print ('Test case already executed - skip' )
250288 continue
251289
290+ if watchdog_triggered :
291+ break
292+
293+ watchdog_timer = 0
294+
252295 setup_simulator ()
253296 time .sleep (1 )
254297
@@ -293,13 +336,7 @@ def run_measurements():
293336 # print('Test failure, abort')
294337 # break
295338
296- print ("Stop Test Driver" )
297-
298- response = requests .post (os .environ ['TEST_DRIVER_URL' ] + '/session/stop' ,
299- headers = {'Authorization' : 'Bearer ' + os .environ ['TEST_DRIVER_KEY' ]})
300- print (f'Test Driver /session/stop:\n > { response .status_code } ' )
301- #print(json.dumps(response.json(), indent=4))
302-
339+ cleanup_test_driver ()
303340 cleanup_simulator ()
304341
305342 print ('Store test results' )
@@ -336,6 +373,7 @@ def run_measurements():
336373def run_measurements_and_retry ():
337374
338375 global exit_run_simulator_process
376+ global exit_watchdog
339377
340378 if ( 'TEST_DRIVER_URL' not in os .environ or
341379 'TEST_DRIVER_CONFIG' not in os .environ or
@@ -346,6 +384,9 @@ def run_measurements_and_retry():
346384 'MO_SIM_RMT_CTRL_CERT' not in os .environ ):
347385 sys .exit ('\n Could not read environment variables' )
348386
387+ m_watchdog_thread = threading .Thread (target = watchdog_thread )
388+ m_watchdog_thread .start ()
389+
349390 n_tries = 3
350391
351392 for i in range (n_tries ):
@@ -367,25 +408,18 @@ def run_measurements_and_retry():
367408
368409 traceback .print_exc ()
369410
370- try :
371- print ("Stop Test Driver" )
372- response = requests .post (os .environ ['TEST_DRIVER_URL' ] + '/session/stop' ,
373- headers = {'Authorization' : 'Bearer ' + os .environ ['TEST_DRIVER_KEY' ]})
374- print (f'Test Driver /session/stop:\n > { response .status_code } ' )
375- #print(json.dumps(response.json(), indent=4))
376- except :
377- print ('Error stopping Test Driver' )
378- traceback .print_exc ()
379-
380- cleanup_simulator ()
381-
382- exit_run_simulator_process = True
383- run_simulator_thread .join ()
384-
385- if i + 1 < n_tries :
386- print ('Retry test cases' )
387- else :
388- print ('\n **Test case execution aborted**' )
389- sys .exit ('\n Error running test cases' )
411+ cleanup_test_driver ()
412+ cleanup_simulator ()
413+
414+ exit_run_simulator_process = True
415+ run_simulator_thread .join ()
416+
417+ if i + 1 < n_tries :
418+ print ('Retry test cases' )
419+ else :
420+ print ('\n **Test case execution aborted**' )
421+
422+ exit_watchdog = True # terminate watchdog thread
423+ m_watchdog_thread .join ()
390424
391425run_measurements_and_retry ()
0 commit comments