Skip to content

Commit 70d6755

Browse files
committed
server tests : use built-in subprocess features, not os.kill and psutil
1 parent 4560484 commit 70d6755

File tree

2 files changed

+10
-41
lines changed

2 files changed

+10
-41
lines changed

examples/server/tests/features/environment.py

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import traceback
77
from contextlib import closing
88

9-
import psutil
10-
119

1210
def before_scenario(context, scenario):
1311
context.debug = 'DEBUG' in os.environ and os.environ['DEBUG'] == 'ON'
@@ -35,21 +33,18 @@ def after_scenario(context, scenario):
3533
if not is_server_listening(context.server_fqdn, context.server_port):
3634
print("\x1b[33;101mERROR: Server stopped listening\x1b[0m")
3735

38-
if not pid_exists(context.server_process.pid):
36+
if context.server_process.poll() is not None:
3937
assert False, f"Server not running pid={context.server_process.pid} ..."
4038

41-
server_graceful_shutdown(context)
39+
server_graceful_shutdown(context) # SIGINT
4240

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()
4545

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):
4947
time.sleep(0.1)
50-
attempts += 1
51-
if attempts > 5:
52-
server_kill_hard(context)
5348
except:
5449
exc = sys.exception()
5550
print("error in after scenario:")
@@ -61,26 +56,10 @@ def after_scenario(context, scenario):
6156
def server_graceful_shutdown(context):
6257
print(f"shutting down server pid={context.server_process.pid} ...")
6358
if os.name == 'nt':
64-
os.kill(context.server_process.pid, signal.CTRL_C_EVENT)
59+
interrupt = signal.CTRL_C_EVENT
6560
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)
8463

8564

8665
def is_server_listening(server_fqdn, server_port):
@@ -90,12 +69,3 @@ def is_server_listening(server_fqdn, server_port):
9069
if _is_server_listening:
9170
print(f"server is listening on {server_fqdn}:{server_port}...")
9271
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-

examples/server/tests/requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ behave~=1.2.6
33
huggingface_hub~=0.20.3
44
numpy~=1.24.4
55
openai~=0.25.0
6-
psutil~=5.9.8
76
prometheus-client~=0.20.0

0 commit comments

Comments
 (0)