From 58757182c9a98e95e3e20010da540705ce4d42be Mon Sep 17 00:00:00 2001 From: jerryfane Date: Tue, 17 Nov 2026 23:48:09 +0100 Subject: [PATCH] test(engine): wait for the failed attempt's teardown instead of sleeping (#31 review note) --- internal/engine/engine_test.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/internal/engine/engine_test.go b/internal/engine/engine_test.go index 9e3a8a2..aa157df 100644 --- a/internal/engine/engine_test.go +++ b/internal/engine/engine_test.go @@ -748,6 +748,13 @@ func TestCancelReachesARetryStartedDuringTeardown(t *testing.T) { } } } + // Attempt 1's teardown is complete once its slot is given back: both + // attempts held one, so the agent count drops from 2 to 1. + tornDown := func() bool { + h.eng.mu.Lock() + defer h.eng.mu.Unlock() + return h.eng.running[spec.KindAgent] == 1 + } h.add("name: p\nstages:\n - {id: r, agent: omp, prompt: x, output: o.json, max_cost: 1, timeout: 1m, retry: 1}\n", false) id := h.start("p") ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) @@ -759,7 +766,11 @@ func TestCancelReachesARetryStartedDuringTeardown(t *testing.T) { case <-time.After(10 * time.Second): t.Fatal("the retry never started") } - time.Sleep(100 * time.Millisecond) // attempt 1's teardown has now run + for deadline := time.Now().Add(5 * time.Second); !tornDown(); time.Sleep(time.Millisecond) { + if time.Now().After(deadline) { + t.Fatal("attempt 1's teardown did not finish") + } + } if err := h.eng.Cancel(context.Background(), id); err != nil { t.Fatal(err) }