Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/start_test.go
T
jschoubben 80ff1afacc §819: the runner's tests folded to 104 unit and 3 integration
Eight unit tests and two integration tests become rows of the test they
sit beside (t.Run of the same body), so the runner ends below its start
of 105 unit and 4 integration tests counted by top-level function.

Builds: design 47 H2 §819
2026-11-22 02:19:17 +01:00

532 lines
28 KiB
Go

package pipeline
import (
"encoding/json"
"errors"
"fmt"
condseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
"slices"
"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"
"git.novox.be/novox/mesh-sdk/go/keys"
)
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.ParsePipelinesByBranchRevisionKey(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")
}
}
// H-A-212, H-A-213 (2) and (4), H-A-217: a package-published starts no pipeline, the movers waiting for it in its
// pipeline placed there; a trunk catalog-revision of a release, a take-in again or a recompute starts ONE pipeline keyed
// <branch>.<revision>, the revision read by its number, its movers the revision's, its source and call the revision's;
// heard twice the second writes nothing; a take-in's revision, a candidate's and one with no mover start nothing.
func TestARevisionOfAReleaseStartsOnePipeline(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}}
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}}
view := func(string) (Half, bool) { return Half{Revision: 9, Entries: entries}, true }
ev := builds.PackagePublished{Artifact: "mesh-sdk:go", Branch: "main", Source: 30, Module: "mesh-sdk", Version: "0.1.9"}
if err := s.Published(ev, view); err != nil || len(mem) != 0 {
t.Fatalf("a publish of an older build wrote %d pipelines (%v), want none", len(mem), err)
}
var asked []int64
movers := []catalog.TakeInEntry{{Module: "app", Commit: "c1", Fingerprint: "src2:ab", Repository: "o/app"}, {Module: "tool", Commit: "c2", Fingerprint: "src2:bc"}}
read := func(branch string, revision int64) (Half, error) {
asked = append(asked, revision)
if branch != "release.1" {
return Half{}, fmt.Errorf("branch %q", branch)
}
r := Half{Revision: revision, Source: 41, Entries: entries, Call: "release of mesh-sdk 0.1.17"}
if revision > 10 {
r.Source = 41 + revision // a later source's revision: one of the same source keeps its movers in 10's (H-A-283)
}
if revision != 12 {
r.Movers = movers
}
return r, nil
}
release := catalog.CatalogRevisionEvent{Kind: "trunk", Branch: keys.EscapeName("release.1"), Revision: 10, Source: 41, CallKind: "release", Call: "release of mesh-sdk 0.1.17"}
for i := range 2 {
if n, err := s.Revised(release, read); n != 1-i || err != nil {
t.Fatalf("hearing %d started %d (%v)", i+1, n, err)
}
}
p, _, err := mem.Get(Key("release.1", 10))
if err != nil || len(mem) != 1 || len(p.Movers) != 2 || p.Revision != 10 || p.Source != 41 || p.Call != "release of mesh-sdk 0.1.17" ||
len(said) != 1 || len(asked) != 2 || asked[0] != 10 {
t.Fatalf("the pipeline %s = %+v (%v), %d written, said %d, read %v", Key("release.1", 10), p, err, len(mem), len(said), asked)
}
if app, _ := MoverOf(p, "app"); app.Fingerprint != "src2:ab" || app.Repository != "o/app" || app.Tier != 1 {
t.Errorf("the mover app = %+v, want the revision's", app)
}
for _, c := range []struct {
name string
ev catalog.CatalogRevisionEvent
want int
}{
{"take-in again", catalog.CatalogRevisionEvent{Kind: "trunk", Branch: release.Branch, Revision: 11, CallKind: "take-in-again"}, 1},
{"recompute with no mover", catalog.CatalogRevisionEvent{Kind: "trunk", Branch: release.Branch, Revision: 12, CallKind: "recompute"}, 0},
{"recompute", catalog.CatalogRevisionEvent{Kind: "trunk", Branch: release.Branch, Revision: 13, CallKind: "recompute"}, 1},
{"take-in", catalog.CatalogRevisionEvent{Kind: "trunk", Branch: release.Branch, Revision: 14, CallKind: "take-in"}, 0},
{"candidate", catalog.CatalogRevisionEvent{Kind: "candidate", Branch: release.Branch, Revision: 15, CallKind: "release"}, 0},
} {
if n, err := s.Revised(c.ev, read); n != c.want || err != nil {
t.Errorf("%s: started %d (%v), want %d", c.name, n, err, c.want)
}
}
if _, err := s.Revised(catalog.CatalogRevisionEvent{Kind: "trunk", Branch: release.Branch, Revision: 16, CallKind: "release"},
func(string, int64) (Half, error) { return Half{}, errors.New("no answer") }); err == nil {
t.Error("a revision that cannot be read is not an error, so it is never heard again")
}
}
// 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>.<revision> 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) {
t.Run("a take in of the old form is refused", testATakeInOfTheOldFormIsRefused)
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.12"], &p); err != nil || p.Call != ev.Call || p.Revision != 12 || len(p.Movers) != 2 {
t.Fatalf("the pipeline main.12 = %+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.12" || 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)
}
}
// design 47 H2 §788: before the registrations view's first read is in, a start raises no registration-missing, says
// "registrations not yet read" once and places no step; once read, its movers are placed from the registrations held.
func TestTheRegistrationCheckWaitsForItsView(t *testing.T) {
reg, entries := placeFixture()
store := memCreated{memStore{}}
asks := &stepAsks{}
read := false
var lines []string
s := &Starter{Pipelines: store, Steps: asks, Name: "the-runner", Say: func(string, any) error { return nil },
Logf: func(f string, a ...any) { lines = append(lines, fmt.Sprintf(f, a...)) },
RegistrationsRead: func() bool { return read },
Registration: func(m string) (catalog.Registration, bool) { return reg, read && m == "app" }}
findings := newHeld()
s.Findings, s.Observer = findings, "the-runner"
view := func(string) (Half, bool) { return Half{Entries: entries}, true }
for _, src := range []int64{4, 5} {
taken := catalog.TakeInTaken{Branch: "main", Source: src, Entries: []catalog.TakeInEntry{{Module: "app", Commit: fmt.Sprint("c", src), Fingerprint: fmt.Sprint("src2:a", src)}}}
if _, err := s.TakenIn(taken, view); err != nil {
t.Fatal(err)
}
}
said := 0
for _, l := range lines {
if strings.HasPrefix(l, "registrations not yet read") {
said++
}
}
if len(findings.held) != 0 || said != 1 || len(asks.asks) != 0 {
t.Fatalf("unread: findings %v, said %d times, %d asks; want none, once, none", findings.held, said, len(asks.asks))
}
read = true
if n, err := s.Waiting(store.memStore, "", view); n == 0 || err != nil || len(asks.asks) == 0 {
t.Fatalf("once read: placed in %d (%v), %d asks", n, err, len(asks.asks))
}
if len(findings.held) != 0 {
t.Fatalf("once read, a registered mover raised %v", findings.held)
}
}
// 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" }}
findings := newHeld()
s.Findings, s.Observer = findings, "the-runner"
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))
}
// H-A-262: the mover waiting on its registration is the finding registration-missing, scope module, id the module.
if f, ok := findings.held["module.app."+RegistrationMissingKind]; !ok || f.Observer != "the-runner" || !strings.Contains(f.Summary, "main.4") {
t.Fatalf("the mover waiting on its registration raised %v, want registration-missing of app naming main.4", findings.held)
}
registered = true
n, err := s.Registered(store.memStore, reg, view)
if _, ok := findings.held["module.app."+RegistrationMissingKind]; ok {
t.Errorf("the registration of app arrived and its registration-missing is still held")
}
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-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)
}
// H-A-311 (1): a placing whose write is refused because the pipeline was written meanwhile (a step's end, a publish) is
// done again at once from the record read again, never left unplaced until a restart.
held = false
taken = catalog.TakeInTaken{Branch: "next", Source: 7, Entries: []catalog.TakeInEntry{{Module: "app", Commit: "c4", Fingerprint: "src2:dd"}}}
if n, err := s.TakenIn(taken, late); n != 1 || err != nil {
t.Fatalf("the take-in of next.7 started %d (%v)", n, err)
}
held = true
raced := &racingStore{memStore: store.memStore, before: func(m memStore) {
e := m["next.7"]
e.p.Superseded, e.rev = append(e.p.Superseded, "other"), e.rev+1 // a concurrent write the placing did not read
m["next.7"] = e
}}
n, err = s.Waiting(raced, "next", late)
p, _, _ = store.Get("next.7")
if n != 1 || err != nil || len(p.Steps) == 0 || !slices.Contains(p.Superseded, "other") {
t.Errorf("a placing cut by a concurrent write placed %d (%v): %d steps, superseded %v, want its steps placed on the record read again (H-A-311)", n, err, len(p.Steps), p.Superseded)
}
// A-327: a mover with no registration, in a pipeline placed from the half held later (the queue's turn, the half held),
// is said to wait on its registration, so the pipeline that cannot end before it is placed says why.
var logged []string
s.Logf = func(f string, a ...any) { logged = append(logged, fmt.Sprintf(f, a...)) }
held = false
taken = catalog.TakeInTaken{Branch: "next", Source: 8, Entries: []catalog.TakeInEntry{{Module: "app", Commit: "c4", Fingerprint: "src2:dd"}, {Module: "contracts", Commit: "c4", Fingerprint: "src2:ee"}}}
if n, err := s.TakenIn(taken, late); n != 1 || err != nil {
t.Fatalf("the take-in of two movers started %d (%v)", n, err)
}
held, logged = true, nil
if n, err := s.Waiting(store.memStore, "next", late); n != 1 || err != nil {
t.Fatalf("the half of next placed %d (%v), want next.8's registered mover", n, err)
}
if !slices.ContainsFunc(logged, func(l string) bool { return strings.Contains(l, "next.8: contracts has no registration yet") }) {
t.Errorf("placing next.8 said %q, want that contracts waits on its registration (A-327)", logged)
}
// H-A-262: the mover still waiting is raised from the placing too, and cleared once the registrations held name it.
if _, ok := findings.held["module.contracts."+RegistrationMissingKind]; !ok {
t.Errorf("placing next.8 raised %v, want registration-missing of contracts", findings.held)
}
// H-A-265 (4): a pipeline that ends with the mover still waiting (failed, H-A-163) leaves no finding: judged at its end
// (the hearer's said) and in Waiting, it clears once no running pipeline has an unplaced mover of the module.
next8, rev8, _ := store.Get("next.8")
next8.Outcome = seat.PipelineOutcomeFailed
if _, err := store.Update("next.8", next8, rev8); err != nil {
t.Fatal(err)
}
h := &Hearer{Store: store.memStore, Say: func(string, any) error { return nil }, RegistrationWaits: func() { s.JudgeRegistrationMissing(store.memStore) }}
if err := h.said(next8, nil, true); err != nil {
t.Fatal(err)
}
if _, ok := findings.held["module.contracts."+RegistrationMissingKind]; ok {
t.Errorf("next.8 ended with contracts unplaced and no other pipeline waits on it, yet its registration-missing is held")
}
// Still waited on by a running pipeline, it stays at an end; the registrations held naming it clear it in Waiting.
findings.held["module.contracts."+RegistrationMissingKind] = condseat.Finding{Scope: "module", ID: "contracts", Kind: RegistrationMissingKind}
taken = catalog.TakeInTaken{Branch: "next", Source: 9, Entries: []catalog.TakeInEntry{{Module: "contracts", Commit: "c5", Fingerprint: "src2:ff"}}}
if n, err := s.TakenIn(taken, late); n != 1 || err != nil {
t.Fatalf("the take-in of contracts started %d (%v)", n, err)
}
s.JudgeRegistrationMissing(store.memStore)
if _, ok := findings.held["module.contracts."+RegistrationMissingKind]; !ok {
t.Errorf("next.9 still waits on contracts' registration, yet its registration-missing was cleared")
}
s.Registration = func(string) (catalog.Registration, bool) { return reg, true }
if _, err := s.Waiting(store.memStore, "", late); err != nil {
t.Fatal(err)
}
if _, ok := findings.held["module.contracts."+RegistrationMissingKind]; ok {
t.Errorf("the registrations held name contracts and its registration-missing is still held")
}
}
// 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.12"); err != nil || len(p.Movers) != 1 || !Moves(p, "moved") {
t.Errorf("main.12 = %+v (%v), want the one mover moved: batched is not named by the take-in", p, err)
}
// design 47 H2 §786: the next kept revision carries each unfinished mover still at its entry's fingerprint, one journal
// line each; a key the revision moves or holds at another fingerprint, or not at all, goes; a revision that moves
// nothing starts a pipeline for the unfinished alone; place-unfinished puts one in a pipeline of its own, a hand act.
var journal []string
s.Logf = func(f string, a ...any) { journal = append(journal, fmt.Sprintf(f, a...)) }
u := memUnfinished{"main.batched": `{"fingerprint":"src2:bb","revision":11}`, "main.moved": `{"fingerprint":"src2:00"}`,
"main.gone": `{"fingerprint":"src2:cc"}`, "dev.batched": `{"fingerprint":"src2:bb"}`}
s.Unfinished = &Unfinished{Store: u, Logf: s.Logf}
half.Revision = 13
ev.Revision, ev.Source = 13, 10
if n, err := s.TakenIn(ev, view); err != nil || n != 1 {
t.Fatalf("the take-in of revision 13 started %d (%v)", n, err)
}
if p, _, _ := mem.Get("main.13"); len(p.Movers) != 2 || !Moves(p, "moved") || !Moves(p, "batched") {
t.Errorf("main.13 moves %+v, want moved and the unfinished batched", p.Movers)
}
if u.String() != `dev.batched={"fingerprint":"src2:bb"} main.batched={"fingerprint":"src2:bb","revision":11}` {
t.Errorf("the state unfinished = %s, want main.moved (moved at src2:aa) and main.gone (no entry) gone", u.String())
}
if !strings.Contains(strings.Join(journal, "\n"), "revision 13 of main puts batched in a pipeline again") {
t.Errorf("the journal says %q, want batched put in a pipeline again", journal)
}
half.Revision = 14
if n, err := s.TakenIn(catalog.TakeInTaken{Branch: "main", Source: 11, Revision: 14}, view); err != nil || n != 1 {
t.Fatalf("a take-in moving nothing started %d (%v), want one pipeline for the unfinished", n, err)
}
if p, _, _ := mem.Get("main.14"); len(p.Movers) != 1 || !Moves(p, "batched") {
t.Errorf("main.14 moves %+v, want batched alone", p.Movers)
}
half.Revision = 11
acts := &actLog{}
v := Verbs{Starter: s, View: view, Acts: acts}
if a, err := v.PlaceUnfinished("agent.dev", seat.PlaceUnfinishedInput{Key: "main.batched", Why: "now"}); err != nil || a.Pipeline != "main.11.unfinished.batched" || len(*acts) != 1 {
t.Errorf("place-unfinished = %+v (%v), %d hand acts", a, err, len(*acts))
}
if p, _, _ := mem.Get("main.11.unfinished.batched"); len(p.Movers) != 1 || !Moves(p, "batched") {
t.Errorf("main.11.unfinished.batched moves %+v, want batched", p.Movers)
}
for _, in := range []seat.PlaceUnfinishedInput{{Key: "main.batched", Why: "again"}, {Key: "main.none", Why: "x"}, {Key: "dev.batched"}} {
if _, err := v.PlaceUnfinished("agent.dev", in); err == nil {
t.Errorf("place-unfinished %+v is refused", in)
}
}
}
// 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.
// testATakeInOfTheOldFormIsRefused is a row of TestATakeInIsOnePipeline (design 47 H2 §819: the module's tests are folded under its budget).
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)
}
}
// H-A-218: a package-published is taken into the pipeline of the branch and revision it names; one naming no revision (an
// older builder) into its source's lowest-numbered revision, the take-in's.
func TestAPublishIsTakenIntoThePipelineOfItsRevision(t *testing.T) {
mem := memStore{}
for _, r := range []int64{7, 8} {
mem[Key("main", r)] = struct {
p seat.Pipeline
rev uint64
}{seat.Pipeline{Pipeline: Key("main", r), Branch: "main", Source: 4, Revision: r, Outcome: seat.PipelineOutcomeRunning,
Movers: []seat.Mover{{Module: "mesh-sdk"}}}, 1}
}
s := &Starter{Pipelines: memCreated{mem}, Store: mem, Say: func(string, any) error { return nil }}
view := func(string) (Half, bool) { return Half{Revision: 8}, true }
published := func(key string) []string { m, _ := MoverOf(mem[key].p, "mesh-sdk"); return m.Published }
ev := builds.PackagePublished{Artifact: "mesh-sdk:go", Module: "mesh-sdk", Branch: "main", Source: 4, Revision: 8, Version: "0.1.18"}
if err := s.Published(ev, view); err != nil || len(published("main.8")) != 1 || len(published("main.7")) != 0 {
t.Fatalf("a publish of revision 8 (%v): main.8 %v, main.7 %v; want main.8's mover alone", err, published("main.8"), published("main.7"))
}
ev.Revision, ev.Artifact = 0, "mesh-sdk:ts"
if err := s.Published(ev, view); err != nil || len(published("main.7")) != 1 || len(published("main.8")) != 1 {
t.Errorf("a publish naming no revision (%v): main.7 %v, main.8 %v; want main.7's, the source's lowest", err, published("main.7"), published("main.8"))
}
}
// racingStore is memStore whose first Update is preceded by before, a write another part makes between a read and its write.
type racingStore struct {
memStore
before func(memStore)
}
func (r *racingStore) Update(key string, p seat.Pipeline, rev uint64) (uint64, error) {
if b := r.before; b != nil {
r.before = nil
b(r.memStore)
}
return r.memStore.Update(key, p, rev)
}