interplane.Get Omega
Read in your language

aien-architecture-mv

Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.

Brought by drakestapleton

branch main Branches Tags History

aien-architecture-mv / tools / ops / test_spark_window_close.sh

86 lines · 4940 bytes Raw Blame History
1#!/bin/sh
2# Offline test: a bare `spark-window close` must not close a window it does not own (L162).
3# Uses a scratch windows folder and scratch lock files; never touches the live windows.
4# Usage: tools/ops/test_spark_window_close.sh [path/to/spark-window]
5set -u
6here=$(cd "$(dirname "$0")" && pwd)
7SW=${1:-$here/spark-window}
8T=$(mktemp -d /tmp/spark-window-close-test.XXXXXX)
9SPARK_WINDOWS_ROOT=$T/windows SPARK_QUIET_LOCK=$T/quiet.lock SPARK_GB10_LOCK=$T/gb10.lock
10export SPARK_WINDOWS_ROOT SPARK_QUIET_LOCK SPARK_GB10_LOCK
11# The L405 gate path, want dir and board are temp too, and the idle check ignores the host load:
12# these temp locks have no other users, so the real machine load says nothing about them.
13export SPARK_QUIET_GATE=$T/quiet.gate SPARK_QUIET_WANT=$T/want.d SPARK_BOARD=$T/BOARD.md SPARK_IDLE_LOAD=100000
14unset SPARK_WINDOW
15pass=0; fail=0
16ok() { pass=$((pass + 1)); echo "ok $1"; }
17bad() { fail=$((fail + 1)); echo "FAIL $1"; }
18check() { if [ "$2" = "$3" ]; then ok "$1"; else bad "$1 (got '$2', want '$3')"; fi; }
19cleanup() {
20 for h in "$T"/windows/*/holder.pid; do [ -f "$h" ] && kill -- -"$(cat "$h")" 2>/dev/null; done
21 rm -rf "$T"
22}
23trap cleanup EXIT
24waitfor() { i=0; while [ ! -e "$1" ] && [ $i -lt 100 ]; do sleep 0.2; i=$((i + 1)); done; [ -e "$1" ]; }
25closed() { [ -f "$SPARK_WINDOWS_ROOT/$1/close.json" ]; }
26holder_alive() { kill -0 "$(cat "$SPARK_WINDOWS_ROOT/$1/holder.pid")" 2>/dev/null; }
27
28# Lane 1: its own script process. Opens inside $(...) like real lanes, waits for go1, then bare-closes.
29cat > "$T/lane1.sh" <<EOF
30id=\$("$SW" open lane1 --ledger L1 --gb10 none 2>/dev/null)
31echo "\$id" > "$T/id1"
32while [ ! -e "$T/go1" ]; do sleep 0.2; done
33"$SW" close > "$T/lane1.out" 2>&1
34echo \$? > "$T/lane1.rc"
35EOF
36sh "$T/lane1.sh" & lane1=$!
37waitfor "$T/id1" && [ -n "$(cat "$T/id1")" ] || { bad "lane 1 opened a window"; exit 1; }
38A=$(cat "$T/id1")
39ok "lane 1 opened window $A"
40check "window A is held" "$([ -f "$SPARK_WINDOWS_ROOT/$A/held" ] && echo yes)" yes
41
42# 1. A stranger's bare close is refused (exit 3), names the holder, and changes nothing.
43sh -c "\"$SW\" close" > "$T/stranger.out" 2>&1; rc=$?
44check "stranger bare close exits 3" "$rc" 3
45grep -q "$A" "$T/stranger.out" && grep -q 'owner=L1' "$T/stranger.out"; check "refusal prints the holder" "$?" 0
46check "window A still held after the refusal" "$(closed "$A" && echo closed || echo held)" held
47holder_alive "$A"; check "window A holder still alive" "$?" 0
48
49# 2. Wrong SPARK_WINDOW does not count as ownership.
50SPARK_WINDOW=20200101T000000Z-aaaaaa sh -c "\"$SW\" close" > /dev/null 2>&1; rc=$?
51check "bare close with another window's SPARK_WINDOW exits 3" "$rc" 3
52check "window A still held after wrong SPARK_WINDOW" "$(closed "$A" && echo closed || echo held)" held
53
54# 3. close with a bogus or unknown id touches nothing.
55sh -c "\"$SW\" close ../x" > /dev/null 2>&1; check "close ../x exits 2" "$?" 2
56sh -c "\"$SW\" close 20200101T000000Z-bbbbbb" > /dev/null 2>&1; check "close of an unknown id exits 1" "$?" 1
57check "window A still held after bad ids" "$(closed "$A" && echo closed || echo held)" held
58
59# 4. The owner's own bare close (same script, different subshell from the open) works.
60touch "$T/go1"; wait "$lane1"
61check "owner bare close exits 0" "$(cat "$T/lane1.rc")" 0
62check "window A closed by its owner" "$(closed "$A" && echo closed || echo held)" closed
63
64# 5. Incident replay: lane 2 opens B; lane 1's stale bare close (a new process, as a finishing
65# script after its lease ran out) must not close B; close A (already closed) by id must not either.
66B=$(sh -c "\"$SW\" open lane2 --ledger L2 --gb10 none 2>/dev/null"); [ -n "$B" ] || { bad "lane 2 opened a window"; exit 1; }
67ok "lane 2 opened window $B"
68sh "$T/lane1.sh" > /dev/null 2>&1 & stale=$! # a second lane-1 run: its open is refused (B is open), id empty
69sleep 1; touch "$T/go1"; wait "$stale"
70check "stale lane-1 bare close exits 3" "$(cat "$T/lane1.rc")" 3
71sh -c "\"$SW\" close" > /dev/null 2>&1; check "stale bare close exits 3" "$?" 3
72sh -c "\"$SW\" close $A" > /dev/null 2>&1; check "close of the already closed A exits 1" "$?" 1
73check "window B untouched" "$(closed "$B" && echo closed || echo held)" held
74holder_alive "$B"; check "window B holder alive" "$?" 0
75
76# 6. SPARK_WINDOW = the window id (what spark-window run sets) allows a bare close from anywhere.
77SPARK_WINDOW=$B sh -c "\"$SW\" close" > /dev/null 2>&1; check "bare close with SPARK_WINDOW=B exits 0" "$?" 0
78check "window B closed" "$(closed "$B" && echo closed || echo held)" closed
79
80# 7. close with an explicit id closes exactly that window, from any process.
81C=$(sh -c "\"$SW\" open lane3 --ledger L3 --gb10 none 2>/dev/null"); [ -n "$C" ] || { bad "lane 3 opened a window"; exit 1; }
82sh -c "\"$SW\" close $C" > /dev/null 2>&1; check "close <ID> exits 0" "$?" 0
83check "window C closed" "$(closed "$C" && echo closed || echo held)" closed
84
85echo "pass $pass fail $fail"
86[ "$fail" -eq 0 ]