package thintest import ( "testing" "time" "amadan.net/oficina/equipo/internal/wire" ) // Whole-branch fix 2, #3: a worker's commit that arrives while there is // no primary waits, visibly (waiting_runner), for one; when a primary // connects it is asked for, from the step's durable record, and // published. func TestContractCommitBetweenPrimaries(t *testing.T) { r := newMacRig(t) w := r.newMacEngineer("Mac A", wire.EngineeringReady) base, cand := sha(1), sha(0xc3) task, d := r.macDispatch(w, "iOS between primaries", base) if !w.lease(d.StepID, d.Attempt) { t.Fatal("start refused") } r.remove(r.primary.fr.Welcome.RunnerID, false) 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{}}) w.send(wire.FrameStep, workerDone(d, cand, wire.OutcomeCommitted)) w.none(wire.FrameSendPack, 50*time.Millisecond) if s := r.engineeringStep(task); s.State != wire.StepDone || s.Pushed || s.Placement != wire.PlacementWaiting { t.Fatalf("with no primary %+v", s) } token := r.inst.EnrolRunner(t).Token h := primaryHello(0) h.Protocol, h.Label = wire.ProtocolMacEngineering, "docs-t2" h.Catalog[0].NeedsMacOS = true np, err := DialRunnerHello(r.ctx, r.inst.URL, token, h) if err != nil || np.Welcome.Role != wire.RolePrimary { t.Fatalf("replacement %+v %v", np, err) } t.Cleanup(np.Close) b := &peer{t: t, r: r.toolRig, fr: np} _, body := w.expect(wire.FrameSendPack) tr := body.(*wire.SendPackCommand).Transfer if tr.Direction != wire.TransferOutbound || tr.CandidateSHA != cand || tr.BaseSHA != base || tr.Attempt != d.Attempt { t.Fatalf("outbound %+v", tr) } if to, _ := r.transferTo(tr.ID); to != np.Welcome.RunnerID { t.Fatalf("the transfer goes to %s", to) } w.send(wire.FrameBlob, chunk(tr, []byte("PACK commit"))) b.expect(wire.FrameBlob) _, body = b.expect(wire.FramePublish) pub := body.(*wire.PublishCommand) if !b.leasePublication(d.StepID, d.Attempt, pub.Publication) { t.Fatal("B's publication refused") } b.send(wire.FramePublished, publishedWith(pub, base, swipeDiff)) if s := r.engineeringStep(task); !s.Pushed || s.Publication != wire.PublicationPublished || s.Placement != "" { t.Fatalf("after B published %+v", s) } } // Whole-branch fix 2, #4: a failed publication can be retried after the // daily sweep has pruned its transfer: Retry asks the worker again, from // the step's record, and the primary publishes. func TestContractPublicationRetryAfterPrune(t *testing.T) { r := newMacRig(t) w := r.newMacEngineer("Mac A", wire.EngineeringReady) base, cand := sha(1), sha(0xc4) task, d := r.macDispatch(w, "iOS retry after prune", base) pub := r.committedAs(w, d, cand, wire.OutcomeCommitted, true) if !r.primary.leasePublication(d.StepID, d.Attempt, pub.Publication) { t.Fatal("publication refused") } r.primary.send(wire.FramePublished, wire.PublishedReport{Publication: pub.Publication, TaskID: task, StepID: d.StepID, Attempt: d.Attempt, CandidateSHA: cand, Reason: "push_failed"}) if s := r.engineeringStep(task); s.Publication != wire.PublicationFailed { t.Fatalf("push failed %+v", s) } // The next day's sweep. chat := r.inst.Store.Chat() chat.SetClock(func() time.Time { return time.Now().UTC().Add(25 * time.Hour) }) if err := chat.Sweep(r.ctx); err != nil { t.Fatal(err) } chat.SetClock(func() time.Time { return time.Now().UTC() }) var n int if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_transfers WHERE step_id = ?", d.StepID).Scan(&n); err != nil || n != 0 { t.Fatalf("%d transfers left %v", n, err) } if _, err := r.client.RetryTask(r.ctx, task); err != nil { t.Fatalf("Retry after the prune: %v", err) } _, body := w.expect(wire.FrameSendPack) tr := body.(*wire.SendPackCommand).Transfer if tr.Direction != wire.TransferOutbound || tr.CandidateSHA != cand || tr.BaseSHA != base || tr.Attempt != d.Attempt { t.Fatalf("outbound %+v", tr) } w.send(wire.FrameBlob, chunk(tr, []byte("PACK commit"))) r.fromPrimary(wire.FrameBlob) again := r.fromPrimary(wire.FramePublish).(*wire.PublishCommand) if again.Publication == pub.Publication || again.Transfer != tr.ID { t.Fatalf("republish %+v", again) } r.primary.send(wire.FramePublished, publishedWith(again, base, swipeDiff)) if s := r.engineeringStep(task); !s.Pushed || s.Publication != wire.PublicationPublished { t.Fatalf("after Retry %+v", s) } }