Files
mesh-delivery/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/stepbounds_integration_test.go
T
jschoubben 07c13b0b9c B1 (builder): the builder's own buckets are named from its credential's module
The versions, bounds, reserved, publish-queue, acted, toolchains-heard and instances buckets were constants naming
mesh-delivery-builder; ownBucketsOf takes them from the module the credential names.

Builds: design 47 G A-296, §428

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 23:32:42 +01:00

108 lines
3.6 KiB
Go

//go:build integration
package main
import (
"encoding/json"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
"sync"
"testing"
"time"
"github.com/nats-io/nats.go/jetstream"
"mesh-delivery-builder/internal/versions"
)
// A-282 (corrected), issue 1930: an ask's step bound key is never deleted to be made again. Two waiters on one ask arm it side
// by side and each takes it as armed; a waiter arming the same movement again writes nothing. A step that moves twice past its
// first bound has its key written again each time, by an update against the revision read with the bound's own time to live,
// so the bound counts from the last movement and the step is not judged standing still; two waiters seeing one movement write
// it once.
func TestTwoWaitersOnOneAskArmItsStepBoundOnce(t *testing.T) {
b := startBed(t)
kv, err := b.js.KeyValue(b.ctx, builds.StateBucket(builds.BuilderModule, versions.BoundsState))
if err != nil {
t.Fatal(err)
}
var mu sync.Mutex
at := time.Date(2026, 11, 12, 12, 0, 0, 0, time.UTC)
clock := func() time.Time { mu.Lock(); defer mu.Unlock(); return at }
moveTo := func(d time.Duration) { mu.Lock(); at = at.Add(d); mu.Unlock() }
waiter := func() *stepBounds {
return &stepBounds{bounds: func() jetstream.KeyValue { return kv }, js: b.js, now: clock, logf: t.Logf}
}
const key, bound = "stand.mwaskaaaaaaaaa", time.Minute
side := func(what string) {
t.Helper()
var wg sync.WaitGroup
errs := make(chan error, 2)
for range 2 {
wg.Add(1)
go func() { defer wg.Done(); errs <- waiter().Arm(b.ctx, key, "the step stood still", bound) }()
}
wg.Wait()
close(errs)
for err := range errs {
if err != nil {
t.Errorf("%s: a waiter of the ask did not take the bound as armed: %v", what, err)
}
}
}
read := func() (jetstream.KeyValueEntry, versions.Bound) {
t.Helper()
e, err := kv.Get(b.ctx, key)
if err != nil {
t.Fatal(err)
}
var v versions.Bound
if err := json.Unmarshal(e.Value(), &v); err != nil {
t.Fatal(err)
}
return e, v
}
side("the first arming")
first, v := read()
if !v.Ends.Equal(clock().Add(bound)) {
t.Errorf("the bound ends %s", v.Ends)
}
if err := waiter().Arm(b.ctx, key, "the step stood still", bound); err != nil {
t.Fatal(err)
}
if again, _ := read(); again.Revision() != first.Revision() {
t.Errorf("the same movement armed again wrote the key: revision %d, was %d", again.Revision(), first.Revision())
}
last := first
for move := range 2 {
moveTo(bound + 10*time.Second) // past the bound before
side("a movement")
e, v := read()
if e.Revision() != last.Revision()+1 || !v.Ends.Equal(clock().Add(bound)) || e.Operation() != jetstream.KeyValuePut {
t.Fatalf("movement %d: revision %d (was %d), ends %s, %s: the bound does not count from the step's movement", move+1, e.Revision(), last.Revision(), v.Ends, e.Operation())
}
msg, err := b.js.Stream(b.ctx, "KV_"+builds.StateBucket(builds.BuilderModule, versions.BoundsState))
if err != nil {
t.Fatal(err)
}
raw, err := msg.GetLastMsgForSubject(b.ctx, "$KV."+builds.StateBucket(builds.BuilderModule, versions.BoundsState)+"."+key)
if err != nil || raw.Header.Get("Nats-TTL") == "" {
t.Fatalf("movement %d: the key written again keeps no time to live (%v, %v)", move+1, raw, err)
}
last = e
}
// Never deleted: the key's history holds no delete marker.
w, err := kv.WatchAll(b.ctx, jetstream.IncludeHistory())
if err != nil {
t.Fatal(err)
}
defer w.Stop()
for e := range w.Updates() {
if e == nil {
break
}
if e.Key() == key && e.Operation() != jetstream.KeyValuePut {
t.Errorf("the bound key was %s at revision %d", e.Operation(), e.Revision())
}
}
}