forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

boot_race.sh72 lines, 3.0 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# Boot N copies of the backend at once against a Postgres whose school
3# template has just been dropped, and report what each boot did.
4#
5# This is the reproduction for the concurrent-boot class. Two things are being
6# watched:
7# * every boot must reach `listening` — overlapping `CREATE DATABASE`s used
8# to kill the losers, because Postgres reports the losing side as a
9# `pg_database_datname_index` unique violation (`23505`) rather than the
10# `42P04` duplicate-database error, and the swallow only matched `42P04`;
11# * the server log must stay clean *of create refusals* — a create that is
12# issued and refused is logged by the server even when the client swallows
13# the error, which is why the create now runs behind a probe and under an
14# advisory lock. The assertion is scoped to those lines so that a test
15# suite sharing the server (its deliberate foreign-key violations) does
16# not read as a boot failure.
17# Exit status is non-zero if any boot failed or the server logged a create
18# refusal.
19#
20# The template of the control database is dropped first; the next boot (this
21# script, or your usual `podman compose up -d backend`) rebuilds and migrates
22# it, so a dev database is the only thing this may disturb.
23#
24# Needs the compose Postgres up (`podman compose up -d postgres`) and a built
25# binary (`cargo build`).
26set -euo pipefail
27
28CONTAINER="${HEZARFEN_PG_CONTAINER:-hezarfen_backend_postgres}"
29CONTROL_DB="${HEZARFEN_CONTROL_DB:-hezarfen_control}"
30N="${N:-8}"
31BASE_PORT="${BASE_PORT:-7800}"
32OUT="${OUT:-$(mktemp -d)}"
33HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
34BIN="${BIN:-$HERE/target/debug/hezarfen_backend}"
35
36psql() { podman exec -i "$CONTAINER" psql -U hezarfen -v ON_ERROR_STOP=1 "$@"; }
37
38echo "== dropping ${CONTROL_DB}_school_template (the next boot rebuilds it)"
39psql -d postgres -q -c "DROP DATABASE IF EXISTS \"${CONTROL_DB}_school_template\""
40before=$(podman logs "$CONTAINER" 2>&1 | wc -l)
41
42echo "== racing $N boots on ports $BASE_PORT..$((BASE_PORT + N - 1))"
43for i in $(seq 0 $((N - 1))); do
44 (
45 HOST=127.0.0.1 PORT=$((BASE_PORT + i)) \
46 DATABASE_URL="postgres://hezarfen:hezarfen@127.0.0.1:5432/${CONTROL_DB}" \
47 FILES_PATH="${FILES_PATH:-/tmp/hez-files}" RUST_LOG=info \
48 timeout 30 "$BIN" >"$OUT/boot$i.log" 2>&1
49 ) &
50done
51wait
52
53fail=0
54for i in $(seq 0 $((N - 1))); do
55 if grep -q "listening on" "$OUT/boot$i.log"; then
56 echo " boot$i (port $((BASE_PORT + i))): up"
57 else
58 echo " boot$i (port $((BASE_PORT + i))): FAILED"
59 tail -3 "$OUT/boot$i.log"
60 fail=1
61 fi
62done
63
64errors=$(podman logs "$CONTAINER" 2>&1 | tail -n +$((before + 1)) |
65 grep -E 'pg_database_datname_index|already exists' | grep -cE 'ERROR|FATAL' || true)
66echo "== server log since the race: $errors create-refusal line(s)"
67[ "$errors" -eq 0 ] || fail=1
68
69echo "== templates now on the server"
70psql -d postgres -Atc "SELECT datname FROM pg_database WHERE datname LIKE '%_school_template'"
71echo "== boot logs: $OUT"
72exit "$fail"