package thin import ( "context" "encoding/json" "io" "log/slog" "sync" "testing" "time" "amadan.net/oficina/equipo/internal/wire" ) // Whole-branch fix 3, #3: below 37 the sender classifies a cancellation // report (not yet dispatched: unclassified, so held), and before it // notes the hold the Checks dispatch is accepted. The dispatch saw no // hold yet; the report must still go at once, not at the next dispatch // or reconnect. The seam pauses the sender between classifying and // noting the hold. func TestWorkerHeldReportRaceWithDispatch(t *testing.T) { inst := newFakeInstance(t) ctx, cancel := context.WithCancel(context.Background()) t.Cleanup(cancel) st, err := OpenState(ctx, t.TempDir()) if err != nil { t.Fatal(err) } t.Cleanup(func() { st.Close() }) link := &Link{Logger: slog.New(slog.NewTextHandler(io.Discard, nil)), Origin: inst.url, Token: "token", State: st, Role: wire.RoleWorker, WorkerOnly: true, Hello: func() wire.Hello { return wire.Hello{Repos: []string{}, CreateIn: []string{}, Catalog: []wire.RepoInfo{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test", "claude": "2.1.312"}} }} paused, release := make(chan struct{}), make(chan struct{}) var once sync.Once var released sync.Once t.Cleanup(func() { released.Do(func() { close(release) }) }) link.heldHook = func() { once.Do(func() { close(paused) <-release }) } steps := &StepEngine{State: st, Notify: link.Notify, Exec: func(_ context.Context, r stepRow, _ func(wire.StepReport)) wire.StepReport { return wire.StepReport{TaskID: r.TaskID, StepID: r.StepID, Attempt: r.Attempt, State: wire.StepDone, Termination: true, Findings: []wire.Finding{}} }} link.Steps = steps if err := steps.Start(ctx); err != nil { t.Fatal(err) } done := make(chan error, 1) go func() { done <- link.Run(ctx) }() t.Cleanup(func() { cancel(); <-done; steps.Wait() }) c := <-inst.conns read(t, c, wire.FrameHello) send := func(id, kind string, body any) { t.Helper() f, err := wire.NewFrame(id, kind, body) if err != nil { t.Fatal(err) } raw, _ := json.Marshal(f) write(t, c, raw) } send("i-welcome", wire.FrameWelcome, wire.Welcome{Generation: 1, RunnerID: "RUNNER2", Protocol: wire.Protocol35, Role: wire.RoleWorker}) reports := make(chan wire.StepReport, 8) go func() { for { _, raw, err := c.Read(context.Background()) if err != nil { return } if f, body, err := wire.ParseFrame(raw); err == nil && f.Type == wire.FrameStep { reports <- *body.(*wire.StepReport) } } }() send("i-c-r", wire.FrameCancelStep, wire.CancelStepCommand{Person: "p@example.com", TaskID: "tkr", StepID: "sr", Attempt: 1}) select { case <-paused: case <-time.After(10 * time.Second): t.Fatal("the cancellation report was never held") } send("i-d-r", wire.FrameDispatch, wire.DispatchCommand{Person: "p@example.com", TaskID: "tkr", StepID: "sr", Attempt: 1, Specialist: wire.SpecialistChecks, Repo: "amadan.net/woodstar/panes", Branch: "equipo/tkr-panes", BaseSHA: "1111111111111111111111111111111111111111", Findings: []wire.Finding{}}) // The dispatch is applied (its classification and its look at the // hold) while the sender is still paused. for deadline := time.Now().Add(5 * time.Second); ; time.Sleep(20 * time.Millisecond) { var n int if err := st.db.QueryRowContext(ctx, "SELECT count(*) FROM steps WHERE step_id = 'sr' AND json_extract(body, '$.specialist') = ?", wire.SpecialistChecks).Scan(&n); err == nil && n == 1 { break } if time.Now().After(deadline) { t.Fatal("the dispatch was not applied") } } time.Sleep(200 * time.Millisecond) released.Do(func() { close(release) }) select { case r := <-reports: if r.StepID != "sr" || r.State != wire.StepCancelled || !r.Termination { t.Fatalf("report %+v", r) } case <-time.After(5 * time.Second): t.Fatal("the cancellation's report was lost until the next dispatch or reconnect") } }