oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package thin
2
3import (
4 "context"
5 "encoding/json"
6 "io"
7 "log/slog"
8 "sync"
9 "testing"
10 "time"
11
12 "amadan.net/oficina/equipo/internal/wire"
13)
14
15// Whole-branch fix 3, #3: below 37 the sender classifies a cancellation
16// report (not yet dispatched: unclassified, so held), and before it
17// notes the hold the Checks dispatch is accepted. The dispatch saw no
18// hold yet; the report must still go at once, not at the next dispatch
19// or reconnect. The seam pauses the sender between classifying and
20// noting the hold.
21func TestWorkerHeldReportRaceWithDispatch(t *testing.T) {
22 inst := newFakeInstance(t)
23 ctx, cancel := context.WithCancel(context.Background())
24 t.Cleanup(cancel)
25 st, err := OpenState(ctx, t.TempDir())
26 if err != nil {
27 t.Fatal(err)
28 }
29 t.Cleanup(func() { st.Close() })
30 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 {
31 return wire.Hello{Repos: []string{}, CreateIn: []string{}, Catalog: []wire.RepoInfo{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test", "claude": "2.1.312"}}
32 }}
33 paused, release := make(chan struct{}), make(chan struct{})
34 var once sync.Once
35 var released sync.Once
36 t.Cleanup(func() { released.Do(func() { close(release) }) })
37 link.heldHook = func() {
38 once.Do(func() {
39 close(paused)
40 <-release
41 })
42 }
43 steps := &StepEngine{State: st, Notify: link.Notify, Exec: func(_ context.Context, r stepRow, _ func(wire.StepReport)) wire.StepReport {
44 return wire.StepReport{TaskID: r.TaskID, StepID: r.StepID, Attempt: r.Attempt, State: wire.StepDone, Termination: true, Findings: []wire.Finding{}}
45 }}
46 link.Steps = steps
47 if err := steps.Start(ctx); err != nil {
48 t.Fatal(err)
49 }
50 done := make(chan error, 1)
51 go func() { done <- link.Run(ctx) }()
52 t.Cleanup(func() { cancel(); <-done; steps.Wait() })
53 c := <-inst.conns
54 read(t, c, wire.FrameHello)
55 send := func(id, kind string, body any) {
56 t.Helper()
57 f, err := wire.NewFrame(id, kind, body)
58 if err != nil {
59 t.Fatal(err)
60 }
61 raw, _ := json.Marshal(f)
62 write(t, c, raw)
63 }
64 send("i-welcome", wire.FrameWelcome, wire.Welcome{Generation: 1, RunnerID: "RUNNER2", Protocol: wire.Protocol35, Role: wire.RoleWorker})
65 reports := make(chan wire.StepReport, 8)
66 go func() {
67 for {
68 _, raw, err := c.Read(context.Background())
69 if err != nil {
70 return
71 }
72 if f, body, err := wire.ParseFrame(raw); err == nil && f.Type == wire.FrameStep {
73 reports <- *body.(*wire.StepReport)
74 }
75 }
76 }()
77 send("i-c-r", wire.FrameCancelStep, wire.CancelStepCommand{Person: "p@example.com", TaskID: "tkr", StepID: "sr", Attempt: 1})
78 select {
79 case <-paused:
80 case <-time.After(10 * time.Second):
81 t.Fatal("the cancellation report was never held")
82 }
83 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{}})
84 // The dispatch is applied (its classification and its look at the
85 // hold) while the sender is still paused.
86 for deadline := time.Now().Add(5 * time.Second); ; time.Sleep(20 * time.Millisecond) {
87 var n int
88 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 {
89 break
90 }
91 if time.Now().After(deadline) {
92 t.Fatal("the dispatch was not applied")
93 }
94 }
95 time.Sleep(200 * time.Millisecond)
96 released.Do(func() { close(release) })
97 select {
98 case r := <-reports:
99 if r.StepID != "sr" || r.State != wire.StepCancelled || !r.Termination {
100 t.Fatalf("report %+v", r)
101 }
102 case <-time.After(5 * time.Second):
103 t.Fatal("the cancellation's report was lost until the next dispatch or reconnect")
104 }
105}
106