ci: tests: fix behavior on windows

This commit is contained in:
Pierrick HYMBERT 2024-03-17 17:45:09 +01:00
parent b24f30fdad
commit fcf327f0e6
3 changed files with 64 additions and 65 deletions

View file

@ -1,10 +1,12 @@
import errno
import os import os
import socket
import subprocess
import time
from contextlib import closing
import signal import signal
import socket
import sys
import time
import traceback
from contextlib import closing
import psutil
def before_scenario(context, scenario): def before_scenario(context, scenario):
@ -20,37 +22,40 @@ def before_scenario(context, scenario):
def after_scenario(context, scenario): def after_scenario(context, scenario):
if context.server_process is None: try:
return if 'server_process' not in context or context.server_process is None:
if scenario.status == "failed": return
if 'GITHUB_ACTIONS' in os.environ: if scenario.status == "failed":
print(f"\x1b[33;101mSCENARIO FAILED: {scenario.name} server logs:\x1b[0m\n\n") if 'GITHUB_ACTIONS' in os.environ:
if os.path.isfile('llama.log'): print(f"\x1b[33;101mSCENARIO FAILED: {scenario.name} server logs:\x1b[0m\n\n")
with closing(open('llama.log', 'r')) as f: if os.path.isfile('llama.log'):
for line in f: with closing(open('llama.log', 'r')) as f:
print(line) for line in f:
if not is_server_listening(context.server_fqdn, context.server_port): print(line)
print("\x1b[33;101mERROR: Server stopped listening\x1b[0m\n") if not is_server_listening(context.server_fqdn, context.server_port):
print("\x1b[33;101mERROR: Server stopped listening\x1b[0m\n")
if not pid_exists(context.server_process.pid): if not pid_exists(context.server_process.pid):
print_server_logs(context) 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)
# Wait few for socket to free up # Wait few for socket to free up
time.sleep(0.05) time.sleep(0.05)
attempts = 0 attempts = 0
while pid_exists(context.server_process.pid) or 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) server_kill(context)
time.sleep(0.1) time.sleep(0.1)
attempts += 1 attempts += 1
if attempts > 5: if attempts > 5:
server_kill_hard(context) server_kill_hard(context)
except:
if scenario.status == "failed" or context.debug: exc = sys.exception()
print_server_logs(context) print("error in after scenario: \n")
print(exc)
print("*** print_tb: \n")
traceback.print_tb(exc.__traceback__, file=sys.stdout)
def server_graceful_shutdown(context): def server_graceful_shutdown(context):
@ -71,11 +76,11 @@ def server_kill_hard(context):
path = context.server_path path = context.server_path
print(f"Server dangling exits, hard killing force {pid}={path}...\n") print(f"Server dangling exits, hard killing force {pid}={path}...\n")
if os.name == 'nt': try:
process = subprocess.check_output(['taskkill', '/F', '/pid', str(pid)]).decode() psutil.Process(pid).kill()
print(process) except psutil.NoSuchProcess:
else: return False
os.kill(-pid, signal.SIGKILL) return True
def is_server_listening(server_fqdn, server_port): def is_server_listening(server_fqdn, server_port):
@ -88,31 +93,9 @@ def is_server_listening(server_fqdn, server_port):
def pid_exists(pid): def pid_exists(pid):
"""Check whether pid exists in the current process table.""" try:
if pid < 0: psutil.Process(pid)
except psutil.NoSuchProcess:
return False return False
if os.name == 'nt': return True
output = subprocess.check_output(['TASKLIST', '/FI', f'pid eq {pid}']).decode()
print(output)
return "No tasks are running" not in output
else:
try:
os.kill(pid, 0)
except OSError as e:
return e.errno == errno.EPERM
else:
return True
def print_server_logs(context):
print("Trying to find server logs:")
out, err = context.server_process.communicate()
if out:
print("Server stdout:\n")
print(out.decode('utf-8'))
print("\n")
if err:
print("Server stderr:\n")
print(err.decode('utf-8'))
print("\n")

View file

@ -5,6 +5,8 @@ import os
import re import re
import socket import socket
import subprocess import subprocess
import sys
import threading
import time import time
from contextlib import closing from contextlib import closing
from re import RegexFlag from re import RegexFlag
@ -1095,10 +1097,23 @@ def start_server_background(context):
pkwargs = { pkwargs = {
'creationflags': flags, 'creationflags': flags,
'stderr': subprocess.PIPE, 'stdout': subprocess.PIPE,
'stdout': subprocess.PIPE 'stderr': subprocess.PIPE
} }
context.server_process = subprocess.Popen( context.server_process = subprocess.Popen(
[str(arg) for arg in [context.server_path, *server_args]], [str(arg) for arg in [context.server_path, *server_args]],
**pkwargs) **pkwargs)
def log_stdout(process):
for line in iter(process.stdout.readline, b''):
print(line.decode('utf-8'), end='')
thread_stdout = threading.Thread(target=log_stdout, args=(context.server_process,))
thread_stdout.start()
def log_stderr(process):
for line in iter(process.stderr.readline, b''):
print(line.decode('utf-8'), end='', file=sys.stderr)
thread_stderr = threading.Thread(target=log_stderr, args=(context.server_process,))
thread_stderr.start()
print(f"server pid={context.server_process.pid}, behave pid={os.getpid()}") print(f"server pid={context.server_process.pid}, behave pid={os.getpid()}")

View file

@ -3,4 +3,5 @@ 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