| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | "log/slog" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // A worker's step reports below 37 (a rollback to an instance that |
| 15 | // places no Engineering on a Mac): only a report positively of a step |
| 16 | // this worker was dispatched that is not Engineering goes; any other is |
| 17 | // kept for an instance that speaks 37. A report is classified by its own |
| 18 | // task and step, whatever attempt the step has moved on to (a failover |
| 19 | // advances it while earlier reports wait), and a classification that |
| 20 | // fails keeps the frame. |
| 21 | func TestWorkerReportsBelow37(t *testing.T) { |
| 22 | ctx := context.Background() |
| 23 | st, err := OpenState(ctx, t.TempDir()) |
| 24 | if err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | dispatch := func(id, specialist string) stepRow { |
| 28 | t.Helper() |
| 29 | d := wire.DispatchCommand{Person: "p@example.com", TaskID: "tk" + id, StepID: "s" + id, Attempt: 1, Specialist: specialist, Repo: "amadan.net/woodstar/panes", Branch: "equipo/tk" + id + "-panes", BaseSHA: "1111111111111111111111111111111111111111", Findings: []wire.Finding{}} |
| 30 | if err := st.AcceptDispatch(ctx, "d-"+id, d); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | return stepRow{DispatchCommand: d} |
| 34 | } |
| 35 | running := func(r stepRow) wire.StepReport { |
| 36 | return wire.StepReport{TaskID: r.TaskID, StepID: r.StepID, Attempt: r.Attempt, State: wire.StepRunning, Findings: []wire.Finding{}} |
| 37 | } |
| 38 | eng, chk := dispatch("e", wire.SpecialistEngineering), dispatch("c", wire.SpecialistChecks) |
| 39 | for _, r := range []stepRow{eng, chk} { |
| 40 | if err := st.ReportStep(ctx, r, running(r)); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | } |
| 44 | // A failover: the Engineering step moves to attempt 2 while its |
| 45 | // attempt 1 report still waits. |
| 46 | if err := st.advanceStep(ctx, &eng, nil, running(eng)); err != nil || eng.Attempt != 2 { |
| 47 | t.Fatalf("advance %v (attempt %d)", err, eng.Attempt) |
| 48 | } |
| 49 | frames, _, err := st.Unacked(ctx, 0) |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | reports := map[string][]wire.Frame{} |
| 54 | for _, f := range frames { |
| 55 | if f.Type != wire.FrameStep { |
| 56 | continue |
| 57 | } |
| 58 | body, _ := f.Decode() |
| 59 | rep := body.(*wire.StepReport) |
| 60 | reports[rep.StepID] = append(reports[rep.StepID], f) |
| 61 | } |
| 62 | if len(reports[eng.StepID]) != 2 || len(reports[chk.StepID]) != 1 { |
| 63 | t.Fatalf("queued %v", reports) |
| 64 | } |
| 65 | for _, f := range reports[eng.StepID] { |
| 66 | if !heldBelow37(st.holdBelow37(ctx, f, true, wire.Protocol35)) { |
| 67 | t.Fatalf("an Engineering report %s was sent at 35", f.Data) |
| 68 | } |
| 69 | if heldBelow37(st.holdBelow37(ctx, f, true, wire.ProtocolMacEngineering)) || heldBelow37(st.holdBelow37(ctx, f, false, wire.Protocol35)) { |
| 70 | t.Fatalf("held at 37, or on a primary: %s", f.Data) |
| 71 | } |
| 72 | } |
| 73 | check := reports[chk.StepID][0] |
| 74 | if heldBelow37(st.holdBelow37(ctx, check, true, wire.Protocol35)) { |
| 75 | t.Fatal("a checks report was held at 35") |
| 76 | } |
| 77 | // Uncertain is kept: a report naming nothing dispatched here, a |
| 78 | // dispatch that cannot be read, a database that cannot be read. |
| 79 | stray, err := wire.NewFrame("stray", wire.FrameStep, wire.StepReport{TaskID: "tkz", StepID: "sz", Attempt: 1, State: wire.StepRunning, Findings: []wire.Finding{}}) |
| 80 | if err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | if !heldBelow37(st.holdBelow37(ctx, stray, true, wire.Protocol35)) { |
| 84 | t.Fatal("a report of no step here was sent at 35") |
| 85 | } |
| 86 | if _, err := st.db.Exec("UPDATE steps SET body='{' WHERE step_id=?", chk.StepID); err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | if !heldBelow37(st.holdBelow37(ctx, check, true, wire.Protocol35)) { |
| 90 | t.Fatal("a report whose dispatch cannot be read was sent at 35") |
| 91 | } |
| 92 | st.Close() |
| 93 | if !heldBelow37(st.holdBelow37(ctx, reports[eng.StepID][0], true, wire.Protocol35)) { |
| 94 | t.Fatal("a report was sent at 35 when the state could not be read") |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func heldBelow37(hold bool, _ error) bool { return hold } |
| 99 | |
| 100 | // A protocol 35 worker (a rollback) told to cancel a step before the |
| 101 | // step's dispatch arrives: the cancellation's record names no specialist |
| 102 | // yet, so its report waits; the dispatch that follows says Checks, the |
| 103 | // record takes that (still cancelled, never run), and the waiting report |
| 104 | // goes on the same connection. Cancelled before an Engineering dispatch, |
| 105 | // the report still waits for 37. |
| 106 | func TestWorkerCancelBeforeDispatchBelow37(t *testing.T) { |
| 107 | inst := newFakeInstance(t) |
| 108 | ctx, cancel := context.WithCancel(context.Background()) |
| 109 | t.Cleanup(cancel) |
| 110 | st, err := OpenState(ctx, t.TempDir()) |
| 111 | if err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | t.Cleanup(func() { st.Close() }) |
| 115 | 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 { |
| 116 | return wire.Hello{Repos: []string{}, CreateIn: []string{}, Catalog: []wire.RepoInfo{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test", "claude": "2.1.312"}} |
| 117 | }} |
| 118 | ran := make(chan string, 4) |
| 119 | steps := &StepEngine{State: st, Notify: link.Notify, Exec: func(_ context.Context, r stepRow, _ func(wire.StepReport)) wire.StepReport { |
| 120 | ran <- r.StepID |
| 121 | return wire.StepReport{TaskID: r.TaskID, StepID: r.StepID, Attempt: r.Attempt, State: wire.StepDone, Termination: true, Findings: []wire.Finding{}} |
| 122 | }} |
| 123 | link.Steps = steps |
| 124 | if err := steps.Start(ctx); err != nil { |
| 125 | t.Fatal(err) |
| 126 | } |
| 127 | done := make(chan error, 1) |
| 128 | go func() { done <- link.Run(ctx) }() |
| 129 | t.Cleanup(func() { cancel(); <-done; steps.Wait() }) |
| 130 | c := <-inst.conns |
| 131 | read(t, c, wire.FrameHello) |
| 132 | send := func(id, kind string, body any) { |
| 133 | t.Helper() |
| 134 | f, err := wire.NewFrame(id, kind, body) |
| 135 | if err != nil { |
| 136 | t.Fatal(err) |
| 137 | } |
| 138 | raw, _ := json.Marshal(f) |
| 139 | write(t, c, raw) |
| 140 | } |
| 141 | send("i-welcome", wire.FrameWelcome, wire.Welcome{Generation: 1, RunnerID: "RUNNER2", Protocol: wire.Protocol35, Role: wire.RoleWorker}) |
| 142 | reports := make(chan wire.StepReport, 8) |
| 143 | go func() { |
| 144 | for { |
| 145 | _, raw, err := c.Read(context.Background()) |
| 146 | if err != nil { |
| 147 | return |
| 148 | } |
| 149 | if f, body, err := wire.ParseFrame(raw); err == nil && f.Type == wire.FrameStep { |
| 150 | reports <- *body.(*wire.StepReport) |
| 151 | } |
| 152 | } |
| 153 | }() |
| 154 | // none: no report of step (any, for "") goes within a moment. A |
| 155 | // report already sent and not acked may go again (the fake instance |
| 156 | // never acks), as after a reconnect. |
| 157 | none := func(what, step string) { |
| 158 | t.Helper() |
| 159 | deadline := time.After(1500 * time.Millisecond) |
| 160 | for { |
| 161 | select { |
| 162 | case r := <-reports: |
| 163 | if step == "" || r.StepID == step { |
| 164 | t.Fatalf("%s: sent at 35 %+v", what, r) |
| 165 | } |
| 166 | case <-deadline: |
| 167 | return |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | dispatch := func(id, specialist string) { |
| 172 | send("i-d-"+id, wire.FrameDispatch, wire.DispatchCommand{Person: "p@example.com", TaskID: "tk" + id, StepID: "s" + id, Attempt: 1, Specialist: specialist, Repo: "amadan.net/woodstar/panes", Branch: "equipo/tk" + id + "-panes", BaseSHA: "1111111111111111111111111111111111111111", Findings: []wire.Finding{}}) |
| 173 | } |
| 174 | for _, id := range []string{"c", "e"} { |
| 175 | send("i-c-"+id, wire.FrameCancelStep, wire.CancelStepCommand{Person: "p@example.com", TaskID: "tk" + id, StepID: "s" + id, Attempt: 1}) |
| 176 | } |
| 177 | none("a cancellation of a step not yet dispatched", "") |
| 178 | dispatch("c", wire.SpecialistChecks) |
| 179 | select { |
| 180 | case r := <-reports: |
| 181 | if r.StepID != "sc" || r.State != wire.StepCancelled || !r.Termination { |
| 182 | t.Fatalf("the checks step's report %+v", r) |
| 183 | } |
| 184 | case <-time.After(10 * time.Second): |
| 185 | t.Fatal("the cancelled checks step's report never went at 35") |
| 186 | } |
| 187 | dispatch("e", wire.SpecialistEngineering) |
| 188 | none("the cancelled Engineering step's report", "se") |
| 189 | select { |
| 190 | case id := <-ran: |
| 191 | t.Fatalf("a cancelled step ran: %s", id) |
| 192 | default: |
| 193 | } |
| 194 | } |
| 195 | |