Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/pins_test.go
T
2026-11-16 14:02:25 +01:00

224 lines
10 KiB
Go

package pipeline
import (
"errors"
"testing"
catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
delivery "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
)
type saidPin struct {
subject string
pins delivery.Pins
}
// A-449 (design 47 G §153, §579): a pipeline pins its catalog graph revision as a delivery record does, under the record id
// pipeline.<key>, when it starts, again at each start of the runner while it runs, and frees it when it ends, by any
// outcome (superseded included); a run on request, which reads its candidate's revision, pins none.
func TestAPipelinePinsItsCatalogRevisionWhileItRuns(t *testing.T) {
var said []saidPin
say := func(subject string, body any) error {
if p, ok := body.(delivery.Pins); ok {
said = append(said, saidPin{subject, p})
}
return nil
}
want := func(i int, subject, record string, revision int64) {
t.Helper()
if i >= len(said) {
t.Fatalf("said %d pins, want one more (%s %s %d)", len(said), subject, record, revision)
}
p := said[i]
if p.subject != subject || p.pins.Record != record || len(p.pins.Catalog) != 1 || p.pins.Catalog[0].Branch != "main" || p.pins.Catalog[0].Revision != revision {
t.Errorf("pin %d = %+v, want %s of %s at main %d", i, p, subject, record, revision)
}
}
// At its start.
s := &Starter{Pipelines: created{}, Say: say}
if ok, err := s.Start(Begin{Branch: "main", Source: 7, Module: "a", Commit: "c1", Revision: 12}); !ok || err != nil {
t.Fatalf("start: %v %v", ok, err)
}
want(0, delivery.SubjectPinned, "pipeline.main.7.a", 12)
// A start that names no revision pins nothing; one written already says nothing more.
said = nil
if _, err := s.Start(Begin{Branch: "main", Source: 8, Module: "a", Commit: "c1"}); err != nil || len(said) != 0 {
t.Errorf("a start with no revision said %v (%v)", said, err)
}
if ok, _ := s.Start(Begin{Branch: "main", Source: 7, Module: "a", Commit: "c1", Revision: 12}); ok || len(said) != 0 {
t.Errorf("a start written already said %v", said)
}
// At the runner's start, for the running pipelines alone; a run on request pins none.
h, store, _, _, _, _ := fixture()
h.Say = say
p := store[pkey]
p.p.Revision = 12
store[pkey] = p
store["main.4.old"] = struct {
p seat.Pipeline
rev uint64
}{seat.Pipeline{Pipeline: "main.4.old", Branch: "main", Revision: 11, Outcome: seat.PipelineOutcomePassed}, 1}
store["main.r1.app"] = struct {
p seat.Pipeline
rev uint64
}{seat.Pipeline{Pipeline: "main.r1.app", Branch: "main", Revision: 13, Run: "r1", Outcome: seat.PipelineOutcomeRunning}, 1}
said = nil
if n, err := h.PinRunning(); n != 1 || err != nil {
t.Fatalf("PinRunning said %d (%v), want the running pipeline's alone", n, err)
}
want(0, delivery.SubjectPinned, "pipeline."+pkey, 12)
// Its end frees it, and so does a supersede.
said = nil
if err := h.said(store[pkey].p, false, true); err != nil {
t.Fatal(err)
}
want(0, delivery.SubjectFreed, "pipeline."+pkey, 12)
said = nil
if n, err := h.Supersede(seat.Pipeline{Pipeline: "main.6.app", Branch: "main", Source: 6, Module: "app", Commit: "c2", Fingerprint: "src2:bb", Outcome: seat.PipelineOutcomeRunning}, nil, nil); n != 1 || err != nil {
t.Fatalf("supersede %d (%v)", n, err)
}
want(0, delivery.SubjectFreed, "pipeline."+pkey, 12)
if len(said) != 1 {
t.Errorf("a supersede said %d pins, want the one freed (through its end, once)", len(said))
}
}
// A-448 (§200, §476): a pipeline of a take-in starts at the revision the take-in made, never at the one the runner's own view
// holds; while the view holds an older one it starts with no step and places none, and the view reaching the revision places
// them. An event that names no revision starts at the view's, as before.
func TestATakeInStartsItsPipelinesAtItsOwnRevisionAndWaitsForTheView(t *testing.T) {
reg, entries := placeFixture()
store := memCreated{memStore{}}
asks := &stepAsks{}
s := &Starter{Pipelines: store, Steps: asks, Name: "the-runner", Say: func(string, any) error { return nil },
Registration: func(m string) (catalog.Registration, bool) { return reg, m == "app" }}
viewRevision := int64(356)
view := func(string) (Half, bool) { return Half{Revision: viewRevision, Entries: entries}, true }
taken := catalog.TakeInTaken{Key: "m.1", Branch: "main", Source: 947, Revision: 357,
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)
}
p, _, _ := store.Get("main.947.app")
if p.Revision != 357 || len(p.Steps) != 0 || len(asks.asks) != 0 {
t.Fatalf("a take-in of revision 357 with the view at 356 started %+v with %d asks, want revision 357 and no step", p, len(asks.asks))
}
if n, err := s.Waiting(store.memStore, "main", view); n != 0 || err != nil || len(asks.asks) != 0 {
t.Errorf("the view at 356 placed %d (%v) of the pipeline waiting for 357", n, err)
}
viewRevision = 357
n, err := s.Waiting(store.memStore, "main", view)
p, _, _ = store.Get("main.947.app")
if n != 1 || err != nil || len(p.Steps) == 0 || len(asks.asks) == 0 || p.Revision != 357 {
t.Errorf("the view reaching 357 placed %d (%v): %d steps, %d asks at revision %d", n, err, len(p.Steps), len(asks.asks), p.Revision)
}
// An event of before A-448 names no revision: the view's.
taken = catalog.TakeInTaken{Key: "m.2", Branch: "main", Source: 948, Entries: []catalog.TakeInEntry{{Module: "app", Commit: "c2", Fingerprint: "src2:bb"}}}
if n, err := s.TakenIn(taken, view); n != 1 || err != nil {
t.Fatalf("the old take-in started %d (%v)", n, err)
}
if p, _, _ := store.Get("main.948.app"); p.Revision != 357 || len(p.Steps) == 0 {
t.Errorf("a take-in naming no revision started %+v, want the view's revision 357 with its steps", p)
}
}
// A-448: the view is read before the starts write; a view that reaches the revision meanwhile places the held-back pipelines at
// once, not at an advance that already came.
func TestAViewThatReachesTheRevisionDuringTheStartsPlacesTheHeldBackPipelines(t *testing.T) {
reg, entries := placeFixture()
store := memCreated{memStore{}}
asks := &stepAsks{}
s := &Starter{Pipelines: store, Steps: asks, Name: "the-runner", Say: func(string, any) error { return nil },
Registration: func(m string) (catalog.Registration, bool) { return reg, m == "app" }}
s.Store = store.memStore
reads := 0
view := func(string) (Half, bool) {
reads++
if reads > 1 { // the first read is before the starts wrote; the view reaches 357 after it
return Half{Revision: 357, Entries: entries}, true
}
return Half{Revision: 356, Entries: entries}, true
}
taken := catalog.TakeInTaken{Key: "m.1", Branch: "main", Source: 947, Revision: 357, Entries: []catalog.TakeInEntry{{Module: "app", Commit: "c1", Fingerprint: "src2:aa"}}}
if n, err := s.TakenIn(taken, view); n != 1 || err != nil {
t.Fatalf("started %d (%v)", n, err)
}
if p, _, _ := store.Get("main.947.app"); len(p.Steps) == 0 || len(asks.asks) == 0 {
t.Errorf("the pipeline has %d steps and %d asks after the view reached 357 during the start, want them placed", len(p.Steps), len(asks.asks))
}
}
// A-448 (§200): the first half a view holds is Ready, which asks again the steps placed (§88); a newer revision is an advance,
// which only places the steps of pipelines that waited for it and asks nothing again.
func TestReadinessSaysTheFirstHoldAndEachNewerRevisionApart(t *testing.T) {
var r Readiness
for i, c := range []struct {
rev int64
first, advanced bool
}{{356, true, false}, {356, false, false}, {355, false, false}, {357, false, true}, {357, false, false}, {360, false, true}} {
if first, advanced := r.Observe(c.rev); first != c.first || advanced != c.advanced {
t.Errorf("observation %d of revision %d: first %v advanced %v, want %v %v", i, c.rev, first, advanced, c.first, c.advanced)
}
}
}
// A-448: placing the steps of a pipeline that waited asks nothing again of the running steps of the others.
func TestPlacingAWaitingPipelineAsksNoRunningStepAgain(t *testing.T) {
reg, entries := placeFixture()
store := memCreated{memStore{}}
asks := &stepAsks{}
s := &Starter{Pipelines: store, Steps: asks, Name: "the-runner", Say: func(string, any) error { return nil },
Registration: func(m string) (catalog.Registration, bool) { return reg, m == "app" }}
view := func(string) (Half, bool) { return Half{Revision: 357, Entries: entries}, true }
if n, err := s.TakenIn(catalog.TakeInTaken{Key: "m.1", Branch: "main", Source: 1, Revision: 357, Entries: []catalog.TakeInEntry{{Module: "app", Commit: "c1", Fingerprint: "src2:aa"}}}, view); n != 1 || err != nil {
t.Fatalf("started %d (%v)", n, err)
}
before := len(asks.asks)
late := func(string) (Half, bool) { return Half{Revision: 358, Entries: entries}, true }
if n, err := s.Waiting(store.memStore, "main", late); n != 0 || err != nil || len(asks.asks) != before {
t.Errorf("an advance placed %d (%v) and asked %d steps again, want nothing", n, err, len(asks.asks)-before)
}
}
// A-449: the pipeline-ended's widens read the pipeline's revision while it is still pinned; the pin is freed after the end is
// said, also when saying it fails.
func TestThePinIsFreedAfterTheEndAndItsWidensAreRead(t *testing.T) {
h, store, _, _, _, _ := fixture()
p := store[pkey]
p.p.Revision = 12
store[pkey] = p
freed, readWhileFreed, bodies := false, false, 0
h.Say = func(subject string, body any) error {
switch body.(type) {
case delivery.Pins:
freed = freed || subject == delivery.SubjectFreed
default:
bodies++
}
return nil
}
h.Widens = func(seat.Pipeline) []seat.Widening {
readWhileFreed = freed // the revision is gone once the freed is said
return nil
}
if err := h.said(store[pkey].p, false, true); err != nil {
t.Fatal(err)
}
if readWhileFreed || !freed || bodies == 0 {
t.Errorf("widens read after the freed %v, freed %v, %d other events: want the widens read first and the pin freed after the end", readWhileFreed, freed, bodies)
}
// An end that cannot be said still frees the pin.
freed = false
h.Say = func(subject string, body any) error {
if _, ok := body.(delivery.Pins); ok {
freed = freed || subject == delivery.SubjectFreed
return nil
}
return errors.New("bus away")
}
if err := h.said(store[pkey].p, false, true); err == nil || !freed {
t.Errorf("an end not said: err %v, freed %v, want the error and the pin freed", err, freed)
}
}