The runner's take-in handler decodes take-in.taken strictly by the seat's contract: an event missing a field its new form requires (branch, source, call) is refused, said in one journal line naming the subject and what it lacks, and ended. The cancel reads no old pipeline (they are dropped at the clean state). Unit tests 84 to 85. Builds: design 47 H §765; lead answers H-A-58, H-A-98 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
319 lines
16 KiB
Go
319 lines
16 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
builds "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
|
|
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"
|
|
)
|
|
|
|
type created map[string][]byte
|
|
|
|
func (c created) Create(key string, value []byte) error {
|
|
if _, ok := c[key]; ok {
|
|
return ErrExists
|
|
}
|
|
c[key] = value
|
|
return nil
|
|
}
|
|
|
|
// The starts (design 47 H §746, §747): a pipeline's key written by compare-and-set on its absence, so a second start of the
|
|
// same key writes nothing; a branch holding a dot is escaped in the key; a start with no source revision or no mover is
|
|
// refused.
|
|
func TestStarts(t *testing.T) {
|
|
store := created{}
|
|
s := &Starter{Pipelines: store, Say: func(string, any) error { return nil }}
|
|
key := Key("main", 41)
|
|
if ok, err := s.Start(Begin{Branch: "main", Source: 41, Movers: []seat.Mover{{Module: "app", Commit: "c1"}}}); !ok || err != nil {
|
|
t.Fatalf("start = %v, %v", ok, err)
|
|
}
|
|
before := string(store[key])
|
|
if ok, err := s.Start(Begin{Branch: "main", Source: 41, Movers: []seat.Mover{{Module: "app", Commit: "c2"}}}); ok || err != nil || string(store[key]) != before {
|
|
t.Errorf("a second start wrote %v (%v)", ok, err)
|
|
}
|
|
if _, err := s.Start(Begin{Branch: "release.1", Source: 3, Movers: []seat.Mover{{Module: "app"}}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if b, r, ok := seat.ParsePipelinesByBranchSourceKey(Key("release.1", 3)); !ok || r != "3" || b == "release" {
|
|
t.Errorf("the key of release.1 is %q", Key("release.1", 3))
|
|
}
|
|
if _, err := s.Start(Begin{Branch: "main", Movers: []seat.Mover{{Module: "app"}}}); err == nil {
|
|
t.Error("a start with no source revision is refused")
|
|
}
|
|
if _, err := s.Start(Begin{Branch: "main", Source: 6}); err == nil {
|
|
t.Error("a start with no mover is refused")
|
|
}
|
|
s.Pipelines = failing{}
|
|
if _, err := s.Start(Begin{Branch: "main", Source: 5, Movers: []seat.Mover{{Module: "app"}}}); err == nil {
|
|
t.Error("a write the bus refuses is an error")
|
|
}
|
|
}
|
|
|
|
// R10/AC1 and AC2 (design 47 H §747, H-A-52): a package-published of a mover of its source's running pipeline whose
|
|
// dependent that pipeline moves starts nothing: the dependent waits there. A publish again of an older build starts one
|
|
// pipeline keyed <branch>.<source>.published.<package>.<version>, its movers the package's kind-1 dependents from the
|
|
// edges the runner holds, at the publishing source's revision, and says pipeline-started with the publish; an ordering
|
|
// edge starts nothing; heard twice, the second writes nothing.
|
|
func TestAPublishStartsOnlyWhatItsPipelineDoesNotMove(t *testing.T) {
|
|
mem := memStore{}
|
|
var said []seat.PipelineStarted
|
|
s := &Starter{Pipelines: memCreated{mem}, Store: mem, Say: func(_ string, body any) error {
|
|
if ev, ok := body.(seat.PipelineStarted); ok {
|
|
said = append(said, ev)
|
|
}
|
|
return nil
|
|
}}
|
|
edges := []catalog.Edge{{From: "mesh-sdk:go", To: "x", Kind: 1}}
|
|
view := func(string) (Half, bool) {
|
|
return Half{Revision: 9, Entries: map[string]catalog.CatalogEntry{
|
|
"mesh-sdk": {Module: "mesh-sdk"},
|
|
"app": {Module: "app", Commit: "c1", Fp: "src2:aa", Repository: "o/app", Edges: edges},
|
|
"tool": {Module: "tool", Commit: "c2", Fp: "src2:bb", Edges: edges},
|
|
"ordered": {Module: "ordered", Edges: []catalog.Edge{{From: "mesh-sdk:go", To: "x", Kind: 4}}}}}, true
|
|
}
|
|
mem[Key("main", 41)] = struct {
|
|
p seat.Pipeline
|
|
rev uint64
|
|
}{seat.Pipeline{Pipeline: "main.41", Branch: "main", Source: 41, Revision: 8, Outcome: seat.PipelineOutcomeRunning,
|
|
Movers: []seat.Mover{{Module: "mesh-sdk"}, {Module: "app"}, {Module: "tool"}}}, 1}
|
|
ev := builds.PackagePublished{Artifact: "mesh-sdk:go", Branch: "main", Source: 41, Module: "mesh-sdk", Version: "0.1.16"}
|
|
if n, err := s.Published(ev, view); n != 0 || err != nil || len(mem) != 1 {
|
|
t.Fatalf("a publish in a pipeline moving its dependents started %d (%v), %d pipelines", n, err, len(mem))
|
|
}
|
|
// A publish again of an older build, at source 30: its pipeline is gone, so every kind-1 dependent is a mover.
|
|
ev.Source, ev.Version = 30, "0.1.9"
|
|
for i := range 2 {
|
|
if n, err := s.Published(ev, view); n != 1-i || err != nil {
|
|
t.Fatalf("hearing %d started %d (%v)", i+1, n, err)
|
|
}
|
|
}
|
|
key := PublishedKey("main", 30, "mesh-sdk:go", "0.1.9")
|
|
p, _, err := mem.Get(key)
|
|
if err != nil || len(p.Movers) != 2 || !Moves(p, "app") || !Moves(p, "tool") || Moves(p, "ordered") || p.Revision != 9 ||
|
|
p.Published == nil || p.Published.Version != "0.1.9" || len(said) != 1 || said[0].Published == nil {
|
|
t.Fatalf("the pipeline %s = %+v (%v), said %+v", key, p, err, said)
|
|
}
|
|
if app, _ := MoverOf(p, "app"); app.Commit != "c1" || app.Fingerprint != "src2:aa" || app.Repository != "o/app" {
|
|
t.Errorf("the mover app = %+v", app)
|
|
}
|
|
}
|
|
|
|
// memCreated is memStore with the Create a start writes through.
|
|
type memCreated struct{ memStore }
|
|
|
|
func (m memCreated) Create(key string, value []byte) error {
|
|
if _, ok := m.memStore[key]; ok {
|
|
return ErrExists
|
|
}
|
|
var p seat.Pipeline
|
|
if err := json.Unmarshal(value, &p); err != nil {
|
|
return err
|
|
}
|
|
m.memStore[key] = struct {
|
|
p seat.Pipeline
|
|
rev uint64
|
|
}{p, 1}
|
|
return nil
|
|
}
|
|
|
|
type failing struct{}
|
|
|
|
func (failing) Create(string, []byte) error { return errors.New("bus away") }
|
|
|
|
// R4/AC1 (design 47 H §746, acceptance test 112): a take-in.taken naming mesh-sdk and one importer starts one pipeline
|
|
// keyed <branch>.<source> by compare-and-set, its movers the event's entries, at the take-in's revision, and says one
|
|
// pipeline-started on <branch>.<source> naming the call and the movers; R4/AC2: heard twice, the second writes nothing. A
|
|
// take-in with no entries and one naming no source revision start nothing.
|
|
func TestATakeInIsOnePipeline(t *testing.T) {
|
|
store := created{}
|
|
var said []seat.PipelineStarted
|
|
var subjects []string
|
|
s := &Starter{Pipelines: store, Say: func(subject string, body any) error {
|
|
if ev, ok := body.(seat.PipelineStarted); ok {
|
|
said, subjects = append(said, ev), append(subjects, subject)
|
|
}
|
|
return nil
|
|
}}
|
|
view := func(string) (Half, bool) { return Half{Revision: 12}, true }
|
|
ev := catalog.TakeInTaken{Branch: "main", Source: 7, Call: "0123456789abcdef", Revision: 12, Entries: []catalog.TakeInEntry{
|
|
{Module: "mesh-sdk", Commit: "c1", Fingerprint: "src2:aa", Repository: "o/sdk"}, {Module: "app", Commit: "c2", Fingerprint: "src2:bb", Repository: "o/app"}}}
|
|
for i := range 2 {
|
|
n, err := s.TakenIn(ev, view)
|
|
if err != nil || n != 1-i {
|
|
t.Fatalf("hearing %d started %d (%v)", i+1, n, err)
|
|
}
|
|
}
|
|
if len(store) != 1 || len(said) != 1 {
|
|
t.Fatalf("written %d pipelines and said %d starts, want one each", len(store), len(said))
|
|
}
|
|
var p seat.Pipeline
|
|
if err := json.Unmarshal(store["main.7"], &p); err != nil || p.Call != ev.Call || p.Revision != 12 || len(p.Movers) != 2 {
|
|
t.Fatalf("the pipeline main.7 = %+v (%v)", p, err)
|
|
}
|
|
if m, _ := MoverOf(p, "mesh-sdk"); m.Commit != "c1" || m.Fingerprint != "src2:aa" || m.Repository != "o/sdk" || m.Tier != 1 {
|
|
t.Errorf("the mover mesh-sdk = %+v", m)
|
|
}
|
|
if subjects[0] != "mesh.seat.mesh-pipeline-runner.event.pipeline-started.main.7" || said[0].Call != ev.Call || len(said[0].Movers) != 2 {
|
|
t.Errorf("pipeline-started %+v on %s", said[0], subjects[0])
|
|
}
|
|
if _, err := s.TakenIn(catalog.TakeInTaken{Branch: "main", Entries: ev.Entries}, view); err == nil {
|
|
t.Error("a take-in naming no source revision is refused")
|
|
}
|
|
if n, err := s.TakenIn(catalog.TakeInTaken{Branch: "main", Source: 9}, view); n != 0 || err != nil {
|
|
t.Errorf("a take-in with no entries started %d (%v)", n, err)
|
|
}
|
|
}
|
|
|
|
// A-327 (design 47 G §21, §23, §54): a pipeline started before its module has a registration starts with no step; the
|
|
// registration-changed that brings it places that pipeline's steps from it and asks them, once: heard again it places nothing.
|
|
func TestAPipelineStartedBeforeItsRegistrationIsPlacedWhenItArrives(t *testing.T) {
|
|
reg, entries := placeFixture()
|
|
store := memCreated{memStore{}}
|
|
asks := &stepAsks{}
|
|
registered := false
|
|
var said []string
|
|
s := &Starter{Pipelines: store, Steps: asks, Name: "the-runner", Say: func(subject string, _ any) error { said = append(said, subject); return nil },
|
|
Registration: func(m string) (catalog.Registration, bool) { return reg, registered && m == "app" }}
|
|
view := func(string) (Half, bool) { return Half{Entries: entries}, true }
|
|
taken := catalog.TakeInTaken{Branch: "main", Source: 4, Entries: []catalog.TakeInEntry{{Module: "app", Commit: "c1", Fingerprint: "src2:aa"}}}
|
|
if n, err := s.TakenIn(taken, view); n != 1 || err != nil {
|
|
t.Fatalf("the take-in started %d (%v)", n, err)
|
|
}
|
|
if p, _, _ := store.Get("main.4"); len(p.Steps) != 0 || len(asks.asks) != 0 {
|
|
t.Fatalf("before its registration the pipeline has %d steps and %d asks, want none", len(p.Steps), len(asks.asks))
|
|
}
|
|
registered = true
|
|
n, err := s.Registered(store.memStore, reg, view)
|
|
p, _, _ := store.Get("main.4")
|
|
if n != 1 || err != nil || len(p.Steps) == 0 || len(asks.asks) == 0 || p.Outcome != seat.PipelineOutcomeRunning {
|
|
t.Fatalf("the registration placed %d (%v): %d steps, %d asks, want the pipeline's steps placed and asked", n, err, len(p.Steps), len(asks.asks))
|
|
}
|
|
before := len(asks.asks)
|
|
if n, err := s.Registered(store.memStore, reg, view); n != 0 || err != nil || len(asks.asks) != before {
|
|
t.Errorf("the registration heard again placed %d (%v), asked %d more, want nothing", n, err, len(asks.asks)-before)
|
|
}
|
|
|
|
// The registration heard before the branch's catalog half is held (or before the runner could place from it) is not
|
|
// lost: the half becoming held, or the registrations' first view, places the waiting pipeline once (Waiting).
|
|
held := false
|
|
late := func(string) (Half, bool) { return Half{Entries: entries}, held }
|
|
taken = catalog.TakeInTaken{Branch: "next", Source: 5, Entries: []catalog.TakeInEntry{{Module: "app", Commit: "c2", Fingerprint: "src2:bb"}}}
|
|
registered = false
|
|
if n, err := s.TakenIn(taken, view); n != 1 || err != nil {
|
|
t.Fatalf("the take-in on next started %d (%v)", n, err)
|
|
}
|
|
registered = true
|
|
if n, err := s.Registered(store.memStore, reg, late); n != 0 || err != nil {
|
|
t.Fatalf("the registration before the half placed %d (%v), want none and no error: it waits on the half", n, err)
|
|
}
|
|
before = len(asks.asks)
|
|
held = true
|
|
if n, err := s.Waiting(store.memStore, "main", late); n != 0 || err != nil {
|
|
t.Errorf("the half of main placed %d (%v) of next's pipelines", n, err)
|
|
}
|
|
n, err = s.Waiting(store.memStore, "next", late)
|
|
p, _, _ = store.Get("next.5")
|
|
if n != 1 || err != nil || len(p.Steps) == 0 || len(asks.asks) == before {
|
|
t.Fatalf("the half of next becoming held placed %d (%v): %d steps, %d asks more, want its steps placed and asked", n, err, len(p.Steps), len(asks.asks)-before)
|
|
}
|
|
before = len(asks.asks)
|
|
if n, err := s.Waiting(store.memStore, "", late); n != 0 || err != nil || len(asks.asks) != before {
|
|
t.Errorf("the registrations' first view after it placed %d (%v), asked %d more, want nothing", n, err, len(asks.asks)-before)
|
|
}
|
|
|
|
// A-336 (§200): a take-in heard before its branch's half is held starts its pipelines with no steps, never placed from
|
|
// empty entries, the registration held or not; the half becoming held places them (Waiting).
|
|
held = false
|
|
taken = catalog.TakeInTaken{Branch: "next", Source: 6, Entries: []catalog.TakeInEntry{{Module: "app", Commit: "c3", Fingerprint: "src2:cc"}}}
|
|
if n, err := s.TakenIn(taken, late); n != 1 || err != nil {
|
|
t.Fatalf("the take-in before the half started %d (%v)", n, err)
|
|
}
|
|
if p, _, _ := store.Get("next.6"); len(p.Steps) != 0 {
|
|
t.Fatalf("a take-in before its branch's half placed %d steps from empty entries, want none", len(p.Steps))
|
|
}
|
|
held = true
|
|
n, err = s.Waiting(store.memStore, "next", late)
|
|
p, _, _ = store.Get("next.6")
|
|
if n != 1 || err != nil || len(p.Steps) == 0 {
|
|
t.Errorf("the half of next placed %d (%v): %d steps, want the take-in's pipeline placed", n, err, len(p.Steps))
|
|
}
|
|
// A-336: a package published heard before its branch's half is held starts nothing, heard again once it is: its
|
|
// dependents are the half's edges (§747).
|
|
held = false
|
|
app := entries["app"]
|
|
app.Edges = []catalog.Edge{{From: "mesh-sdk:go", To: "app:program", Kind: 1}}
|
|
entries["app"] = app
|
|
published := builds.PackagePublished{Artifact: "mesh-sdk:go", Branch: "next", Source: 7, Module: "mesh-sdk", Version: "0.1.17"}
|
|
if n, err := s.Published(published, late); n != 0 || err == nil {
|
|
t.Fatalf("the publish before the half started %d (%v), want none and an error, heard again", n, err)
|
|
}
|
|
held = true
|
|
if n, err := s.Published(published, late); n != 1 || err != nil {
|
|
t.Fatalf("the publish with the half held started %d (%v)", n, err)
|
|
}
|
|
p, _, _ = store.Get(PublishedKey("next", 7, "mesh-sdk:go", "0.1.17"))
|
|
if len(p.Steps) == 0 {
|
|
t.Errorf("the publish's pipeline placed %d steps, want its steps", len(p.Steps))
|
|
}
|
|
|
|
// A-446: a pipeline an earlier runner left set aside for its new version (the record keeps a `held` field, outcome running,
|
|
// no step placed) is placed and asked as a newly started one when the half is held; no hold is read any more.
|
|
var old seat.Pipeline
|
|
if err := json.Unmarshal([]byte(`{"pipeline":"main.940","branch":"main","movers":[{"module":"app","commit":"c9","fingerprint":"src2:new","repository":"","tier":1}],"source":940,"outcome":"running",`+
|
|
`"held":{"since":"2026-11-15T18:05:00Z","fingerprint":"src2:new"}}`), &old); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store.memStore["main.940"] = struct {
|
|
p seat.Pipeline
|
|
rev uint64
|
|
}{old, 1}
|
|
before, saidBefore := len(asks.asks), len(said)
|
|
n, err = s.Waiting(store.memStore, "main", late)
|
|
p, _, _ = store.Get("main.940")
|
|
if len(said) != saidBefore+1 || !strings.HasPrefix(said[saidBefore], "mesh.seat.mesh-pipeline-runner.event.pipeline-started.main.940") {
|
|
t.Errorf("placing the set-aside pipeline said %v, want its pipeline-started (A-446a)", said[saidBefore:])
|
|
}
|
|
if n != 1 || err != nil || len(p.Steps) == 0 || len(asks.asks) == before {
|
|
t.Errorf("the pipeline a runner before this one set aside: placed %d (%v), %d steps, %d asks more, want it started as a newly started one", n, err, len(p.Steps), len(asks.asks)-before)
|
|
}
|
|
}
|
|
|
|
// R1/AC1 (design 47 H §747): a start names an event, never a sweep. A take-in starts the pipelines of its own entries
|
|
// alone: a module of the branch's head whose fingerprint has no pipeline gets none from it.
|
|
func TestTakenInStartsOnlyItsEntries(t *testing.T) {
|
|
mem := memStore{}
|
|
store := memCreated{mem}
|
|
s := &Starter{Pipelines: store, Store: mem, Say: func(string, any) error { return nil }}
|
|
half := Half{Revision: 12, Source: 9, Entries: map[string]catalog.CatalogEntry{
|
|
"moved": {Commit: "c1", Fp: "src2:aa", Repository: "o/moved"},
|
|
"batched": {Commit: "c2", Fp: "src2:bb", Repository: "o/batched"},
|
|
}}
|
|
view := func(string) (Half, bool) { return half, true }
|
|
ev := catalog.TakeInTaken{Branch: "main", Source: 9, Revision: 12,
|
|
Entries: []catalog.TakeInEntry{{Module: "moved", Commit: "c1", Fingerprint: "src2:aa"}}}
|
|
n, err := s.TakenIn(ev, view)
|
|
if err != nil || n != 1 {
|
|
t.Fatalf("the take-in started %d (%v), want its one entry", n, err)
|
|
}
|
|
if p, _, err := mem.Get("main.9"); err != nil || len(p.Movers) != 1 || !Moves(p, "moved") {
|
|
t.Errorf("main.9 = %+v (%v), want the one mover moved: batched is not named by the take-in", p, err)
|
|
}
|
|
}
|
|
|
|
// R16/AC1 (design 47 H §765, H-A-58, H-A-98): a take-in.taken of the old form, with no call (or no branch or source), is
|
|
// refused by the strict decode, naming what it lacks; one of the new form is read whole.
|
|
func TestATakeInOfTheOldFormIsRefused(t *testing.T) {
|
|
old := `{"key":"m.1","kind":"taken","branch":"main","source":3,"entries":[{"module":"app","commit":"c1","fingerprint":"src2:aa","pull":7}]}`
|
|
if _, err := DecodeTakenIn([]byte(old)); err == nil || !strings.Contains(err.Error(), "call") {
|
|
t.Errorf("the old form decoded (%v), want refused naming call", err)
|
|
}
|
|
ev, err := DecodeTakenIn([]byte(`{"branch":"main","source":3,"call":"0123456789abcdef","revision":4,"entries":[{"module":"app","commit":"c1","fingerprint":"src2:aa","repository":"o/app"}]}`))
|
|
if err != nil || ev.Call != "0123456789abcdef" || len(ev.Entries) != 1 {
|
|
t.Errorf("the new form = %+v (%v)", ev, err)
|
|
}
|
|
}
|