clone

a personal forge on plain git: static pages, stacked-diff reviews, signed local ci, repo sites and releases

main
forge / test / scenarios / lake-lock.sh
lake-lock.sh 1.8K
1# One writer at a time, under lake.lock: a second forge lake waits for the2# first, or gives up after FORGE_LAKE_WAIT seconds and says so. A lock whose3# process is gone is taken over. Two runs at once add each fact once.4. "$(dirname "$0")/../lib.sh"5user dami6repo dami/greet7clone dami dami/greet "$T/w/d"8at "$T/w/d" commit "first"9push_as dami "$T/w/d" origin main10lock=$FORGE_ROOT/.forge/lake.lock11hold() { sleep "$1" & holder=$!; T_PIDS="$T_PIDS $holder"; mkdir "$lock"; echo "$holder" >"$lock/pid"; }12hold 30013fails_with "another forge lake has held $lock for 1s" env FORGE_LAKE_WAIT=1 "$FORGE" lake14missing "$FORGE_ROOT/.forge/lake/catalog.sqlite"15# Its holder died: the next run takes the lock over.16kill "$holder"17wait "$holder" 2>/dev/null || true18ok forge lake19has "$T_LOG" '^lake: added 1 event,'20missing "$lock"21# A run that finds the lock taken waits for it.22hold 30023(sleep 2 && rm -rf "$lock") &24t0=$(date +%s)25ok env FORGE_LAKE_WAIT=30 "$FORGE" lake26[ $(($(date +%s) - t0)) -ge 2 ] || fail "forge lake didn't wait for the lock"27kill "$holder"28# Two at once: one adds the new events, the other finds nothing left.29ev=$FORGE_ROOT/.forge/events.jsonl30i=031while [ "$i" -lt 50 ]; do32 i=$((i + 1))33 printf '{"t":%s000,"repo":"dami/greet","type":"push","user":"dami","refs":"refs/heads/n%s"}\n' "$((T_NOW + i))" "$i" >>"$ev"34done35"$FORGE" lake >"$T/a" 2>&1 & a=$!36"$FORGE" lake >"$T/b" 2>&1 & b=$!37wait "$a" || fail "the first of two forge lakes failed: $(cat "$T/a")"38wait "$b" || fail "the second of two forge lakes failed: $(cat "$T/b")"39eq "$(cat "$T/a" "$T/b" | grep -c '^lake: added 50 events,')" 140eq "$(cat "$T/a" "$T/b" | grep -c '^lake: added 0 events,')" 141eq "$(lq "SELECT count(*) || ' ' || count(DISTINCT refs) FROM events WHERE refs LIKE 'refs/heads/n%'")" "50 50"