| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/wire" |
| 8 | ) |
| 9 | |
| 10 | // Whole-branch fix 2, #3: a worker's commit that arrives while there is |
| 11 | // no primary waits, visibly (waiting_runner), for one; when a primary |
| 12 | // connects it is asked for, from the step's durable record, and |
| 13 | // published. |
| 14 | func TestContractCommitBetweenPrimaries(t *testing.T) { |
| 15 | r := newMacRig(t) |
| 16 | w := r.newMacEngineer("Mac A", wire.EngineeringReady) |
| 17 | base, cand := sha(1), sha(0xc3) |
| 18 | task, d := r.macDispatch(w, "iOS between primaries", base) |
| 19 | if !w.lease(d.StepID, d.Attempt) { |
| 20 | t.Fatal("start refused") |
| 21 | } |
| 22 | r.remove(r.primary.fr.Welcome.RunnerID, false) |
| 23 | w.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, CandidateSHA: cand, Phase: wire.OutcomeCommitted, OutcomePhase: wire.OutcomeCommitted, Findings: []wire.Finding{}}) |
| 24 | w.send(wire.FrameStep, workerDone(d, cand, wire.OutcomeCommitted)) |
| 25 | w.none(wire.FrameSendPack, 50*time.Millisecond) |
| 26 | if s := r.engineeringStep(task); s.State != wire.StepDone || s.Pushed || s.Placement != wire.PlacementWaiting { |
| 27 | t.Fatalf("with no primary %+v", s) |
| 28 | } |
| 29 | token := r.inst.EnrolRunner(t).Token |
| 30 | h := primaryHello(0) |
| 31 | h.Protocol, h.Label = wire.ProtocolMacEngineering, "docs-t2" |
| 32 | h.Catalog[0].NeedsMacOS = true |
| 33 | np, err := DialRunnerHello(r.ctx, r.inst.URL, token, h) |
| 34 | if err != nil || np.Welcome.Role != wire.RolePrimary { |
| 35 | t.Fatalf("replacement %+v %v", np, err) |
| 36 | } |
| 37 | t.Cleanup(np.Close) |
| 38 | b := &peer{t: t, r: r.toolRig, fr: np} |
| 39 | _, body := w.expect(wire.FrameSendPack) |
| 40 | tr := body.(*wire.SendPackCommand).Transfer |
| 41 | if tr.Direction != wire.TransferOutbound || tr.CandidateSHA != cand || tr.BaseSHA != base || tr.Attempt != d.Attempt { |
| 42 | t.Fatalf("outbound %+v", tr) |
| 43 | } |
| 44 | if to, _ := r.transferTo(tr.ID); to != np.Welcome.RunnerID { |
| 45 | t.Fatalf("the transfer goes to %s", to) |
| 46 | } |
| 47 | w.send(wire.FrameBlob, chunk(tr, []byte("PACK commit"))) |
| 48 | b.expect(wire.FrameBlob) |
| 49 | _, body = b.expect(wire.FramePublish) |
| 50 | pub := body.(*wire.PublishCommand) |
| 51 | if !b.leasePublication(d.StepID, d.Attempt, pub.Publication) { |
| 52 | t.Fatal("B's publication refused") |
| 53 | } |
| 54 | b.send(wire.FramePublished, publishedWith(pub, base, swipeDiff)) |
| 55 | if s := r.engineeringStep(task); !s.Pushed || s.Publication != wire.PublicationPublished || s.Placement != "" { |
| 56 | t.Fatalf("after B published %+v", s) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // Whole-branch fix 2, #4: a failed publication can be retried after the |
| 61 | // daily sweep has pruned its transfer: Retry asks the worker again, from |
| 62 | // the step's record, and the primary publishes. |
| 63 | func TestContractPublicationRetryAfterPrune(t *testing.T) { |
| 64 | r := newMacRig(t) |
| 65 | w := r.newMacEngineer("Mac A", wire.EngineeringReady) |
| 66 | base, cand := sha(1), sha(0xc4) |
| 67 | task, d := r.macDispatch(w, "iOS retry after prune", base) |
| 68 | pub := r.committedAs(w, d, cand, wire.OutcomeCommitted, true) |
| 69 | if !r.primary.leasePublication(d.StepID, d.Attempt, pub.Publication) { |
| 70 | t.Fatal("publication refused") |
| 71 | } |
| 72 | r.primary.send(wire.FramePublished, wire.PublishedReport{Publication: pub.Publication, TaskID: task, StepID: d.StepID, Attempt: d.Attempt, CandidateSHA: cand, Reason: "push_failed"}) |
| 73 | if s := r.engineeringStep(task); s.Publication != wire.PublicationFailed { |
| 74 | t.Fatalf("push failed %+v", s) |
| 75 | } |
| 76 | // The next day's sweep. |
| 77 | chat := r.inst.Store.Chat() |
| 78 | chat.SetClock(func() time.Time { return time.Now().UTC().Add(25 * time.Hour) }) |
| 79 | if err := chat.Sweep(r.ctx); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | chat.SetClock(func() time.Time { return time.Now().UTC() }) |
| 83 | var n int |
| 84 | if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_transfers WHERE step_id = ?", d.StepID).Scan(&n); err != nil || n != 0 { |
| 85 | t.Fatalf("%d transfers left %v", n, err) |
| 86 | } |
| 87 | if _, err := r.client.RetryTask(r.ctx, task); err != nil { |
| 88 | t.Fatalf("Retry after the prune: %v", err) |
| 89 | } |
| 90 | _, body := w.expect(wire.FrameSendPack) |
| 91 | tr := body.(*wire.SendPackCommand).Transfer |
| 92 | if tr.Direction != wire.TransferOutbound || tr.CandidateSHA != cand || tr.BaseSHA != base || tr.Attempt != d.Attempt { |
| 93 | t.Fatalf("outbound %+v", tr) |
| 94 | } |
| 95 | w.send(wire.FrameBlob, chunk(tr, []byte("PACK commit"))) |
| 96 | r.fromPrimary(wire.FrameBlob) |
| 97 | again := r.fromPrimary(wire.FramePublish).(*wire.PublishCommand) |
| 98 | if again.Publication == pub.Publication || again.Transfer != tr.ID { |
| 99 | t.Fatalf("republish %+v", again) |
| 100 | } |
| 101 | r.primary.send(wire.FramePublished, publishedWith(again, base, swipeDiff)) |
| 102 | if s := r.engineeringStep(task); !s.Pushed || s.Publication != wire.PublicationPublished { |
| 103 | t.Fatalf("after Retry %+v", s) |
| 104 | } |
| 105 | } |
| 106 | |