Skip to content

Commit

Permalink
fail faster in integration test (reflex-dev#1493)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackie-pc authored Aug 1, 2023
1 parent 95b6ff4 commit f9be9d6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
13 changes: 11 additions & 2 deletions scripts/integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ export PYTHONUNBUFFERED=1
# Start the server in the background
reflex run --loglevel debug --env "$2" & pid=$!

# TODO does this even work on windows? Not clear, possibly not impactful though.
trap "kill -INT $pid ||:" EXIT
# Within the context of this bash, $pid_in_bash is what we need to pass to "kill" on exit
# This is true on all platforms.
pid_in_bash=$pid
trap "kill -INT $pid_in_bash ||:" EXIT

echo "Started server with PID $pid"

# Assume we run from the root of the repo
popd

# In Windows, our Python script below needs to work with the WINPID
if [ -f /proc/$pid/winpid ]; then
pid=$(cat /proc/$pid/winpid)
echo "Windows detected, passing winpid $pid to port waiter"
fi

python scripts/wait_for_listening_port.py 3000 8000 --timeout=600 --server-pid "$pid"
34 changes: 26 additions & 8 deletions scripts/wait_for_listening_port.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,36 @@
import socket
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Tuple

# psutil is already a dependency of Reflex itself - so it's OK to use
import psutil

def _wait_for_port(port, server_pid, timeout):

def _pid_exists(pid):
# os.kill(pid, 0) doesn't work on Windows (actually kills the PID)
# psutil.pid_exists() doesn't work on Windows (does os.kill underneath)
# psutil.pids() seems to return the right thing. Inefficient but doesn't matter - keeps things simple.
#
# Note: For windows, the pid here is really the "winpid".
return pid in psutil.pids()


def _wait_for_port(port, server_pid, timeout) -> Tuple[bool, str]:
start = time.time()
print(f"Waiting for up to {timeout} seconds for port {port} to start listening.")
while True:
# TODO fail early if server pid not there
if not _pid_exists(server_pid):
return False, f"Server PID {server_pid} is not running."
try:
socket.create_connection(("localhost", port), timeout=0.5)
print(f"OK! Port {port} is listening after {time.time() - start} seconds")
return True
return True, f"Port {port} is listening after {time.time() - start} seconds"
except Exception:
if time.time() - start > timeout:
print(f"FAIL: Port {port} still not listening after {timeout} seconds.")
return False
return (
False,
f"Port {port} still not listening after {timeout} seconds.",
)
time.sleep(5)


Expand All @@ -39,8 +54,11 @@ def main():
executor.submit(_wait_for_port, p, args.server_pid, args.timeout)
)
for f in as_completed(futures):
if not f.result():
print("At least one port failed... exiting with failure.")
ok, msg = f.result()
if ok:
print(f"OK: {msg}")
else:
print(f"FAIL: {msg}")
exit(1)


Expand Down

0 comments on commit f9be9d6

Please sign in to comment.