Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/queue_test.go
T
jschoubben 0548dc8f80 R13: a mover whose dependency was superseded out says so in its build's why
A waiting pipeline keeps in superseded (C-R5) the modules a newer waiting pipeline of its branch
took out of it, written with its movers by compare-and-set; at its placing each mover building on
one of them (an edge of a kind its build waits on) has its build step's why say that dependency was
taken out and that it builds against the newest published package.

Builds: design 47 H §748; lead answers H-A-53, H-A-107

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
2026-11-20 04:56:31 +01:00

301 lines
13 KiB
Go

package pipeline
import (
"slices"
"strings"
"testing"
"time"
catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
)
// memQueue is the state queue in memory, written by compare-and-set as the bus's is.
type memQueue map[string]struct {
v []byte
rev uint64
}
func (m memQueue) Get(key string) ([]byte, uint64, error) {
e, ok := m[key]
if !ok {
return nil, 0, ErrNoKey
}
return e.v, e.rev, nil
}
func (m memQueue) Create(key string, v []byte) (uint64, error) {
if _, ok := m[key]; ok {
return 0, ErrExists
}
m[key] = struct {
v []byte
rev uint64
}{v, 1}
return 1, nil
}
func (m memQueue) Update(key string, v []byte, rev uint64) (uint64, error) {
if e, ok := m[key]; !ok || e.rev != rev {
return 0, ErrChanged
}
m[key] = struct {
v []byte
rev uint64
}{v, rev + 1}
return rev + 1, nil
}
func (m memQueue) Delete(key string) error {
if _, ok := m[key]; !ok {
return ErrNoKey
}
delete(m, key)
return nil
}
func (m memQueue) Keys(prefix string) ([]string, error) {
var out []string
for k := range m {
if strings.HasPrefix(k, prefix) {
out = append(out, k)
}
}
slices.Sort(out)
return out, nil
}
// queueFixture is a starter of pipelines-at-once 1 over a state pipelines and a state queue in memory, a hearer whose ends
// take their turn, and the verbs over both; said and waits record what they say.
func queueFixture() (*Starter, memStore, memQueue, *[]string, *[]seat.Wait, *stepAsks, func(string) (Half, bool), Verbs) {
reg, entries := placeFixture()
for _, m := range []string{"lib", "tool"} {
entries[m] = catalog.CatalogEntry{Module: m, Commit: "c1", Fp: "src2:" + m, Artifacts: entries["app"].Artifacts}
}
mem, q := memStore{}, memQueue{}
asks := &stepAsks{}
said, waits := &[]string{}, &[]seat.Wait{}
say := func(subject string, body any) error {
*said = append(*said, subject[strings.Index(subject, ".event.")+7:])
if w, ok := body.(seat.Wait); ok {
*waits = append(*waits, w)
}
return nil
}
now := func() time.Time { return time.Date(2026, 11, 20, 4, 0, 0, 0, time.UTC) }
s := &Starter{Pipelines: memCreated{mem}, Store: mem, Say: say, Steps: asks, Name: "the-runner", Now: now,
Registration: func(string) (catalog.Registration, bool) { return reg, true },
Queue: &Queue{Store: q, AtOnce: 1}}
view := func(string) (Half, bool) { return Half{Revision: 7, Entries: entries}, true }
h := &Hearer{Store: mem, Name: "the-runner", Say: say, Now: now, Turn: func(key string) { _, _ = s.Turn(key, view) }}
s.Ended = func(p seat.Pipeline, steps []int) error { return h.EndedAtPlacing(p, steps) }
return s, mem, q, said, waits, asks, view, Verbs{Hearer: h, Starter: s, View: view, Acts: &actLog{}}
}
// takeIn hears a take-in.taken of main at source, its entries the modules named.
func takeIn(t *testing.T, s *Starter, view func(string) (Half, bool)) func(source int64, modules ...string) {
return func(source int64, modules ...string) {
t.Helper()
ev := catalog.TakeInTaken{Branch: "main", Source: source, Call: "c0ffee", Revision: 7}
for _, m := range modules {
ev.Entries = append(ev.Entries, catalog.TakeInEntry{Module: m, Commit: "c1", Fingerprint: "src2:" + m})
}
if n, err := s.TakenIn(ev, view); n != 1 || err != nil {
t.Fatalf("the take-in of source %d started %d (%v)", source, n, err)
}
}
}
// R11/AC1, R11/AC2 (design 47 H §748, H-A-14, H-A-36, H-A-52, H-A-72, H-A-96): with pipelines-at-once 1, a second and a
// third take-in are written waiting under the next sequences of queue.next, with no step asked, each saying pipeline-started
// and wait-held step pipeline on pipelines-at-once; the first's end, here its cancel, deletes its running key and places
// the lowest sequence, which says wait-ended and has its steps asked; at the runner's start a running key whose pipeline
// runs no more is deleted, a waiting key whose pipeline is gone dropped, and the next waiting placed.
func TestThePipelinesWaitTheirTurn(t *testing.T) {
s, mem, q, said, waits, asks, view, v := queueFixture()
take := takeIn(t, s, view)
take(4, "app")
first := len(asks.ids)
if p, _, _ := mem.Get("main.4"); p.Outcome != seat.PipelineOutcomeRunning || p.Seq != 0 || first == 0 {
t.Fatalf("the first pipeline = %s seq %d with %d asks, want running and asked", p.Outcome, p.Seq, first)
}
if _, ok := q["running.main.4"]; !ok {
t.Fatalf("the queue = %v, want running.main.4", q)
}
*said, *waits = nil, nil
take(5, "lib")
take(6, "tool")
for i, key := range []string{"main.5", "main.6"} {
p, _, _ := mem.Get(key)
if p.Outcome != seat.PipelineOutcomeWaiting || p.Seq != int64(i+1) || len(p.Steps) != 0 {
t.Errorf("%s = %s seq %d with %d steps, want waiting at %d with none", key, p.Outcome, p.Seq, len(p.Steps), i+1)
}
}
if len(asks.ids) != first {
t.Errorf("a waiting pipeline asked %d steps", len(asks.ids)-first)
}
if _, ok := q["waiting.00000000000000000001"]; !ok || string(q["queue.next"].v) != "3" {
t.Errorf("the queue = %v, want waiting.<1 in 20 digits> and queue.next 3", q)
}
if !slices.Contains(*said, "pipeline-started.main.5") || !slices.Contains(*said, "wait-held.main.5") || len(*waits) != 2 ||
(*waits)[0].Step != seat.WaitStepPipeline || (*waits)[0].On != seat.WaitOnPipelinesAtOnce || (*waits)[0].Pipeline != "main.5" {
t.Errorf("said %v, waits %+v; want pipeline-started and wait-held step pipeline on pipelines-at-once", *said, *waits)
}
// R11/AC2: a cancel places the next, the lowest sequence.
*said, *waits = nil, nil
if _, err := v.Cancel("operator", seat.CancelInput{Pipeline: "main.4", Why: "wrong"}); err != nil {
t.Fatal(err)
}
if p, _, _ := mem.Get("main.5"); p.Outcome != seat.PipelineOutcomeRunning || len(p.Steps) == 0 || len(asks.ids) == first {
t.Errorf("after the cancel main.5 = %s with %d steps, want placed and asked", p.Outcome, len(p.Steps))
}
if p, _, _ := mem.Get("main.6"); p.Outcome != seat.PipelineOutcomeWaiting {
t.Errorf("main.6 = %s, want still waiting", p.Outcome)
}
_, gone := q["running.main.4"]
_, runs := q["running.main.5"]
_, waits5 := q["waiting.00000000000000000001"]
if gone || !runs || waits5 {
t.Errorf("the queue after the cancel = %v", q)
}
if !slices.Contains(*said, "wait-ended.main.5") || len(*waits) != 1 || (*waits)[0].How != "ran" {
t.Errorf("said %v, waits %+v; want wait-ended of main.5", *said, *waits)
}
// At the start: main.5's end was written while its key stayed, and a waiting key names a pipeline never written.
p5, rev, _ := mem.Get("main.5")
p5.Outcome = seat.PipelineOutcomePassed
mem.Update("main.5", p5, rev)
q.Create("waiting.00000000000000000000", []byte(`{"pipeline":"main.0"}`))
if n, err := s.StartQueue(view); n != 1 || err != nil {
t.Fatalf("the start placed %d (%v), want main.6", n, err)
}
_, stale := q["running.main.5"]
_, lost := q["waiting.00000000000000000000"]
if p, _, _ := mem.Get("main.6"); p.Outcome != seat.PipelineOutcomeRunning || stale || lost {
t.Errorf("after the start main.6 = %s, queue %v", p.Outcome, q)
}
}
// R13/AC1 (design 47 H §748, ADR 0682 §7): a newer waiting pipeline of the same branch takes the movers it also moves out of
// an older waiting one, which ends superseded, saying wait-ended and pipeline-ended, once no mover is left; a running
// pipeline moving the same module runs to its end.
func TestANewerWaitingPipelineSupersedesTheOlder(t *testing.T) {
s, mem, q, said, waits, _, view, v := queueFixture()
take := takeIn(t, s, view)
take(4, "app")
take(5, "app", "lib")
take(6, "app")
p5, _, _ := mem.Get("main.5")
if p5.Outcome != seat.PipelineOutcomeWaiting || len(p5.Movers) != 1 || p5.Movers[0].Module != "lib" || !slices.Equal(p5.Superseded, []string{"app"}) {
t.Errorf("main.5 = %s moving %+v, superseded %v; want waiting with lib alone, app superseded", p5.Outcome, p5.Movers, p5.Superseded)
}
// H-A-53, H-A-107: lib, building on app, which was taken out, is placed with its build's why saying so.
reg, entries := placeFixture()
entries["lib"] = catalog.CatalogEntry{Module: "lib", Artifacts: entries["app"].Artifacts, Edges: []catalog.Edge{{From: "app:program", To: "lib:program", Kind: 1}}}
placing := p5
s.placeMovers(&placing, func(string) (catalog.Registration, bool) { return reg, true }, entries, time.Unix(1, 0))
if st := stepsOf(placing, "lib"); len(st) == 0 || st[0].Step != seat.StepResultStepBuild || !strings.Contains(st[0].Why, "app") || !strings.Contains(st[0].Why, "newest published") {
t.Errorf("lib's build = %+v, want its why naming app superseded out", st)
}
if p, _, _ := mem.Get("main.4"); p.Outcome != seat.PipelineOutcomeRunning || !Moves(p, "app") {
t.Errorf("the running main.4 = %s, want running to its end with app", p.Outcome)
}
*said, *waits = nil, nil
take(7, "lib")
p, _, _ := mem.Get("main.5")
if p.Outcome != seat.PipelineOutcomeSuperseded || p.Ended == nil {
t.Errorf("main.5 = %s, want superseded once no mover is left", p.Outcome)
}
if _, ok := q["waiting.00000000000000000001"]; ok {
t.Errorf("the queue still holds main.5's waiting key: %v", q)
}
if !slices.Contains(*said, "pipeline-ended.main.5") || !slices.ContainsFunc(*waits, func(w seat.Wait) bool { return w.Pipeline == "main.5" && w.How == "superseded" }) {
t.Errorf("said %v, waits %+v; want main.5's wait-ended superseded and its pipeline-ended", *said, *waits)
}
for _, key := range []string{"main.6", "main.7"} {
if p, _, _ := mem.Get(key); p.Outcome != seat.PipelineOutcomeWaiting {
t.Errorf("%s = %s, want waiting", key, p.Outcome)
}
}
// R14/AC2 (§748, H-A-53): place, with why, places main.7 ahead of main.6, over the bound, written in the hand-act log
// first; one naming no why, or a pipeline not waiting, is refused, nothing done.
acts := v.Acts.(*actLog)
if _, err := v.Place("operator", seat.PlaceInput{Pipeline: "main.7"}); err == nil {
t.Error("a place naming no why was taken")
}
a, err := v.Place("operator", seat.PlaceInput{Pipeline: "main.7", Why: "the fix is urgent"})
if err != nil || a.Pipeline.Outcome != seat.PipelineOutcomeRunning || len(a.Pipeline.Steps) == 0 {
t.Fatalf("place = %s with %d steps (%v), want running and placed", a.Pipeline.Outcome, len(a.Pipeline.Steps), err)
}
if len(*acts) != 1 || !strings.Contains((*acts)[0], "mesh-pipeline-runner.place") || !strings.Contains((*acts)[0], "urgent") {
t.Errorf("hand acts = %v", *acts)
}
if p, _, _ := mem.Get("main.6"); p.Outcome != seat.PipelineOutcomeWaiting {
t.Errorf("main.6 = %s, want still waiting behind", p.Outcome)
}
if _, err := v.Place("operator", seat.PlaceInput{Pipeline: "main.4", Why: "x"}); err == nil {
t.Error("a running pipeline was placed")
}
}
// R12/AC1 to AC3 (design 47 H §748; H-A-14, H-A-52, H-A-106): a start beyond pipelines-waiting-cap still waits and raises
// queue-full naming it, cleared when an end brings the waiting back to the cap; a pipeline whose every step not ended is an
// approval asked of the operator leaves the bound, the next placed, and once answered and still running it is keyed running
// at once, over the bound; pipelines waiting with none running raise pipelines-stalled naming the first, cleared when one
// runs. The findings are the scope delivery's, their kind their id, warnings with no owner.
func TestTheQueuesApprovalStallAndCap(t *testing.T) {
s, mem, q, _, _, _, view, v := queueFixture()
f := newHeld()
s.Queue.Findings, s.Queue.Cap = f, 1
take := takeIn(t, s, view)
take(4, "app")
take(5, "lib")
if len(f.held) != 0 {
t.Fatalf("findings %v with one waiting at a cap of 1", f.held)
}
take(6, "tool")
full, ok := f.held["delivery.queue-full.queue-full"]
if p, _, _ := mem.Get("main.6"); !ok || p.Outcome != seat.PipelineOutcomeWaiting || !strings.Contains(full.Summary, "main.6") ||
full.Severity != "warning" || full.Owner != "" {
t.Fatalf("over the cap main.6 = %s, queue-full %+v (%v)", p.Outcome, full, ok)
}
// main.4 waits on its approval alone.
p4, rev, _ := mem.Get("main.4")
for i := range p4.Steps {
st := &p4.Steps[i]
st.Outcome = seat.StepResultOutcomePassed
if st.Step == seat.StepResultStepApproval {
st.Outcome, st.Ask = seat.StepResultOutcomeRunning, "the-ask"
}
}
mem.Update("main.4", p4, rev)
if err := v.Hearer.said(p4, nil, false); err != nil {
t.Fatal(err)
}
_, keyed := q["running.main.4"]
if p, _, _ := mem.Get("main.5"); keyed || p.Outcome != seat.PipelineOutcomeRunning {
t.Errorf("with main.4 on its approval: main.4 keyed %v, main.5 %s; want main.4 out of the bound and main.5 placed", keyed, p.Outcome)
}
if _, ok := f.held["delivery.queue-full.queue-full"]; ok {
t.Error("queue-full stands with one waiting at a cap of 1")
}
s.Rejoin("main.4")
if running, _ := s.Queue.Running(); len(running) != 2 {
t.Errorf("after main.4's answer, running %v; want main.4 keyed at once beside main.5", running)
}
// None runs while main.6 waits: pipelines-stalled names it, cleared once one runs.
for _, k := range []string{"running.main.4", "running.main.5"} {
q.Delete(k)
}
s.Queue.judge(t.Logf)
if st, ok := f.held["delivery.pipelines-stalled.pipelines-stalled"]; !ok || !strings.Contains(st.Summary, "main.6") || st.Owner != "" {
t.Errorf("pipelines-stalled = %+v (%v), want it naming main.6", st, ok)
}
if n, err := s.PlaceQueued(view); n != 1 || err != nil {
t.Fatalf("placed %d (%v)", n, err)
}
if _, ok := f.held["delivery.pipelines-stalled.pipelines-stalled"]; ok {
t.Error("pipelines-stalled stands with main.6 running")
}
}