steprun: an unread ask whose refusal cannot be written is given back; the unwritten-end and said-end cases assert what they claim; kit case RunsCounted
Builds: design 47 G §24; lead answers A-489a, A-489b Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
This commit is contained in:
@@ -11,6 +11,8 @@ package conformance
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
|
||||
"slices"
|
||||
@@ -288,3 +290,24 @@ func StepEnds(t *testing.T, steps steprun.AskSteps) {
|
||||
t.Errorf("the build module's steps cannot say a step's end: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// RunsCounted is the case every build module runs on its puller (A-489a): the poison rule and a transport fault's run count the
|
||||
// runs the module's steps state says started, so the puller is built with Prior over the module's own read of that state. The
|
||||
// module builds its puller with read as its steps state's read; a step key it holds with a started run must count as that run.
|
||||
func RunsCounted(t *testing.T, puller func(read func(key string) (json.RawMessage, error)) *steprun.Puller) {
|
||||
t.Helper()
|
||||
if err := runsCounted(puller); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func runsCounted(puller func(read func(key string) (json.RawMessage, error)) *steprun.Puller) error {
|
||||
p := puller(func(string) (json.RawMessage, error) { return json.RawMessage(`{"run":2}`), nil })
|
||||
switch {
|
||||
case p == nil || p.Prior == nil:
|
||||
return errors.New("the build module's puller has no Prior: an ask given back would count toward poison")
|
||||
case p.Prior("any") != 2:
|
||||
return fmt.Errorf("Prior over a step key holding run 2 = %d, want 2: the puller does not read the module's steps state", p.Prior("any"))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -196,3 +196,18 @@ func TestStepEndsRefusesStepsThatCannotSayTheirEnd(t *testing.T) {
|
||||
}
|
||||
StepEnds(t, steprun.AskSteps{Node: "n", SayStep: say})
|
||||
}
|
||||
|
||||
// A puller built without Prior, or with one that does not read the module's steps state, fails the case.
|
||||
func TestRunsCountedRefusesAPullerThatDoesNotReadTheStepsState(t *testing.T) {
|
||||
wired := func(read func(string) (json.RawMessage, error)) *steprun.Puller {
|
||||
return &steprun.Puller{Prior: steprun.PriorRuns(read)}
|
||||
}
|
||||
if err := runsCounted(wired); err != nil {
|
||||
t.Errorf("a wired puller: %v", err)
|
||||
}
|
||||
for name, p := range map[string]*steprun.Puller{"none": {}, "constant": {Prior: func(string) int { return 0 }}} {
|
||||
if runsCounted(func(func(string) (json.RawMessage, error)) *steprun.Puller { return p }) == nil {
|
||||
t.Errorf("%s: the case passed a puller that does not count the state's runs", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -632,7 +632,9 @@ func (p *Puller) refuseUnread(msg *Pulled, why error) {
|
||||
step := sdkstep.Step{ID: probe.ID, Tool: tool, Node: p.Node, State: sdkstep.StepRefused, Started: at, Ended: at,
|
||||
Reason: fmt.Sprintf("this build module on %s cannot read the ask: %v", p.Node, why), Owner: buildrun.Owner}
|
||||
if werr := writeEnd(p.Put, key, step); werr != nil {
|
||||
p.logf("the unread ask %s cannot be answered on %s: %v", probe.ID, p.Node, werr)
|
||||
p.logf("the unread ask %s cannot be answered on %s, so it is given back: %v", probe.ID, p.Node, werr)
|
||||
_ = msg.Nak() // A-489a: an end that could not be written is given back, never acknowledged
|
||||
return
|
||||
} else {
|
||||
p.logf("an ask refused: %v", why)
|
||||
_ = msg.Ack() // A-489: ended and written, so it leaves the work queue
|
||||
|
||||
@@ -146,12 +146,12 @@ func DeclareAskWorkers(ctx context.Context, js jetstream.JetStream, language str
|
||||
subject, _ := sdkstep.AskSubject(language, kind)
|
||||
// Two workers whose filters do not overlap: main's subject and the asker's, `<main's>.<asker>` (design 47 G §544).
|
||||
if _, err := js.CreateOrUpdateConsumer(ctx, stream, jetstream.ConsumerConfig{Durable: worker, FilterSubjects: []string{subject},
|
||||
AckPolicy: jetstream.AckExplicitPolicy, AckWait: ackWait, MaxDeliver: 5}); err != nil {
|
||||
AckPolicy: jetstream.AckExplicitPolicy, AckWait: ackWait, MaxDeliver: sdkstep.AskMaxDeliver}); err != nil {
|
||||
return fmt.Errorf("the worker %s: %w", worker, err)
|
||||
}
|
||||
named, _ := sdkstep.AskNamedWorker(language, kind)
|
||||
if _, err := js.CreateOrUpdateConsumer(ctx, stream, jetstream.ConsumerConfig{Durable: named, FilterSubjects: []string{subject + ".*"},
|
||||
AckPolicy: jetstream.AckExplicitPolicy, AckWait: ackWait, MaxDeliver: 5}); err != nil {
|
||||
AckPolicy: jetstream.AckExplicitPolicy, AckWait: ackWait, MaxDeliver: sdkstep.AskMaxDeliver}); err != nil {
|
||||
return fmt.Errorf("the worker %s: %w", named, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
nodebuildgo "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo"
|
||||
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
|
||||
)
|
||||
|
||||
@@ -45,7 +46,7 @@ func TestOnlyRunsThatStartedCountTowardPoison(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
var ended []sdkstep.StepEnded
|
||||
ran := make(chan struct{}, 1)
|
||||
p := &Puller{Source: src, Language: sdkstep.AskGo, Node: "shanks", Put: st.put, Capacity: func() int { return 1 },
|
||||
p := &Puller{Source: src, Language: sdkstep.AskGo, Logf: t.Logf, Node: "shanks", Put: st.put, Capacity: func() int { return 1 },
|
||||
PullWait: 10 * time.Millisecond, Heartbeat: time.Hour, Prior: func(string) int { return c.prior },
|
||||
Say: func(event string, body any, id string) error {
|
||||
if event == "step-ended" {
|
||||
@@ -90,20 +91,107 @@ func TestOnlyRunsThatStartedCountTowardPoison(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A-489a: an end that could not be written is given back, never acknowledged.
|
||||
// A-489a: an end that could not be written is given back, never acknowledged: the ask's start is written, its end is not
|
||||
// (poison), and an unreadable ask with an id whose refusal cannot be written.
|
||||
func TestAnEndThatCannotBeWrittenIsGivenBack(t *testing.T) {
|
||||
src := NewFakeAsks()
|
||||
src.Delivered = sdkstep.AskMaxDeliver
|
||||
_ = src.PublishAsk(testAsk(sdkstep.AskDelivery))
|
||||
put := func(string, any) (uint64, error) { return 0, errors.New("the state is full") }
|
||||
p := &Puller{Source: src, Language: sdkstep.AskGo, Node: "shanks", Put: put, Capacity: func() int { return 1 }, PullWait: 10 * time.Millisecond,
|
||||
Run: func(context.Context, sdkstep.StepAsk) (any, error) { return nil, nil }}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go p.Pull(ctx)
|
||||
waitFor(t, "the ask given back", func() bool { _, _, nk := src.Fates(); return len(nk) >= 1 })
|
||||
if acked, _, _ := src.Fates(); len(acked) != 0 {
|
||||
t.Errorf("acked %v, want none: no end was written", acked)
|
||||
was := EndAttempts
|
||||
EndAttempts = 1
|
||||
defer func() { EndAttempts = was }()
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
data func() []byte
|
||||
prior int
|
||||
}{
|
||||
{"poison", func() []byte { raw, _ := sdkstep.EncodeStepAsk(testAsk(sdkstep.AskDelivery)); return raw }, sdkstep.AskRuns},
|
||||
{"unread with an id", func() []byte { return []byte(`{"id":"mwn1abcdef0123","kind":"check","v":99}`) }, 0},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
src := NewFakeAsks()
|
||||
src.Publish(sdkstep.AskCheck, c.data())
|
||||
if c.prior > 0 { // a delivery of the delivery queue
|
||||
src = NewFakeAsks()
|
||||
src.Publish(sdkstep.AskDelivery, c.data())
|
||||
}
|
||||
var mu sync.Mutex
|
||||
puts := 0
|
||||
put := func(string, any) (uint64, error) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
puts++
|
||||
if puts > 1 || c.prior == 0 { // the start is written, the end is not
|
||||
return 0, errors.New("the state is full")
|
||||
}
|
||||
return 1, nil
|
||||
}
|
||||
p := &Puller{Source: src, Language: sdkstep.AskGo, Node: "shanks", Put: put, Capacity: func() int { return 1 }, PullWait: 10 * time.Millisecond,
|
||||
Prior: func(string) int { return c.prior }, Run: func(context.Context, sdkstep.StepAsk) (any, error) { return nil, nil }}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go p.Pull(ctx)
|
||||
waitFor(t, "the ask given back", func() bool { _, _, nk := src.Fates(); return len(nk) >= 1 })
|
||||
if acked, termed, _ := src.Fates(); len(acked)+len(termed) != 0 {
|
||||
t.Errorf("acked %v termed %v, want none: no end was written", acked, termed)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A-489a: a refused run (Declining) and a dropped ask each say their pipeline step's end as could not
|
||||
// run, naming its pipeline, and are acknowledged once it is written.
|
||||
func TestAnEndedPipelineStepIsSaidAndAcknowledged(t *testing.T) {
|
||||
for _, c := range []string{"declined in its run", "dropped"} {
|
||||
t.Run(c, func(t *testing.T) {
|
||||
src, st := NewFakeAsks(), newSteps()
|
||||
a := sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: sdkstep.AskStep, Language: sdkstep.AskGo, Asker: "mesh-delivery-pipeline", At: time.Now(),
|
||||
Step: &sdkstep.StepRun{Pipeline: "main.12.m", Step: sdkstep.StepLint, Module: "m", Artifact: "m:program", Commit: strings.Repeat("a", 40),
|
||||
Fingerprint: "fp-1", Fetch: sdkstep.Fetch{Repository: "novox/x", Commit: strings.Repeat("a", 40), Dir: "modules/m"}}}
|
||||
raw, eerr := sdkstep.EncodeStepAsk(a)
|
||||
if eerr != nil {
|
||||
t.Fatal(eerr)
|
||||
}
|
||||
src.Publish(sdkstep.AskCheck, raw)
|
||||
var mu sync.Mutex
|
||||
var ended []sdkstep.StepEnded
|
||||
p := &Puller{Source: src, Language: sdkstep.AskGo, Node: "shanks", Put: st.put, Capacity: func() int { return 1 }, PullWait: 10 * time.Millisecond,
|
||||
Say: func(event string, body any, id string) error {
|
||||
if event == "step-ended" {
|
||||
var e sdkstep.StepEnded
|
||||
b, _ := json.Marshal(body)
|
||||
if json.Unmarshal(b, &e) == nil {
|
||||
mu.Lock()
|
||||
ended = append(ended, e)
|
||||
mu.Unlock()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
Run: func(context.Context, sdkstep.StepAsk) (any, error) {
|
||||
return nil, &Declining{Text: "this node builds with another toolchain", Lacks: []string{"the toolchain"}}
|
||||
}}
|
||||
if c == "dropped" {
|
||||
drop, _ := json.Marshal(nodebuildgo.Drop{V: int64(sdkstep.Version), By: "mesh-delivery-builder", Why: "superseded", At: ptr(time.Now())})
|
||||
p.Load = func(key string) ([]byte, error) {
|
||||
if key == sdkstep.DropKey(a.ID) {
|
||||
return drop, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go p.Pull(ctx)
|
||||
waitFor(t, "the end said", func() bool { mu.Lock(); defer mu.Unlock(); return len(ended) >= 1 })
|
||||
waitFor(t, "the ask acknowledged", func() bool { ak, _, _ := src.Fates(); return len(ak) == 1 })
|
||||
mu.Lock()
|
||||
e := ended[0]
|
||||
mu.Unlock()
|
||||
if e.Outcome != sdkstep.OutcomeCouldNotRun || e.Pipeline != "main.12.m" {
|
||||
t.Errorf("the end said %+v, want could not run naming its pipeline", e)
|
||||
}
|
||||
if _, termed, naked := src.Fates(); len(termed)+len(naked) != 0 {
|
||||
t.Errorf("termed %v naked %v, want it acknowledged alone", termed, naked)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user