Skip to content

Commit

Permalink
ci: tests: fix behavior on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
phymbert committed Mar 17, 2024
1 parent b24f30f commit fcf327f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 67 deletions.
113 changes: 48 additions & 65 deletions examples/server/tests/features/environment.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import errno
import os
import signal
import socket
import subprocess
import sys
import time
import traceback
from contextlib import closing
import signal

import psutil


def before_scenario(context, scenario):
Expand All @@ -20,37 +22,40 @@ def before_scenario(context, scenario):


def after_scenario(context, scenario):
if context.server_process is None:
return
if scenario.status == "failed":
if 'GITHUB_ACTIONS' in os.environ:
print(f"\x1b[33;101mSCENARIO FAILED: {scenario.name} server logs:\x1b[0m\n\n")
if os.path.isfile('llama.log'):
with closing(open('llama.log', 'r')) as f:
for line in f:
print(line)
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):
print_server_logs(context)
assert False, f"Server not running pid={context.server_process.pid} ..."

server_graceful_shutdown(context)

# Wait few for socket to free up
time.sleep(0.05)

attempts = 0
while pid_exists(context.server_process.pid) or is_server_listening(context.server_fqdn, context.server_port):
server_kill(context)
time.sleep(0.1)
attempts += 1
if attempts > 5:
server_kill_hard(context)

if scenario.status == "failed" or context.debug:
print_server_logs(context)
try:
if 'server_process' not in context or context.server_process is None:
return
if scenario.status == "failed":
if 'GITHUB_ACTIONS' in os.environ:
print(f"\x1b[33;101mSCENARIO FAILED: {scenario.name} server logs:\x1b[0m\n\n")
if os.path.isfile('llama.log'):
with closing(open('llama.log', 'r')) as f:
for line in f:
print(line)
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):
assert False, f"Server not running pid={context.server_process.pid} ..."

server_graceful_shutdown(context)

# Wait few for socket to free up
time.sleep(0.05)

attempts = 0
while pid_exists(context.server_process.pid) or is_server_listening(context.server_fqdn, context.server_port):
server_kill(context)
time.sleep(0.1)
attempts += 1
if attempts > 5:
server_kill_hard(context)
except:
exc = sys.exception()
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):
Expand All @@ -71,11 +76,11 @@ def server_kill_hard(context):
path = context.server_path

print(f"Server dangling exits, hard killing force {pid}={path}...\n")
if os.name == 'nt':
process = subprocess.check_output(['taskkill', '/F', '/pid', str(pid)]).decode()
print(process)
else:
os.kill(-pid, signal.SIGKILL)
try:
psutil.Process(pid).kill()
except psutil.NoSuchProcess:
return False
return True


def is_server_listening(server_fqdn, server_port):
Expand All @@ -88,31 +93,9 @@ def is_server_listening(server_fqdn, server_port):


def pid_exists(pid):
"""Check whether pid exists in the current process table."""
if pid < 0:
try:
psutil.Process(pid)
except psutil.NoSuchProcess:
return False
if os.name == 'nt':
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")
return True

19 changes: 17 additions & 2 deletions examples/server/tests/features/steps/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import re
import socket
import subprocess
import sys
import threading
import time
from contextlib import closing
from re import RegexFlag
Expand Down Expand Up @@ -1095,10 +1097,23 @@ def start_server_background(context):

pkwargs = {
'creationflags': flags,
'stderr': subprocess.PIPE,
'stdout': subprocess.PIPE
'stdout': subprocess.PIPE,
'stderr': subprocess.PIPE
}
context.server_process = subprocess.Popen(
[str(arg) for arg in [context.server_path, *server_args]],
**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()}")
1 change: 1 addition & 0 deletions examples/server/tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ behave~=1.2.6
huggingface_hub~=0.20.3
numpy~=1.24.4
openai~=0.25.0
psutil~=5.9.8
prometheus-client~=0.20.0

0 comments on commit fcf327f

Please sign in to comment.