#!/usr/bin/env bash # Shared environment. Point JAVA_HOME at a JDK 25 (or newer) installation. : "${JAVA_HOME:?set JAVA_HOME to a JDK 25+ installation}" export PATH="$JAVA_HOME/bin:$PATH" MVN="${MVN:-mvn}" JAR="target/transactions-1.0.0.jar" APP_PORT="${APP_PORT:-8081}" LOG="${LOG:-/tmp/transactions-demo.log}" # Strip machine-specific noise from committed transcripts. clean() { grep -v "Picked up JAVA_TOOL_OPTIONS" | grep -v "^OpenJDK 64-Bit Server VM warning"; } # Reduce a Spring log line to its message, so transcripts diff cleanly between runs. tidy() { sed -E 's/^[0-9T:.-]+Z +//; s/^[A-Z]+ +[0-9]+ --- \[[^]]*\] \[[^]]*\] +//; s/ +: /: /'; } # Start detached and block until it answers. Deliberately NOT setsid: setsid forks when it is # not already a process-group leader, so $! would name a process that exits immediately and # the JVM would survive every later stop_app -- holding the port, so the next scenario fails # to bind and curl answers from the previous one. That reads exactly like the configuration # under test having had no effect. start_app() { stop_app mkdir -p target nohup java -jar "$JAR" "$@" > "$LOG" 2>&1 < /dev/null & echo $! > target/app.pid for _ in $(seq 1 60); do curl -s -o /dev/null "http://127.0.0.1:${APP_PORT}/tx/silent" 2>/dev/null && return 0 kill -0 "$(cat target/app.pid)" 2>/dev/null || { echo "JVM exited during startup:" tail -20 "$LOG"; return 1; } sleep 1 done echo "application did not answer"; tail -20 "$LOG"; return 1 } # Stop by recorded PID. Never by pattern: `ps | grep transactions` also matches the shell # running this script, because that string is on its own command line. stop_app() { if [ -f target/app.pid ]; then pid=$(cat target/app.pid) if [ -n "$pid" ] && grep -qa "transactions" "/proc/$pid/cmdline" 2>/dev/null; then kill -9 "$pid" 2>/dev/null || true wait "$pid" 2>/dev/null || true fi rm -f target/app.pid fi for _ in $(seq 1 40); do if ! (exec 3<>/dev/tcp/127.0.0.1/"${APP_PORT}") 2>/dev/null; then break; fi sleep 0.25 done exec 3<&- 2>/dev/null || true }