6
6
import traceback
7
7
from contextlib import closing
8
8
9
- import psutil
10
-
11
9
12
10
def before_scenario (context , scenario ):
13
11
context .debug = 'DEBUG' in os .environ and os .environ ['DEBUG' ] == 'ON'
@@ -35,21 +33,18 @@ def after_scenario(context, scenario):
35
33
if not is_server_listening (context .server_fqdn , context .server_port ):
36
34
print ("\x1b [33;101mERROR: Server stopped listening\x1b [0m" )
37
35
38
- if not pid_exists ( context .server_process .pid ) :
36
+ if context .server_process .poll () is not None :
39
37
assert False , f"Server not running pid={ context .server_process .pid } ..."
40
38
41
- server_graceful_shutdown (context )
39
+ server_graceful_shutdown (context ) # SIGINT
42
40
43
- # Wait few for socket to free up
44
- time .sleep (0.05 )
41
+ if context .server_process .wait (0.5 ) is None :
42
+ print (f"server still alive after 500ms, force-killing pid={ context .server_process .pid } ..." )
43
+ context .server_process .kill () # SIGKILL
44
+ context .server_process .wait ()
45
45
46
- attempts = 0
47
- while pid_exists (context .server_process .pid ) or is_server_listening (context .server_fqdn , context .server_port ):
48
- server_kill (context )
46
+ while is_server_listening (context .server_fqdn , context .server_port ):
49
47
time .sleep (0.1 )
50
- attempts += 1
51
- if attempts > 5 :
52
- server_kill_hard (context )
53
48
except :
54
49
exc = sys .exception ()
55
50
print ("error in after scenario:" )
@@ -61,26 +56,10 @@ def after_scenario(context, scenario):
61
56
def server_graceful_shutdown (context ):
62
57
print (f"shutting down server pid={ context .server_process .pid } ..." )
63
58
if os .name == 'nt' :
64
- os . kill ( context . server_process . pid , signal .CTRL_C_EVENT )
59
+ interrupt = signal .CTRL_C_EVENT
65
60
else :
66
- os .kill (context .server_process .pid , signal .SIGINT )
67
-
68
-
69
- def server_kill (context ):
70
- print (f"killing server pid={ context .server_process .pid } ..." )
71
- context .server_process .kill ()
72
-
73
-
74
- def server_kill_hard (context ):
75
- pid = context .server_process .pid
76
- path = context .server_path
77
-
78
- print (f"Server dangling exits, hard killing force { pid } ={ path } ..." )
79
- try :
80
- psutil .Process (pid ).kill ()
81
- except psutil .NoSuchProcess :
82
- return False
83
- return True
61
+ interrupt = signal .SIGINT
62
+ context .server_process .send_signal (interrupt )
84
63
85
64
86
65
def is_server_listening (server_fqdn , server_port ):
@@ -90,12 +69,3 @@ def is_server_listening(server_fqdn, server_port):
90
69
if _is_server_listening :
91
70
print (f"server is listening on { server_fqdn } :{ server_port } ..." )
92
71
return _is_server_listening
93
-
94
-
95
- def pid_exists (pid ):
96
- try :
97
- psutil .Process (pid )
98
- except psutil .NoSuchProcess :
99
- return False
100
- return True
101
-
0 commit comments