Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/hold_test.go
T
jschoubben 9fed8edb50 A-206: the held wait's clock is the build.ended of the awaited runner, heard from the builder's stream
The runner hears the builder's stream of build ends (kept last per subject, read whole at its start, A-181: consumes
`mesh-delivery-builder.build.ended.**`) and keeps when each build ended, the stream's own time. wait-held carries it as
`clock-from`, the bound of the wait is armed from it (30 minutes less what passed), and a bound whose build has not ended is armed
when its end is heard, with wait-held said again. A failed end starts no clock.

Builds: design 47 G §266, §269, §703, A-181, A-206

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 06:05:29 +01:00

235 lines
11 KiB
Go

package pipeline
import (
"encoding/json"
"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"
)
// 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
}
// A take-in giving the runner's own module a new fingerprint holds every pipeline it starts, a later take-in's too while one is
// held, with no step asked; the new runner running healthy releases them, placing and asking their steps (design 47 G §65, §266,
// §276, §534; A-195).
func TestPipelinesAreHeldForTheNewRunnerAndReleasedWhenItRuns(t *testing.T) {
reg, entries := placeFixture()
entries["runner"] = catalog.CatalogEntry{Module: "runner"}
store := memCreated{memStore{}}
asks := &stepAsks{}
var said []string
var bounds []string
s := &Starter{Pipelines: store, Steps: asks, Name: "the-runner", Self: "runner", SelfFp: "src2:old",
Awaiting: Awaiting(store.memStore), Ends: func(string, string) (time.Time, bool) { return time.Unix(9, 0), true },
Hold: func(fp string, _ time.Time) error { bounds = append(bounds, fp); return nil },
Registration: func(m string) (catalog.Registration, bool) { return reg, m == "app" },
Say: func(subject string, _ any) error {
said = append(said, subject[strings.Index(subject, ".event.")+7:])
return nil
}}
view := func(string) (Half, bool) { return Half{Entries: entries}, true }
n, err := s.TakenIn(catalog.TakeInTaken{Key: "m.1", Branch: "main", Source: 4, Entries: []catalog.TakeInEntry{
{Module: "app", Commit: "c1", Fingerprint: "src2:aa"}, {Module: "runner", Commit: "c2", Fingerprint: "src2:new"}}}, view)
if err != nil || n != 2 {
t.Fatalf("take-in = %d, %v", n, err)
}
p, _, _ := store.Get("main.4.app")
if p.Held == nil || p.Held.Fingerprint != "src2:new" || len(p.Steps) != 0 || len(asks.asks) != 0 {
t.Fatalf("held = %+v, steps %d, asks %d", p.Held, len(p.Steps), len(asks.asks))
}
if strings.Join(said, "|") != "wait-held.app.main.c1|wait-held.runner.main.c2" || len(bounds) != 2 || bounds[0] != "src2:new" {
t.Errorf("said %v, bounds %v", said, bounds)
}
// A later take-in not naming the runner is held for the same version.
if n, err := s.TakenIn(catalog.TakeInTaken{Key: "m.2", Branch: "main", Source: 5, Entries: []catalog.TakeInEntry{{Module: "app", Commit: "c3", Fingerprint: "src2:bb"}}}, view); err != nil || n != 1 {
t.Fatalf("second take-in = %d, %v", n, err)
}
if p, _, _ := store.Get("main.5.app"); p.Held == nil || len(asks.asks) != 0 {
t.Fatalf("the later pipeline is not held: %+v", p.Held)
}
// Another fingerprint's release starts nothing.
if n, err := s.Release(store.memStore, "src2:other", "ran", view); err != nil || n != 0 {
t.Fatalf("release of another version = %d, %v", n, err)
}
said = nil
n, err = s.Release(store.memStore, "src2:new", "ran", view)
if err != nil || n != 3 {
t.Fatalf("release = %d, %v", n, err)
}
p, _, _ = store.Get("main.4.app")
if p.Held != nil || len(p.Steps) == 0 || len(asks.asks) == 0 {
t.Errorf("after the release: held %+v, steps %d, asks %d", p.Held, len(p.Steps), len(asks.asks))
}
if !strings.Contains(strings.Join(said, "|"), "wait-ended.app.main.c1|pipeline-started.app.main.c1") {
t.Errorf("said %v", said)
}
if _, held := Awaiting(store.memStore)(); held {
t.Error("a pipeline is still held")
}
}
// The verb release starts the held pipelines by hand, recorded in the hand-act log first, saying why and ending each wait
// `released`, and drops the bound of the wait; a release with no why does nothing (design 47 G §276, A-206).
func TestReleaseVerbStartsTheHeldPipelinesByHand(t *testing.T) {
reg, entries := placeFixture()
entries["runner"] = catalog.CatalogEntry{Module: "runner"}
store := memCreated{memStore{}}
asks := &stepAsks{}
var said []string
s := &Starter{Pipelines: store, Steps: asks, Name: "the-runner", Self: "runner", SelfFp: "src2:old", Awaiting: Awaiting(store.memStore),
Registration: func(m string) (catalog.Registration, bool) { return reg, m == "app" },
Say: func(subject string, _ any) error {
said = append(said, subject[strings.Index(subject, ".event.")+7:])
return nil
}}
view := func(string) (Half, bool) { return Half{Entries: entries}, true }
if _, err := s.TakenIn(catalog.TakeInTaken{Key: "m.1", Branch: "main", Source: 4, Entries: []catalog.TakeInEntry{
{Module: "app", Commit: "c1", Fingerprint: "src2:aa"}, {Module: "runner", Commit: "c2", Fingerprint: "src2:new"}}}, view); err != nil {
t.Fatal(err)
}
acts, dropped := &actLog{}, []string{}
v := Verbs{Hearer: &Hearer{Store: store.memStore}, Starter: s, View: view, Acts: acts,
Drop: func(k string) error { dropped = append(dropped, k); return nil }}
if _, err := v.Release("operator", seat.ReleaseInput{}); err == nil || len(*acts) != 0 {
t.Fatalf("a release saying no why: %v, acts %v", err, *acts)
}
said = nil
a, err := v.Release("operator", seat.ReleaseInput{Why: "the new runner will not come"})
if err != nil || a.Released != 2 {
t.Fatalf("release = %+v, %v", a, err)
}
if p, _, _ := store.Get("main.4.app"); p.Held != nil || len(p.Steps) == 0 || len(asks.asks) == 0 {
t.Errorf("after the release: held %+v, steps %d", p.Held, len(p.Steps))
}
if len(*acts) != 1 || !strings.HasPrefix((*acts)[0], "mesh-pipeline-runner.release: ") || len(dropped) != 1 || dropped[0] != HeldKey("src2:new") {
t.Errorf("acts %v, dropped %v", *acts, dropped)
}
if !strings.Contains(strings.Join(said, "|"), "wait-ended.app.main.c1") {
t.Errorf("said %v", said)
}
}
// The bound's expiry starts the held pipelines and says each as late through the starter's Late, with the awaited fingerprint and
// since when; a pipeline that stays held (no view) is not said (design 47 G §266, A-206).
func TestExpiredBoundSaysEachReleasedPipelineLate(t *testing.T) {
reg, entries := placeFixture()
store := memCreated{memStore{}}
since := time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC)
for _, k := range []string{"main.4.app", "dev.4.app"} {
store.memStore[k] = struct {
p seat.Pipeline
rev uint64
}{seat.Pipeline{Pipeline: k, Branch: strings.Split(k, ".")[0], Module: "app", Outcome: seat.PipelineOutcomeRunning,
Held: &seat.Held{Since: since, Fingerprint: "src2:new"}}, 1}
}
var late []string
s := &Starter{Pipelines: store, Steps: &stepAsks{}, Name: "the-runner", Say: func(string, any) error { return nil },
Registration: func(string) (catalog.Registration, bool) { return reg, true },
Late: func(p seat.Pipeline, h seat.Held) {
late = append(late, p.Pipeline+"@"+h.Fingerprint+"@"+h.Since.Format(time.RFC3339))
}}
view := func(b string) (Half, bool) { return Half{Entries: entries}, b == "main" }
if n, err := s.ReleaseExpired(store.memStore, HeldKey("src2:new"), view); err == nil || n != 1 {
t.Fatalf("release = %d, %v (the pipeline of a branch with no view stays held, an error)", n, err)
}
if len(late) != 1 || late[0] != "main.4.app@src2:new@2026-11-11T12:00:00Z" {
t.Errorf("late %v", late)
}
}
// A wait of the step pipeline waits on the runner version (design 47 G §534, A-206).
func TestHeldWaitIsOnTheRunnerVersion(t *testing.T) {
w := (&Starter{}).waitOf(seat.Pipeline{Pipeline: "main.4.app", Branch: "main", Module: "app"}, time.Unix(1, 0), "")
if w.Step != seat.WaitStepPipeline || w.On != "runner version" {
t.Errorf("wait %+v", w)
}
}
// The wait of a held pipeline counts from the build end of the awaited runner: wait-held says it as `clock-from` and the bound is
// armed from it; while that build has not ended nothing is armed, and hearing its end arms the bound and says wait-held again
// with `clock-from`; a failed end starts no clock (design 47 G §266, §269, A-181, A-206).
func TestHeldWaitCountsFromTheBuildEndOfTheAwaitedRunner(t *testing.T) {
reg, entries := placeFixture()
store := memCreated{memStore{}}
ends := &BuildEnds{}
var waits []seat.Wait
var armed []string
s := &Starter{Pipelines: store, Steps: &stepAsks{}, Name: "the-runner", Self: "runner", SelfFp: "src2:old", Awaiting: Awaiting(store.memStore),
Ends: ends.At, Hold: func(fp string, from time.Time) error {
armed = append(armed, fp+"@"+from.Format(time.RFC3339))
return nil
},
Registration: func(string) (catalog.Registration, bool) { return reg, true },
Say: func(subject string, body any) error {
if w, ok := body.(seat.Wait); ok && strings.Contains(subject, ".wait-held.") {
waits = append(waits, w)
}
return nil
}}
view := func(string) (Half, bool) { return Half{Entries: entries}, true }
taken := catalog.TakeInTaken{Key: "m.1", Branch: "main", Source: 4, Entries: []catalog.TakeInEntry{
{Module: "app", Commit: "c1", Fingerprint: "src2:aa"}, {Module: "runner", Commit: "c2", Fingerprint: "src2:new"}}}
end := time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC)
// The runner's build has not ended: held, no clock, no bound.
if _, err := s.TakenIn(taken, view); err != nil {
t.Fatal(err)
}
if len(waits) != 2 || waits[0].ClockFrom != nil || len(armed) != 0 {
t.Fatalf("waits %+v, armed %v", waits, armed)
}
// A failed end starts no clock; a done end does.
if _, _, ok := ends.Heard([]byte(`{"module":"runner","fingerprint":"src2:new","outcome":"failed"}`), end); ok {
t.Error("a failed end was kept")
}
if m, fp, ok := ends.Heard([]byte(`{"module":"runner","fingerprint":"src2:new","outcome":"done"}`), end); !ok || m != "runner" || fp != "src2:new" {
t.Fatalf("heard %q %q %v", m, fp, ok)
}
waits = nil
if n, err := s.Clocked(store.memStore, "runner", "src2:new"); err != nil || n != 2 {
t.Fatalf("clocked = %d, %v", n, err)
}
if len(waits) != 2 || waits[0].ClockFrom == nil || !waits[0].ClockFrom.Equal(end) || len(armed) != 1 || armed[0] != "src2:new@2026-11-11T12:00:00Z" {
t.Errorf("waits %+v, armed %v", waits, armed)
}
// A pipeline held after the end is known says the clock at once.
waits = nil
taken.Key, taken.Source = "m.2", 5
if _, err := s.TakenIn(taken, view); err != nil {
t.Fatal(err)
}
if len(waits) == 0 || waits[0].ClockFrom == nil || !waits[0].ClockFrom.Equal(end) {
t.Errorf("waits %+v", waits)
}
}
// The bound counts from the build end: the 30 minutes less what passed since, never under a second.
func TestHeldTTLCountsFromTheBuildEnd(t *testing.T) {
end := time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC)
for _, c := range []struct {
after, want time.Duration
}{{0, 30 * time.Minute}, {10 * time.Minute, 20 * time.Minute}, {45 * time.Minute, time.Second}} {
if got := HeldTTL(end, end.Add(c.after)); got != c.want {
t.Errorf("after %s the bound has %s, want %s", c.after, got, c.want)
}
}
}