Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 additions & 3 deletions test_startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,60 @@

set -euo pipefail

request() {
local url="$1" code
# Servers with reload=True can print their URL before the socket accepts connections.
code=$(curl --silent --location --retry 5 --retry-connrefused --retry-delay 1 --output /dev/null --write-out '%{http_code}' --max-time 5 "$url" || true)
if (( code < 200 || code >= 500 )); then
echo "Request to $url failed with HTTP status $code"
return 1
fi
}

run() {
pwd
output=$({ timeout 10 uv run --no-sync ./"$1" "${@:2}"; } 2>&1)
exitcode=$?
[[ $exitcode -eq 124 ]] && exitcode=0 # exitcode 124 is coming from "timeout command above"
local output_file
output_file=$(mktemp)
local request_output_file
request_output_file=$(mktemp)
local exitcode=0
local timeout_seconds=15
{ timeout "$timeout_seconds" uv run --no-sync ./"$1" "${@:2}" < <(sleep "$timeout_seconds"); } > "$output_file" 2>&1 &
local pid=$!

local ready=0
# Poll for the whole timeout window so slow-starting servers are still detected; exit early once ready or dead.
for ((i = 0; i < timeout_seconds * 5; i++)); do
if grep -qE "NiceGUI ready to go|Uvicorn running on http://127.0.0.1:8000" "$output_file"; then
ready=1
break
fi
if ! kill -0 "$pid" 2>/dev/null; then
break
fi
sleep 0.2
done

if [[ $ready -eq 1 ]]; then
local url
url=$(grep -Eo "http://(127\.0\.0\.1|localhost):[0-9]+" "$output_file" | head -n 1 || true)
request "${url:-http://127.0.0.1:8080}/" > "$request_output_file" 2>&1 || exitcode=1
else
exitcode=1
fi

local process_exit=0
wait "$pid" || process_exit=$?
[[ $process_exit -eq 124 ]] && process_exit=0 # exitcode 124 is coming from "timeout command above"
[[ $process_exit -ne 0 ]] && exitcode=$process_exit
local output
output=$(cat "$output_file")
local request_output
request_output=$(cat "$request_output_file")
rm "$output_file" "$request_output_file"
if [[ -n $request_output ]]; then
output="$output"$'\n'"$request_output"
fi
echo "$output" | grep -qE "NiceGUI ready to go|Uvicorn running on http://127.0.0.1:8000" || exitcode=1
echo "$output" | grep -qE "Traceback|Error" && exitcode=1
if [[ $exitcode -ne 0 ]]; then
Expand Down