Builds: design 47 G §24; lead answers A-489a, A-489b Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
248 lines
10 KiB
Go
248 lines
10 KiB
Go
package steprun
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"sync"
|
|
"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"
|
|
)
|
|
|
|
// Design 47 G *Asked on a work queue, ended by an event* (§24): the bus counts an ask given back before it ran (a seat list
|
|
// not read yet) as a delivery, and that is no run. Only runs that started count toward the poison rule (Puller.Prior, from the
|
|
// step key), and a poisoned pipeline step is ended by its step-ended event, so the runner that asked hears it.
|
|
func TestOnlyRunsThatStartedCountTowardPoison(t *testing.T) {
|
|
stepAsk := func() sdkstep.StepAsk {
|
|
return 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"}}}
|
|
}
|
|
for _, c := range []struct {
|
|
name string
|
|
delivered uint64
|
|
prior int
|
|
ends string // what its end says; "" when it must run
|
|
}{
|
|
{"given back three times, never run", sdkstep.AskRuns + 1, 0, ""},
|
|
{"three runs started and none ended", 3, sdkstep.AskRuns, "poison"},
|
|
{"three runs started, delivered many times", sdkstep.AskRuns + 4, sdkstep.AskRuns, "poison"},
|
|
{"its last delivery, given back before and never run", sdkstep.AskMaxDeliver, 0, "out of its deliveries"},
|
|
{"its last delivery after a run that started", sdkstep.AskMaxDeliver, 1, "poison"},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
src, st := NewFakeAsks(), newSteps()
|
|
src.Delivered = c.delivered
|
|
a := stepAsk()
|
|
raw, err := sdkstep.EncodeStepAsk(a)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
src.Publish(sdkstep.AskCheck, raw) // a pipeline step rides the check queue
|
|
var mu sync.Mutex
|
|
var ended []sdkstep.StepEnded
|
|
ran := make(chan struct{}, 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" {
|
|
var e sdkstep.StepEnded
|
|
raw, _ := json.Marshal(body)
|
|
if err := json.Unmarshal(raw, &e); err == nil {
|
|
mu.Lock()
|
|
ended = append(ended, e)
|
|
mu.Unlock()
|
|
}
|
|
}
|
|
return nil
|
|
},
|
|
Run: func(ctx context.Context, a sdkstep.StepAsk) (any, error) { ran <- struct{}{}; return nil, nil }}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
go p.Pull(ctx)
|
|
if c.ends != "" {
|
|
waitFor(t, "the poisoned ask's end said", func() bool { mu.Lock(); defer mu.Unlock(); return len(ended) == 1 })
|
|
mu.Lock()
|
|
e := ended[0]
|
|
mu.Unlock()
|
|
if e.Outcome != sdkstep.OutcomeCouldNotRun || !strings.Contains(e.Said, c.ends) || e.Pipeline != "main.12.m" {
|
|
t.Fatalf("the end said %+v, want could not run, poison, naming its pipeline", e)
|
|
}
|
|
if acked, termed, naked := src.Fates(); len(acked) != 1 || len(termed)+len(naked) != 0 {
|
|
t.Errorf("acked %v termed %v naked %v, want it acknowledged once its end is written (A-489a)", acked, termed, naked)
|
|
}
|
|
select {
|
|
case <-ran:
|
|
t.Fatal("an ended ask ran")
|
|
default:
|
|
}
|
|
return
|
|
}
|
|
select {
|
|
case <-ran:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("an ask only given back was ended as poison, and never ran")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// 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) {
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// ADR 0664 §1 with the bus's count above the runs that started: the ask was given back twice before its first run, so the
|
|
// bus says delivered 3 while the step key says 1 run began; a transport fault is run 2 (given again after 4 minutes), not 3.
|
|
func TestATransportFaultCountsRunsThatStartedNotDeliveries(t *testing.T) {
|
|
src, st := NewFakeAsks(), newSteps()
|
|
src.Delivered = 3
|
|
a := testAsk(sdkstep.AskDelivery)
|
|
_ = src.PublishAsk(a)
|
|
p := &Puller{Source: src, Language: sdkstep.AskGo, Node: "shanks", Put: st.put, Capacity: func() int { return 1 }, PullWait: 10 * time.Millisecond,
|
|
Prior: func(string) int { return 1 },
|
|
Run: func(context.Context, sdkstep.StepAsk) (any, error) {
|
|
return nil, &sdkstep.Passing{Err: errors.New("lookup proxy: i/o timeout")}
|
|
}}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
go p.Pull(ctx)
|
|
waitFor(t, "the ask given again", func() bool { src.mu.Lock(); defer src.mu.Unlock(); return len(src.Delayed) == 1 })
|
|
key, _ := sdkstep.AskStepKey("", "shanks", a.ID)
|
|
s, _ := st.get(key)
|
|
src.mu.Lock()
|
|
delays := append([]time.Duration(nil), src.Delays...)
|
|
src.mu.Unlock()
|
|
if s.State != sdkstep.StepAgain || s.Run != 2 || len(delays) != 1 || delays[0] != sdkstep.AgainAfter(2) {
|
|
t.Errorf("step %+v, delays %v, want again in run 2 after %s", s, delays, sdkstep.AgainAfter(2))
|
|
}
|
|
}
|
|
|
|
// Puller.Prior over a steps state's read: the Run of the step the key holds, 0 for an absent key, a failed read or what is no
|
|
// step, and nil for no read.
|
|
func TestPriorRunsReadsTheRunOfTheStepAKeyHolds(t *testing.T) {
|
|
read := func(key string) (json.RawMessage, error) {
|
|
switch key {
|
|
case "running":
|
|
return json.RawMessage(`{"id":"x","run":2}`), nil
|
|
case "junk":
|
|
return json.RawMessage(`[`), nil
|
|
case "broken":
|
|
return nil, errors.New("no bus")
|
|
}
|
|
return nil, nil
|
|
}
|
|
prior := PriorRuns(read)
|
|
for key, want := range map[string]int{"running": 2, "absent": 0, "junk": 0, "broken": 0} {
|
|
if got := prior(key); got != want {
|
|
t.Errorf("PriorRuns(%s) = %d, want %d", key, got, want)
|
|
}
|
|
}
|
|
if PriorRuns(nil) != nil {
|
|
t.Error("PriorRuns(nil) is not nil")
|
|
}
|
|
}
|