From 0cc20539bc54879af95b643fac803feffdab5a72 Mon Sep 17 00:00:00 2001 From: PJ Date: Fri, 14 Aug 2026 17:50:19 +0530 Subject: [PATCH] test(campaign): wait for the trap instead of racing it The reaping test gave the wedged script one second to install its TERM trap, so a loaded machine signalled it first and the test failed for a reason it does not test. It now waits for the script to say the trap exists, then cancels. Claude-Session: https://claude.ai/code/session_01A5KmftdEJ49A9z5mF5ESrX --- .../campaign/end_to_end_test.go | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/cmd/internal-tools/campaign/end_to_end_test.go b/cmd/internal-tools/campaign/end_to_end_test.go index 317fbab..37fa97c 100644 --- a/cmd/internal-tools/campaign/end_to_end_test.go +++ b/cmd/internal-tools/campaign/end_to_end_test.go @@ -148,19 +148,32 @@ func TestRun_EndToEndAgainstStubBinary(t *testing.T) { func TestExecuteCommand_RunTimeoutSignalsSoTheRunReapsItsChildren(t *testing.T) { directory := t.TempDir() marker := filepath.Join(directory, "child-reaped") + trapped := filepath.Join(directory, "trap-installed") script := filepath.Join(directory, "wedged") body := "#!/bin/sh\n" + "sleep 300 &\n" + "child=$!\n" + "trap 'kill $child; echo reaped > " + marker + "; exit 143' TERM\n" + + "echo installed > " + trapped + "\n" + "wait $child\n" if err := os.WriteFile(script, []byte(body), 0o755); err != nil { t.Fatal(err) } - ctx, cancel := context.WithTimeout(context.Background(), time.Second) + // Cancel only once the script has installed its trap. A fixed deadline + // races the shell under a loaded machine, and a run signalled before its + // trap exists fails this test for a reason it does not test. + ctx, cancel := context.WithCancel(context.Background()) defer cancel() - if _, err := executeCommand(ctx, script, nil, io.Discard); err != nil { + finished := make(chan error, 1) + go func() { + _, err := executeCommand(ctx, script, nil, io.Discard) + finished <- err + }() + waitForFile(t, trapped) + cancel() + + if err := <-finished; err != nil { t.Fatalf("execute: %v", err) } @@ -169,3 +182,15 @@ func TestExecuteCommand_RunTimeoutSignalsSoTheRunReapsItsChildren(t *testing.T) "an unattended sweep leaks one sidecar per wedged run") } } + +func waitForFile(t *testing.T, path string) { + t.Helper() + deadline := time.Now().Add(30 * time.Second) + for time.Now().Before(deadline) { + if _, err := os.Stat(path); err == nil { + return + } + time.Sleep(5 * time.Millisecond) + } + t.Fatalf("%s never appeared", path) +}