Files
tidewall/tools/smoke.sh
T
claudeandClaude Opus 5.5 0797b11ab9 H7: Test the SIGTERM stop in smoke
tools/smoke.sh now starts the server through packaging/server/tidewall-server,
sends the wrapper SIGTERM once the server has started, and checks it writes its
stats and stop lines and exits 0, as H7 asked. make check runs it.

Refs #70

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 22:08:16 -05:00

103 lines
4.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Boots the headless server, runs a full-size match for 1,000 ticks, prints the timing
# statistics, and checks that the server:
# - exits 0 and logs start, stats and stop lines that all carry the seed,
# - produces the same state hash when run again with the same seed,
# - exits non-zero when a script error happens (a deliberate --fault),
# - exits non-zero on a bad command line,
# - listens on a UDP port when asked to.
#
# tools/smoke.sh unthrottled, so it finishes in seconds
# SMOKE_REALTIME=1 tools/smoke.sh at the real 30 Hz tick rate (about 34 seconds)
set -uo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")/.."
seed="${SMOKE_SEED:-20261109}"
ticks="${SMOKE_TICKS:-1000}"
pace="--unthrottled"
[[ "${SMOKE_REALTIME:-0}" == "1" ]] && pace=""
status=0
code=0
# run <args...>: runs the server, sets $output (JSON lines only) and $code.
run() {
local raw
raw="$(timeout --signal=KILL 300 tools/godot.sh -- --server "$@" 2>&1)"
code=$?
output="$(grep '^{' <<<"$raw")"
stderr_tail="$(grep -v '^{' <<<"$raw" | tail -n 15)"
}
# field <event> <key>: prints a value from the first log line of that event.
field() {
grep -m1 "\"event\":\"$1\"" <<<"$output" | sed -E "s/.*\"$2\":(\"[^\"]*\"|[^,}]*).*/\1/" | tr -d '"'
}
fail() {
echo "smoke FAIL: $*"
status=1
}
run --seed="$seed" --bots=24 --ticks="$ticks" --log-every=250 $pace
if [[ "$code" -ne 0 ]]; then
fail "server exited $code"
echo "$output"
echo "$stderr_tail"
fi
for event in start stats stop; do
grep -q "\"event\":\"$event\"" <<<"$output" || fail "no '$event' log line"
done
lines="$(wc -l <<<"$output")"
seeded="$(grep -c "\"seed\":$seed[,}]" <<<"$output")"
[[ "$lines" -eq "$seeded" ]] || fail "only $seeded of $lines log lines carry the seed"
[[ "$(field stats ticks)" == "$ticks" ]] || fail "ran $(field stats ticks) ticks, expected $ticks"
hash="$(field stats state_hash)"
echo "smoke: $ticks ticks, 24 bots, seed $seed"
echo "smoke: tick us min $(field stats min_us) mean $(field stats mean_us) p50 $(field stats p50_us)" \
"p95 $(field stats p95_us) p99 $(field stats p99_us) max $(field stats max_us)," \
"$(field stats over_budget) over the $(field stats budget_us) us budget"
echo "smoke: state hash $hash"
run --seed="$seed" --bots=24 --ticks="$ticks" --unthrottled
[[ "$code" -eq 0 && "$(field stats state_hash)" == "$hash" ]] \
|| fail "a second run with the same seed gave a different state hash"
run --seed="$seed" --bots=2 --ticks=50 --unthrottled --fault=script-error
[[ "$code" -eq 1 ]] || fail "a script error exited $code, expected 1"
[[ "$(field stop errors)" -ge 1 ]] || fail "a script error wasn't counted in the stop line"
run --bots=99 --nonsense
[[ "$code" -eq 2 ]] || fail "a bad command line exited $code, expected 2"
grep -q '"event":"config_error"' <<<"$output" || fail "a bad command line logged no config_error"
# A listening server opens its port, waits in the lobby with nobody there, and stops cleanly.
port=$((20000 + RANDOM % 20000))
run --listen --port="$port" --seed="$seed" --bots=4 --ticks=120 --unthrottled
[[ "$code" -eq 0 ]] || fail "a listening server exited $code"
[[ "$(field start listening)" == "true" ]] || fail "a listening server didn't say it was listening"
[[ "$(field start match_phase)" == "lobby" ]] || fail "a listening server didn't start in the lobby"
# SIGTERM to the packaged wrapper becomes a clean stop: stats and stop lines, exit 0.
state="$(mktemp -d)"
TIDEWALL_ENGINE="$(pwd)/tools/godot/$(sed -n 's/^\([0-9a-f]*\) *tools\/godot\/\(.*_linux\.x86_64\)$/\2/p' tools/engine.sha512)" \
TIDEWALL_GAME="$(pwd)" TIDEWALL_STATE="$state" \
packaging/server/tidewall-server --seed="$seed" --bots=2 >"$state/log" 2>&1 &
wrapper=$!
for _ in $(seq 1 100); do
grep -q '"event":"start"' "$state/log" 2>/dev/null && break
sleep 0.1
done
kill -TERM "$wrapper"
wait "$wrapper"
code=$?
output="$(grep '^{' "$state/log")"
rm -rf "$state"
[[ "$code" -eq 0 ]] || fail "SIGTERM to the server wrapper exited $code, expected 0"
grep -q '"event":"stats"' <<<"$output" || fail "SIGTERM left no stats line"
[[ "$(field stop exit_code)" == "0" ]] || fail "SIGTERM left no clean stop line"
if [[ "$status" -eq 0 ]]; then
echo "smoke: ok (clean run, reproducible hash, script error exits 1, bad config exits 2, listens, SIGTERM stops cleanly)"
fi
exit "$status"