Builds: design 47 G §24, §25; lead answers A-489, A-489a Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
160 lines
6.5 KiB
Go
160 lines
6.5 KiB
Go
package steprun
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
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, 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.
|
|
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)
|
|
}
|
|
}
|
|
|
|
// 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")
|
|
}
|
|
}
|