server tests : use built-in subprocess features, not os.kill and psutil
This commit is contained in:
parent
4560484488
commit
70d675580f
2 changed files with 10 additions and 41 deletions
|
@ -6,8 +6,6 @@ import time
|
||||||
import traceback
|
import traceback
|
||||||
from contextlib import closing
|
from contextlib import closing
|
||||||
|
|
||||||
import psutil
|
|
||||||
|
|
||||||
|
|
||||||
def before_scenario(context, scenario):
|
def before_scenario(context, scenario):
|
||||||
context.debug = 'DEBUG' in os.environ and os.environ['DEBUG'] == 'ON'
|
context.debug = 'DEBUG' in os.environ and os.environ['DEBUG'] == 'ON'
|
||||||
|
@ -35,21 +33,18 @@ def after_scenario(context, scenario):
|
||||||
if not is_server_listening(context.server_fqdn, context.server_port):
|
if not is_server_listening(context.server_fqdn, context.server_port):
|
||||||
print("\x1b[33;101mERROR: Server stopped listening\x1b[0m")
|
print("\x1b[33;101mERROR: Server stopped listening\x1b[0m")
|
||||||
|
|
||||||
if not pid_exists(context.server_process.pid):
|
if context.server_process.poll() is not None:
|
||||||
assert False, f"Server not running pid={context.server_process.pid} ..."
|
assert False, f"Server not running pid={context.server_process.pid} ..."
|
||||||
|
|
||||||
server_graceful_shutdown(context)
|
server_graceful_shutdown(context) # SIGINT
|
||||||
|
|
||||||
# Wait few for socket to free up
|
if context.server_process.wait(0.5) is None:
|
||||||
time.sleep(0.05)
|
print(f"server still alive after 500ms, force-killing pid={context.server_process.pid} ...")
|
||||||
|
context.server_process.kill() # SIGKILL
|
||||||
|
context.server_process.wait()
|
||||||
|
|
||||||
attempts = 0
|
while is_server_listening(context.server_fqdn, context.server_port):
|
||||||
while pid_exists(context.server_process.pid) or is_server_listening(context.server_fqdn, context.server_port):
|
|
||||||
server_kill(context)
|
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
attempts += 1
|
|
||||||
if attempts > 5:
|
|
||||||
server_kill_hard(context)
|
|
||||||
except:
|
except:
|
||||||
exc = sys.exception()
|
exc = sys.exception()
|
||||||
print("error in after scenario:")
|
print("error in after scenario:")
|
||||||
|
@ -61,26 +56,10 @@ def after_scenario(context, scenario):
|
||||||
def server_graceful_shutdown(context):
|
def server_graceful_shutdown(context):
|
||||||
print(f"shutting down server pid={context.server_process.pid} ...")
|
print(f"shutting down server pid={context.server_process.pid} ...")
|
||||||
if os.name == 'nt':
|
if os.name == 'nt':
|
||||||
os.kill(context.server_process.pid, signal.CTRL_C_EVENT)
|
interrupt = signal.CTRL_C_EVENT
|
||||||
else:
|
else:
|
||||||
os.kill(context.server_process.pid, signal.SIGINT)
|
interrupt = signal.SIGINT
|
||||||
|
context.server_process.send_signal(interrupt)
|
||||||
|
|
||||||
def server_kill(context):
|
|
||||||
print(f"killing server pid={context.server_process.pid} ...")
|
|
||||||
context.server_process.kill()
|
|
||||||
|
|
||||||
|
|
||||||
def server_kill_hard(context):
|
|
||||||
pid = context.server_process.pid
|
|
||||||
path = context.server_path
|
|
||||||
|
|
||||||
print(f"Server dangling exits, hard killing force {pid}={path}...")
|
|
||||||
try:
|
|
||||||
psutil.Process(pid).kill()
|
|
||||||
except psutil.NoSuchProcess:
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def is_server_listening(server_fqdn, server_port):
|
def is_server_listening(server_fqdn, server_port):
|
||||||
|
@ -90,12 +69,3 @@ def is_server_listening(server_fqdn, server_port):
|
||||||
if _is_server_listening:
|
if _is_server_listening:
|
||||||
print(f"server is listening on {server_fqdn}:{server_port}...")
|
print(f"server is listening on {server_fqdn}:{server_port}...")
|
||||||
return _is_server_listening
|
return _is_server_listening
|
||||||
|
|
||||||
|
|
||||||
def pid_exists(pid):
|
|
||||||
try:
|
|
||||||
psutil.Process(pid)
|
|
||||||
except psutil.NoSuchProcess:
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
|
@ -3,5 +3,4 @@ behave~=1.2.6
|
||||||
huggingface_hub~=0.20.3
|
huggingface_hub~=0.20.3
|
||||||
numpy~=1.24.4
|
numpy~=1.24.4
|
||||||
openai~=0.25.0
|
openai~=0.25.0
|
||||||
psutil~=5.9.8
|
|
||||||
prometheus-client~=0.20.0
|
prometheus-client~=0.20.0
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue