carlos / platform Public

ci: compile split packages' tests side by side; two test flakes fixed

- test-gate.sh starts every split package's go test -list (which
  compiles its test binary) at once, and each package's shards start as
  soon as its own list is ready. Before, the lists ran in turn and
  host's shards started at +86 s; now all start by about +45 s on a warm
  cache. Each list is still its own go command, so the fence still
  counts one package's tests exactly. The gate prints each list's time.
- edge: TestLocalUpstreamRestartWitness held no port between upstream
  restarts and another test process took it; heldTCPPort keeps it with
  an SO_REUSEPORT socket that never listens.
- console: the claim stage recorder times the first stage from
  domInteractive, which is always before the page starts its 650 ms
  clock (648.4 ms on CI).

An unrelated activator flake seen on the way is discussion 880.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Squashed from gate-parallel-lists into main.
claude agent, operated by paul@keymail.dev e6d1f204572deb155ee623ecacd5d953bc78cd53 parent 225c41d
4 files changed, +167 −51
  • .amadan/test-gate.sh +69 −12
  • internal/console/testdata/demo_claim_layout.mjs +12 −5
  • internal/edge/deadaddr_test.go +82 −0
  • internal/edge/localhold_witness_test.go +4 −34

CI passed — run details

diff --git a/.amadan/test-gate.sh b/.amadan/test-gate.sh
index 0632a0f37..aca787753 100755
--- a/.amadan/test-gate.sh
+++ b/.amadan/test-gate.sh
@@ -51,8 +51,9 @@
# how long it took, so a slow run says whether it was short of cores.
set -eu
-# Heaviest first: discovery compiles each package's tests in turn, so the
-# packages that dominate the wall clock get their shards started earliest.
+# Heaviest first: shards start in this order once their package's tests
+# have compiled, and the packages that dominate the wall clock should not
+# queue behind the others.
: "${TEST_SPLIT=./internal/console=24 ./internal/edge=4 ./internal/activator=4 ./internal/host=6 .=5 ./internal/grouproll=2}"
weights=$(pwd)/.amadan/test-weights.tsv
gate_t0=$(date +%s)
@@ -168,6 +169,29 @@ start launchctx sh -c 'cd launchctx && go vet ./... && go test ./... -count=1'
start vet go vet ./...
start gofmt sh -c 'fmt_out=$(gofmt -l .); if [ -n "$fmt_out" ]; then echo "gofmt needed:"; echo "$fmt_out"; exit 1; fi; echo "gofmt -l .: clean"'
+# The split packages' test lists, all at once. `go test -list` compiles the
+# test binary, which the shards then reuse from the build cache. One after
+# another the compiles held the last packages' shards back by a minute and
+# a half (host's started at +86 s on 2026-11-11); side by side, each
+# package waits only for its own. The deal below reads each list in turn,
+# as before. Each is its own go command, so a package's list is exactly
+# that package's: the fence counts on it.
+# A package's tag names its files, its shards and a shell variable, so
+# anything but letters, digits and _ becomes _.
+split_tag() { echo "$1" | sed 's|^\./||; s|^\.$|root|; s|[^A-Za-z0-9_]|_|g'; }
+for spec in $TEST_SPLIT; do
+ tag=$(split_tag "${spec%=*}")
+ (
+ set +e
+ t0=$(date +%s)
+ go test -count=1 -list '.*' "${spec%=*}" >"$out/list-$tag.raw" 2>&1
+ echo $? >"$out/list-$tag.rc"
+ echo $(($(date +%s) - t0)) >"$out/list-$tag.secs"
+ ) &
+ started="$started list-$tag:$!"
+ eval "list_pid_$tag=\$!"
+done
+
# Everything not split runs as one ordinary `go test`. Packages are named
# by import path, so match TEST_SPLIT's directories through `go list`.
split_paths=""
@@ -209,26 +233,30 @@ if [ "${GATE_WEIGHTS:-}" = 1 ]; then
shard_json=-json
fi
-for spec in $TEST_SPLIT; do
+# deal_split deals one split package's listed tests onto its shards and
+# starts them.
+deal_split() {
+ spec=$1
dir=${spec%=*}
shards=${spec#*=}
discover go list "$dir" >"$out/go-list"
pkg=$(cat "$out/go-list")
- tag=$(echo "$dir" | sed 's|^\./||; s|^\.$|root|; s|/|_|g')
+ tag=$(split_tag "$dir")
- # -list compiles the test binary, which the shards below then reuse
- # from the build cache. Benchmarks are listed but never run by a plain
- # `go test`, so they are left out of the deal.
- if ! discover go test -count=1 -list '.*' "$dir" >"$out/list-$tag.raw" 2>&1; then
- cp "$out/list-$tag.raw" "$out/$tag.log"
+ # This package's list, started above, has finished. A list with no
+ # status file was killed before it could write one. Benchmarks are
+ # listed but never run by a plain `go test`, so they are left out of
+ # the deal.
+ if [ "$(cat "$out/list-$tag.rc" 2>/dev/null || echo 1)" -ne 0 ]; then
+ cp "$out/list-$tag.raw" "$out/$tag.log" 2>/dev/null || true
fail_job "$tag" "could not list the tests in $pkg"
- continue
+ return
fi
grep -E '^(Test|Example|Fuzz)' "$out/list-$tag.raw" | sort -u >"$out/list-$tag" || true
listed=$(wc -l <"$out/list-$tag")
if [ "$listed" -eq 0 ]; then
fail_job "$tag" "go test -list found no tests in $pkg, which is not believable"
- continue
+ return
fi
# Longest-processing-time first: heaviest test onto the least-loaded
@@ -249,7 +277,7 @@ for spec in $TEST_SPLIT; do
dealt=$(wc -l <"$out/deal-$tag")
if [ "$dealt" -ne "$listed" ]; then
fail_job "$tag" "dealt $dealt of $listed tests in $pkg"
- continue
+ return
fi
i=0
@@ -265,6 +293,26 @@ for spec in $TEST_SPLIT; do
if [ "$launched" -ne "$listed" ]; then
fail_job "$tag-deal" "launched $launched of $listed tests in $pkg"
fi
+}
+
+# Each package's shards start as soon as its own list is ready, whatever
+# order the lists finish in. A list writes its .secs file last.
+pending=$TEST_SPLIT
+while [ -n "$pending" ]; do
+ left=""
+ for spec in $pending; do
+ tag=$(split_tag "${spec%=*}")
+ if [ -e "$out/list-$tag.secs" ]; then
+ eval "wait \$list_pid_$tag" || true
+ deal_split "$spec"
+ else
+ left="$left $spec"
+ fi
+ done
+ pending=$left
+ if [ -n "$pending" ]; then
+ sleep 1
+ fi
done
wait
@@ -307,6 +355,15 @@ for name in $names; do
[ "$rc" -eq 0 ] || failed="$failed $name"
done
+# How long each split package's tests took to compile and list, side by
+# side from the start: a shard cannot start before its package's list.
+lists=""
+for spec in $TEST_SPLIT; do
+ tag=$(split_tag "${spec%=*}")
+ lists="$lists $tag $(cat "$out/list-$tag.secs" 2>/dev/null || echo '?') s,"
+done
+echo "=== lists:${lists%,}"
+
# CPU used against wall clock. The shell's `times` covers every job it
# has waited for, and each job waited for its go test and the test
# binaries under it, so this is the whole gate's CPU. Busy cores well
diff --git a/internal/console/testdata/demo_claim_layout.mjs b/internal/console/testdata/demo_claim_layout.mjs
index c742460e0..0d2ae4eeb 100644
--- a/internal/console/testdata/demo_claim_layout.mjs
+++ b/internal/console/testdata/demo_claim_layout.mjs
@@ -77,12 +77,19 @@ export const watchClaimCSP = `
const record = () => {
const stage = claim.dataset.state === 'claimed' ? 4 : Number(progress.dataset.stage);
if (stage === last) return;
- // The server-rendered stage is on screen from the first contentful
- // paint, which a cold or busy browser can reach before this event
- // fires. The page counts its 650 ms from its own paint, so timing the
- // first stage from here would short it (644.9 ms on CI, 2026-11-10).
+ // The first stage is timed from when the document finished parsing
+ // (domInteractive), which is always before the page's script starts
+ // its own 650 ms clock. This event can fire after the page has
+ // already painted and started that clock, and the first-paint entry
+ // may not be posted yet, so timing from here, or from first paint
+ // when it is missing, shorted the stage (644.9 ms and 648.4 ms on CI,
+ // 2026-11-10/11).
let time = performance.now();
- if (last === undefined) time = Math.min(time, performance.getEntriesByName('first-contentful-paint')[0]?.startTime ?? time);
+ if (last === undefined) {
+ const nav = performance.getEntriesByType('navigation')[0];
+ const paint = performance.getEntriesByName('first-contentful-paint')[0];
+ time = Math.min(time, nav?.domInteractive || time, paint?.startTime ?? time);
+ }
window.claimStageTimes.push({stage,time});
last = stage;
};
diff --git a/internal/edge/deadaddr_test.go b/internal/edge/deadaddr_test.go
index ad505358a..6da5525b2 100644
--- a/internal/edge/deadaddr_test.go
+++ b/internal/edge/deadaddr_test.go
@@ -3,6 +3,7 @@
package edge
import (
+ "context"
"errors"
"net"
"os"
@@ -10,6 +11,8 @@ import (
"sync"
"syscall"
"testing"
+
+ "golang.org/x/sys/unix"
)
// deadSockets maps each address deadAddr handed out to the socket holding
@@ -106,6 +109,40 @@ func reviveDeadAddr(addr string) (net.Listener, error) {
// CI gate runs many test processes at once, and a port that was merely
// closed went to another test's server, whose 404 the edge then proxied
// (TestAllUpstreamsDownAnswers503WithRetryAfter, discussion 867).
+func TestHeldTCPPortRestartsWithoutLettingGo(t *testing.T) {
+ addr, listen := heldTCPPort(t)
+ refused := func(when string) {
+ t.Helper()
+ if c, err := net.Dial("tcp", addr); err == nil {
+ c.Close()
+ t.Fatalf("%s: dial %s connected, want refused", when, addr)
+ }
+ if ln, err := net.Listen("tcp", addr); err == nil {
+ ln.Close()
+ t.Fatalf("%s: another listener took %s", when, addr)
+ }
+ }
+ refused("before any listener")
+ for round := 1; round <= 3; round++ {
+ ln, err := listen()
+ if err != nil {
+ t.Fatalf("round %d: listen: %v", round, err)
+ }
+ go func() {
+ if c, err := ln.Accept(); err == nil {
+ c.Close()
+ }
+ }()
+ c, err := net.Dial("tcp", addr)
+ if err != nil {
+ t.Fatalf("round %d: dial while listening: %v", round, err)
+ }
+ c.Close()
+ ln.Close()
+ refused("after round " + strconv.Itoa(round))
+ }
+}
+
func TestDeadAddrStaysDeadForTheTest(t *testing.T) {
addr := deadAddr(t)
if ln, err := net.Listen("tcp", addr); err == nil {
@@ -138,3 +175,48 @@ func TestDeadAddrStaysDeadForTheTest(t *testing.T) {
t.Fatal("revived the same address twice")
}
}
+
+// heldTCPPort is a loopback address whose upstream a test can start, stop
+// and start again, with the port kept from everyone else the whole time.
+// A socket bound with SO_REUSEPORT and never listening holds it; each call
+// to listen binds a listener beside it with SO_REUSEPORT, which only this
+// process can do (the kernel matches the user, and a listener without the
+// option is refused). While no listener is open, connections are refused
+// at once, as for deadAddr. Reserving a port and closing it, as the restart
+// witness did until 2026-11-11, let another test process take it between
+// restarts ("bind: address already in use" on CI).
+func heldTCPPort(t *testing.T) (addr string, listen func() (net.Listener, error)) {
+ t.Helper()
+ syscall.ForkLock.RLock()
+ fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
+ if err == nil {
+ syscall.CloseOnExec(fd)
+ }
+ syscall.ForkLock.RUnlock()
+ if err != nil {
+ t.Fatalf("socket: %v", err)
+ }
+ t.Cleanup(func() { syscall.Close(fd) })
+ if err := syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
+ t.Fatalf("SO_REUSEPORT: %v", err)
+ }
+ if err := syscall.Bind(fd, &syscall.SockaddrInet4{Addr: [4]byte{127, 0, 0, 1}}); err != nil {
+ t.Fatalf("bind 127.0.0.1:0: %v", err)
+ }
+ sa, err := syscall.Getsockname(fd)
+ in4, ok := sa.(*syscall.SockaddrInet4)
+ if err != nil || !ok {
+ t.Fatalf("getsockname: %T, %v; want an IPv4 address", sa, err)
+ }
+ addr = net.JoinHostPort("127.0.0.1", strconv.Itoa(in4.Port))
+ lc := net.ListenConfig{Control: func(_, _ string, c syscall.RawConn) error {
+ var serr error
+ if err := c.Control(func(fd uintptr) {
+ serr = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1)
+ }); err != nil {
+ return err
+ }
+ return serr
+ }}
+ return addr, func() (net.Listener, error) { return lc.Listen(context.Background(), "tcp", addr) }
+}
diff --git a/internal/edge/localhold_witness_test.go b/internal/edge/localhold_witness_test.go
index 3c941e96e..1f069c0c0 100644
--- a/internal/edge/localhold_witness_test.go
+++ b/internal/edge/localhold_witness_test.go
@@ -277,44 +277,14 @@ func witnessEdge(t *testing.T, reg *registry.Registry, hold localHold) *Edge {
return e
}
-// reserveLoopbackAddr picks a free 127.0.0.1 port and releases it, so the
-// route can name an address the upstream binds later.
-func reserveLoopbackAddr(t *testing.T) string {
- t.Helper()
- l, err := net.Listen("tcp", "127.0.0.1:0")
- if err != nil {
- t.Fatalf("reserve port: %v", err)
- }
- addr := l.Addr().String()
- l.Close()
- return addr
-}
-
-// listenTCPFixed binds addr, retrying briefly in case the kernel has not yet
-// released it.
-func listenTCPFixed(addr string) func() (net.Listener, error) {
- return func() (net.Listener, error) {
- var lastErr error
- for i := 0; i < 50; i++ {
- l, err := net.Listen("tcp", addr)
- if err == nil {
- return l, nil
- }
- lastErr = err
- time.Sleep(20 * time.Millisecond)
- }
- return nil, lastErr
- }
-}
-
func witnessTCPEdge(t *testing.T, hold localHold) (*Edge, func() (net.Listener, error)) {
t.Helper()
- addr := reserveLoopbackAddr(t)
+ addr, listen := heldTCPPort(t)
reg := openReg(t)
if err := reg.Upsert(registry.Route{Host: witnessHost, Kind: "service", Addr: addr, Status: "ready"}); err != nil {
t.Fatalf("upsert: %v", err)
}
- return witnessEdge(t, reg, hold), listenTCPFixed(addr)
+ return witnessEdge(t, reg, hold), listen
}
// TestLocalUpstreamRestartWitness restarts a local upstream under load and
@@ -353,7 +323,7 @@ func TestLocalUpstreamRestartWitness(t *testing.T) {
// requests wait for its readmission, and readmission takes up to R.
t.Run("probed", func(t *testing.T) {
t.Parallel()
- addr := reserveLoopbackAddr(t)
+ addr, listen := heldTCPPort(t)
h := provision.Health{Path: "/healthz", IntervalMS: 100, FailAfter: 1, RiseAfter: 2}
reg := openReg(t)
if err := reg.Upsert(registry.Route{Host: witnessHost, Kind: "service", Addrs: addr, Health: healthJSON(t, h), Status: "ready"}); err != nil {
@@ -365,7 +335,7 @@ func TestLocalUpstreamRestartWitness(t *testing.T) {
if got, ok := e.probers.Hold(witnessHost); !ok || got != min(hold.dial+r, hold.cap) {
t.Fatalf("Hold = (%v, %v), want (%v, true)", got, ok, min(hold.dial+r, hold.cap))
}
- st := runRestartWitness(t, e, listenTCPFixed(addr))
+ st := runRestartWitness(t, e, listen)
logWitness(t, "probed", st)
checkWitness(t, st, r)
})