Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/asks_test.go
T
jschoubben e9673046cb Runner: read the bus, not a copy; arm a bound only while its count shrinks (issue 1984)
Review round 1. The store kept a running pipeline's mover-key values in
memory while its record stayed at the revision read, but the module's
own runner_unlogged_append writes through a store of its own: a read
between that store's record write and its mover-key writes kept old
values at the new revision, and the runner served `running` while the
bus held `passed`. The copy goes; each key is still read by its name,
none listed, with the trunk's check of the revisions read.

A bound passing while step ends wait now arms the bound again only while
their count (not yet delivered plus unacknowledged) shrank since the
bound before, kept as `waited` on the ask's mark beside `unread`: an end
the handler naks forever, which no delivery limit stops, ends the step
saying the count did not shrink. Every such why says only what is true
("its ask X is not known to be answered: ..."), and none raises
call-unanswered. The ask clock the start makes now reads the durable
itself, so the integration row fails when the start wires none; the
nudge's mutex is the hearer's own.

Builds: issue 1984 note 6 (1984/AC1, 1984/AC2), notes 7 (b), (c), 9 (d), 10 (e)
2026-11-23 18:07:56 +01:00

290 lines
15 KiB
Go

package pipeline
import (
"errors"
"fmt"
"slices"
"strings"
"testing"
"time"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
wants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
nodebuildgo "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo"
)
// Design 47 H2 §778, H-A-395: every ask but a test run's is bounded from its put; its holder's ask-taken arms the bound again
// with what is left of it since taken-at, each step-progress from now, and the step's end deletes it. The bound passing ends
// the step could not run ("not answered" when taken, "not taken" naming the seat's holder when not), drops its ask through
// the seat's drop, raises the finding and ends the pipeline; a late ask-taken clears the finding. At a start an ask taken 9
// minutes before, with a 10-minute bound, has one minute left.
func TestAnAsksBoundCountsFromThePullAndEndsItsStep(t *testing.T) {
for _, taken := range []bool{true, false} {
t.Run(fmt.Sprintf("taken %v", taken), func(t *testing.T) {
v, store, said, asks, _, _ := verbsFixture()
var armed, rearmed, unbound, raised, cleared, dropped []string
logs := []string{}
v.Hearer.Logf = func(f string, a ...any) { logs = append(logs, fmt.Sprintf(f, a...)) }
now := time.Date(2026, 11, 22, 2, 0, 0, 0, time.UTC)
v.Hearer.Now = func() time.Time { return now }
c := &AskClock{Bound: 10 * time.Minute,
Arm: func(key string, ttl time.Duration) error { armed = append(armed, key+" "+ttl.String()); return nil },
Rearm: func(key string, ttl time.Duration) error { rearmed = append(rearmed, key+" "+ttl.String()); return nil },
Unbound: func(key string) error { unbound = append(unbound, key); return nil },
Raise: func(s, ask string, _ time.Duration) error { raised = append(raised, s+" "+ask); return nil },
Clear: func(s, ask string) error { cleared = append(cleared, s+" "+ask); return nil }}
v.Starter.Asks, v.Hearer.Asks = c, c
v.Hearer.DropRun = func(onSeat, node, ask, why string) error {
dropped = append(dropped, onSeat+" "+node+" "+ask+" "+why)
return nil
}
// A test run's ask keeps §725's bounds; a nil clock arms none.
if err := c.arm(Asked{Seat: nodebuildgo.Seat, ID: "r1", Run: "run"}); err != nil || len(armed) != 0 {
t.Fatalf("a test run's ask armed %v (%v)", armed, err)
}
if err := (*AskClock)(nil).arm(Asked{Seat: nodebuildgo.Seat, ID: "x"}); err != nil {
t.Fatal(err)
}
// Every step but app:program's lint has ended; that lint failed, and its rerun's ask is armed before it is put.
p, rev, _ := store.Get(pkey)
for i := range p.Steps {
if open(p.Steps[i]) {
p.Steps[i].Outcome = seat.StepResultOutcomePassed
}
}
p.Steps[stepIndex(p, "lint", "app:program")].Outcome = seat.StepResultOutcomeFailed
store.Update(pkey, p, rev)
old := p.Steps[stepIndex(p, "lint", "app:program")].Ask
_ = v.Hearer.Clock.MarkAsk(old, AskMark{Pipeline: pkey, Module: "app", Step: "lint"})
r, err := v.Rerun("operator", seat.RerunInput{Pipeline: pkey, Step: "lint", Artifact: "app:program", Why: "retry"})
if err != nil {
t.Fatal(err)
}
// The step asked again keeps one ask key, its new ask's: the earlier one is deleted (H-A-506 addendum).
if _, had, _ := v.Hearer.Clock.AskOf(old); had {
t.Errorf("the ask key of the earlier ask %s outlives the rerun", old)
}
if m, ok, _ := v.Hearer.Clock.AskOf(r.Result.Ask); !ok || m.Pipeline != pkey || m.Step != "lint" {
t.Errorf("the rerun's ask key = %+v, %v; want it naming %s's lint step", m, ok, pkey)
}
ask, key := r.Result.Ask, AskBoundKey(nodebuildgo.Seat, r.Result.Ask)
if want := []string{key + " 10m0s"}; !slices.Equal(armed, want) || len(asks.ids) != 1 {
t.Fatalf("armed %v asking %v, want %v before one ask", armed, asks.ids, want)
}
if got, _, _ := store.Get(pkey); got.Steps[stepIndex(got, "lint", "app:program")].AskedAt == nil {
t.Fatal("the step keeps no asked-at")
}
if taken {
// Taken 9 minutes ago: one minute is left of its bound, at its ask-taken as at a start.
at, err := v.Hearer.Taken(ask, "n1", now.Add(-9*time.Minute))
if err != nil || at == nil || !at.Equal(now.Add(-9*time.Minute)) {
t.Fatalf("taken-at %v (%v)", at, err)
}
if err := c.Taken(nodebuildgo.Seat, ask, *at, now); err != nil || !slices.Equal(rearmed, []string{key + " 1m0s"}) {
t.Fatalf("re-armed %v (%v), want one minute left", rearmed, err)
}
open, err := v.OpenAsks()
if err != nil || len(open) != 1 || open[0].Ask != ask || !open[0].Since.Equal(*at) || c.Left(open[0].Since, now) != time.Minute {
t.Fatalf("open asks %+v (%v), want the ask since its taken-at", open, err)
}
if err := c.Progress(nodebuildgo.Seat, ask); err != nil || rearmed[1] != key+" 10m0s" {
t.Fatalf("a step-progress re-armed %v (%v), want the whole bound", rearmed, err)
}
}
// The bound passes: the step ends could not run and is dropped, the finding raised, the pipeline ended.
if err := v.AskUnanswered(nodebuildgo.Seat, ask); err != nil {
t.Fatal(err)
}
got, _, _ := store.Get(pkey)
s := got.Steps[stepIndex(got, "lint", "app:program")]
why, node := UntakenWhy(nodebuildgo.Seat), ""
if taken {
why, node = UnansweredWhy(ask), "n1"
}
if s.Outcome != seat.StepResultOutcomeCouldNotRun || s.Why != why || s.Ended == nil {
t.Errorf("the step = %+v, want could not run: %s", s, why)
}
if !taken && !strings.Contains(s.Why, "node-build-go's holder takes none") {
t.Errorf("an untaken ask's why %q names no holder that does not take", s.Why)
}
if !slices.Equal(dropped, []string{nodebuildgo.Seat + " " + node + " " + ask + " " + why}) {
t.Errorf("dropped %v, want the ask dropped through its seat", dropped)
}
// The end is one journal line, as every said end (H-A-320 (1), H-A-333).
if n := countLines(logs, "step ended "+pkey+" app lint could not run in "); n != 1 {
t.Errorf("the bound's end journaled %d times in %q, want once", n, logs)
}
if got.Outcome != seat.PipelineOutcomeFailed || len(asks.ids) != 1 {
t.Errorf("the pipeline is %s (%s), asked %d, want failed and nothing asked again", got.Outcome, got.Why, len(asks.ids))
}
if !slices.Equal(raised, []string{nodebuildgo.Seat + " " + ask}) || !slices.Contains(*said, "pipeline-ended.main.5") {
t.Errorf("raised %v, said %v", raised, *said)
}
// A bound whose step ended asks nothing; the step's end deletes the bound; a late ask-taken after the pipeline's
// end clears nothing: the end deleted the ask's key, so the finding stands as what happened (H-A-506 second
// addendum; H-A-238 (2) holds while the pipeline runs).
if err := v.AskUnanswered(nodebuildgo.Seat, ask); err != nil || len(asks.ids) != 1 || len(dropped) != 1 {
t.Errorf("a bound of an ended step asked %v, dropped %v (%v)", asks.ids, dropped, err)
}
if err := c.Ended(nodebuildgo.Seat, ask); err != nil || !slices.Equal(unbound, []string{key}) {
t.Errorf("the step's end deleted %v (%v)", unbound, err)
}
if _, err := v.Hearer.Taken(ask, "n2", now); err != nil || len(cleared) != 0 {
t.Errorf("a late ask-taken after the pipeline's end cleared %v (%v), want nothing", cleared, err)
}
})
}
if s, a, ok := ParseAskBound(AskBoundKey("mesh-wants", "a1")); !ok || s != "mesh-wants" || a != "a1" {
t.Errorf("ParseAskBound = %s %s %v", s, a, ok)
}
if _, _, ok := ParseAskBound(DropBoundKey("node-build-go", "a1")); ok {
t.Error("a drop's bound read as an ask's")
}
}
// 1984/AC2 (issue 1984 notes 6, 7, 9 and 10): before a bound ends its step, the runner reads its own step-ended durable of the
// ask's seat once. While step ends wait there, fewer than at the bound before, the step is not ended: its bound is armed
// again and the journal says how many wait; a count no smaller ends the step naming it. With none waiting the step ends as
// before, its why true, the journal naming the count read. A read that fails arms the bound once more, kept on the ask's
// mark; a second failed read at that bound ends the step saying the durable was not read. Neither says its holder said
// nothing, and neither raises call-unanswered. A read that succeeds between clears the mark.
func TestABoundPassingWhileStepEndsWaitArmsItAgain(t *testing.T) {
type read struct {
pending, unacked uint64
err error
}
for _, c := range []struct {
name string
reads []read
// what each bound's passing leaves: "armed" (the step still running, its bound armed again) or the why it ended with
want []string
}{
{"step ends wait: armed again, then ended once none waits", []read{{3, 1, nil}, {0, 0, nil}}, []string{"armed", "unanswered"}},
{"a failed read arms once more; a second ends saying the durable was not read", []read{{0, 0, errors.New("timeout")}, {0, 0, errors.New("timeout")}}, []string{"armed", "unread"}},
{"a read between two failed ones clears the first", []read{{0, 0, errors.New("timeout")}, {2, 0, nil}, {0, 0, errors.New("timeout")}, {0, 0, errors.New("timeout")}}, []string{"armed", "armed", "armed", "unread"}},
// 1984/AC2, notes 9 (d) and 10 (e): an end the runner takes again and again (the durables keep no delivery limit)
// never shrinks the count, so the bound is armed again only while the count shrinks, and a count no smaller ends the
// step saying it.
{"armed again only while the count shrinks; a count no smaller ends the step naming it", []read{{5, 0, nil}, {3, 1, nil}, {4, 0, nil}}, []string{"armed", "armed", "stuck"}},
} {
t.Run(c.name, func(t *testing.T) {
v, store, _, _, _, journal := verbsFixture()
now := time.Date(2026, 11, 23, 17, 0, 0, 0, time.UTC)
v.Hearer.Now = func() time.Time { return now }
var rearmed, raised, dropped []string
n := 0
clock := &AskClock{Bound: 10 * time.Minute,
Arm: func(string, time.Duration) error { return nil },
Rearm: func(key string, ttl time.Duration) error { rearmed = append(rearmed, key+" "+ttl.String()); return nil },
Unbound: func(string) error { return nil },
Raise: func(s, ask string, _ time.Duration) error { raised = append(raised, s+" "+ask); return nil },
Waiting: func(onSeat string) (uint64, uint64, error) {
if onSeat != nodebuildgo.Seat {
t.Errorf("the durable of %s was read; want the ask's seat's", onSeat)
}
r := c.reads[n]
n++
return r.pending, r.unacked, r.err
}}
v.Starter.Asks, v.Hearer.Asks = clock, clock
v.Hearer.DropRun = func(onSeat, _, ask, why string) error { dropped = append(dropped, ask+" "+why); return nil }
p, rev, _ := store.Get(pkey)
for i := range p.Steps {
if open(p.Steps[i]) {
p.Steps[i].Outcome = seat.StepResultOutcomePassed
}
}
p.Steps[stepIndex(p, "lint", "app:program")].Outcome = seat.StepResultOutcomeFailed
store.Update(pkey, p, rev)
r, err := v.Rerun("operator", seat.RerunInput{Pipeline: pkey, Step: "lint", Artifact: "app:program", Why: "retry"})
if err != nil {
t.Fatal(err)
}
ask := r.Result.Ask
if _, err := v.Hearer.Taken(ask, "n1", now.Add(-10*time.Minute)); err != nil {
t.Fatal(err)
}
for i, want := range c.want {
*journal = nil
if err := v.AskUnanswered(nodebuildgo.Seat, ask); err != nil {
t.Fatal(err)
}
got, _, _ := store.Get(pkey)
s := got.Steps[stepIndex(got, "lint", "app:program")]
switch want {
case "armed":
if !open(s) || len(rearmed) != i+1 || rearmed[i] != AskBoundKey(nodebuildgo.Seat, ask)+" 10m0s" || len(dropped)+len(raised) != 0 {
t.Fatalf("bound %d: the step is %s (%s), armed again %v, dropped %v, raised %v; want it running and its bound armed again whole",
i+1, s.Outcome, s.Why, rearmed, dropped, raised)
}
if c.reads[i].err != nil && countLines(*journal, "is not read") != 1 {
t.Errorf("bound %d: the journal %q does not say the durable was not read", i+1, *journal)
}
if c.reads[i].err == nil && countLines(*journal, fmt.Sprintf("%d step ends wait", c.reads[i].pending+c.reads[i].unacked)) != 1 {
t.Errorf("bound %d: the journal %q does not say how many step ends wait", i+1, *journal)
}
m, _, _ := v.Hearer.Clock.AskOf(ask)
if (m.Unread != nil) != (c.reads[i].err != nil) {
t.Errorf("bound %d: the ask's mark keeps unread %v after a read that failed %v", i+1, m.Unread, c.reads[i].err != nil)
}
if n := c.reads[i].pending + c.reads[i].unacked; c.reads[i].err == nil && (m.Waited == nil || *m.Waited != n) {
t.Errorf("bound %d: the ask's mark keeps waited %v; want %d, the count read", i+1, m.Waited, n)
}
case "unanswered":
if s.Outcome != seat.StepResultOutcomeCouldNotRun || s.Why != UnansweredWhy(ask) || len(raised) != 1 {
t.Fatalf("bound %d: the step is %s (%s), raised %v; want could not run: %s, and its finding", i+1, s.Outcome, s.Why, raised, UnansweredWhy(ask))
}
if countLines(*journal, "0 not yet delivered, 0 unacknowledged") != 1 {
t.Errorf("bound %d: the journal %q does not name the count read", i+1, *journal)
}
case "unread", "stuck":
what := unreadWhat(nodebuildgo.Seat)
if want == "stuck" {
what = stuckWhat(nodebuildgo.Seat, 4, 4)
}
if s.Outcome != seat.StepResultOutcomeCouldNotRun || s.Why != notKnownWhy(ask, what) || strings.Contains(s.Why, "said nothing") {
t.Fatalf("bound %d: the step is %s (%s); want could not run: %s", i+1, s.Outcome, s.Why, notKnownWhy(ask, what))
}
if len(raised) != 0 || len(dropped) != 1 {
t.Errorf("bound %d: raised %v, dropped %v; want no call-unanswered, which says its holder said nothing, and the ask dropped", i+1, raised, dropped)
}
}
}
if n != len(c.reads) {
t.Errorf("the durable was read %d times; want once at each bound, %d", n, len(c.reads))
}
})
}
}
// H-A-252: the pipeline's one validate ask names no mover (H-A-51), and its holder's end names the mover whose body it took:
// it is matched by its ask alone and ends the validate step of every mover under it, whichever mover it names.
func TestAValidateEndNamingOneMoverEndsEveryMoversValidate(t *testing.T) {
h, store, _, _, _ := fixture()
p, rev, _ := store.Get(pkey)
p.Movers = []seat.Mover{{Module: "app", Commit: "c1", Fingerprint: "src2:aa"}, {Module: "lib", Commit: "c2", Fingerprint: "src2:bb"}}
p.Steps = []seat.StepResult{
{Module: "app", Step: seat.StepResultStepValidate, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeRunning, Ask: "v1", Seat: wants.Seat},
{Module: "lib", Step: seat.StepResultStepValidate, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeRunning, Ask: "v1", Seat: wants.Seat}}
store.Update(pkey, p, rev)
var bs StepSeat
for _, x := range StepSeats {
if x.Seat == wants.Seat {
bs = x
}
}
ev := StepEnded{ID: "v1", Node: "n1", Pipeline: pkey, Module: "lib", Commit: "c2", Fingerprint: "src2:bb", Step: seat.StepResultStepValidate, Outcome: "passed"}
if _, err := h.OnStepEnded(bs, bs.Prefix+"n1", ev); err != nil {
t.Fatal(err)
}
got, _, _ := store.Get(pkey)
for _, s := range got.Steps {
if s.Outcome != seat.StepResultOutcomePassed {
t.Errorf("%s's validate = %s, want passed by the one end", s.Module, s.Outcome)
}
}
if got.Outcome != seat.PipelineOutcomePassed {
t.Errorf("the pipeline = %s (%s), want passed", got.Outcome, got.Why)
}
}