Files
mesh-delivery/modules/mesh-grants/internal/grants/statecap_test.go
T
jschoubben 146b9b4ff8 Grants: a held bucket of a key-ttl state gets per-message TTL
The indexer's state heard is declared key-ttl, but its bucket was made before
that declaration, and grants' handling of a held stream only added subjects,
raised caps and moved a source filter: every version-reserved the indexer
kept with a time to live was refused (per-message TTL is disabled, 10166) and
given up after five tries. A held bucket whose state is declared key-ttl now
has per-message time to live turned on, add-only like the cap raise, once per
manifest, and said as asserted; the indexer's TTL stays.

Builds: design 47 H2 H-A-501 (corrected)
2026-11-22 22:22:31 +01:00

109 lines
4.5 KiB
Go

package grants
import (
"fmt"
"slices"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-sdk/go/catalogue"
"mesh-grants/internal/buscompose"
)
// raiser is a Creator that holds one stream at a cap and records the cap it is raised to.
type raiser struct {
Creator
to int64
}
func (r *raiser) RaiseMaxBytes(_ string, to int64) (int64, bool, error) {
r.to = to
return 64 << 20, true, nil
}
// A state's declared max-mib reaches the bucket (issue 1975 note 6): the composed stream carries it, and a bucket the bus
// holds at the default 64 MiB is raised to it by the assertion.
func TestAStatesDeclaredCapReachesItsBucket(t *testing.T) {
m := catalogue.Manifest{Module: "mesh-pipeline-runner", State: []catalogue.StateDeclaration{{Name: "compositions", MaxMiB: 256}}}
var composed *Item
for _, it := range Composed(m, nil) {
if it.Stream == "KV_mesh-pipeline-runner_compositions" {
composed = &it
}
}
if composed == nil || composed.Composed.MaxBytes != 256<<20 {
t.Fatalf("the composed bucket is %+v, want a cap of 256 MiB", composed)
}
a := Assertion{Manifest: "d", Items: []Item{*composed}}
a.Items[0].Held = &Limits{MaxBytes: 64 << 20}
r := &raiser{}
_, raises := createMissing(r, &creations{}, &a, m, nil, false)
if r.to != 256<<20 || len(raises) != 1 {
t.Errorf("the held bucket was raised to %d (%v), want 256 MiB", r.to, raises)
}
}
// ttlTurner is a Creator that records each held stream whose per-message time to live it turns on.
type ttlTurner struct {
Creator
turned []string
}
func (r *ttlTurner) AllowMsgTTL(stream string, marker time.Duration) (bool, error) {
r.turned = append(r.turned, fmt.Sprintf("%s %s", stream, marker))
return true, nil
}
func (r *ttlTurner) RaiseMaxBytes(string, int64) (int64, bool, error) { return 0, false, nil }
// A held bucket of a state declared key-ttl gets per-message time to live, once per manifest, and is said (design 47 H2
// H-A-501, corrected); a bucket of a state with no key-ttl is not touched.
func TestAHeldKeyTTLStateGetsPerMessageTTL(t *testing.T) {
m := catalogue.Manifest{Module: "mesh-delivery-indexer", State: []catalogue.StateDeclaration{{Name: "heard", KeyTTL: true}, {Name: "plain"}}}
a := Assertion{Manifest: "d", Items: Composed(m, nil)}
for i := range a.Items {
a.Items[i].Held = &Limits{}
}
r, done := &ttlTurner{}, &creations{}
made, _ := createMissing(r, done, &a, m, nil, false)
if want := []string{"KV_mesh-delivery-indexer_heard " + buscompose.KeyTTLMarkerTTL.String()}; !slices.Equal(r.turned, want) {
t.Fatalf("turned on %v, want %v", r.turned, want)
}
if !a.Asserted || !slices.ContainsFunc(made, func(s string) bool { return strings.Contains(s, "per-message time to live turned on") }) {
t.Errorf("asserted %v, said %v; want the turning on said", a.Asserted, made)
}
if createMissing(r, done, &a, m, nil, false); len(r.turned) != 1 {
t.Errorf("asked again at the same manifest: %v", r.turned)
}
}
// adder is a Creator that records the subjects it is asked to add to a held stream.
type adder struct {
Creator
add []string
}
func (a *adder) AddSubjects(_ string, add []string) error { a.add = add; return nil }
// A held subject the composed one covers is replaced by it, and the assertion says so (design 47 H H-A-470, third
// addendum: the bus refuses `registration.>` beside `registration.*`, JetStream 10052); no other subject is dropped.
func TestAHeldSubjectTheComposedOneCoversIsReplaced(t *testing.T) {
held, replaced := WidenSubjects([]string{"m.registration.*", "kept.subject"}, []string{"m.registration.>"})
if !slices.Equal(held, []string{"kept.subject", "m.registration.>"}) || !slices.Equal(replaced, []string{"m.registration.*"}) {
t.Errorf("widened to %v replacing %v", held, replaced)
}
if held, replaced := WidenSubjects([]string{"a.>"}, []string{"a.>", "b.*"}); !slices.Equal(held, []string{"a.>", "b.*"}) || replaced != nil {
t.Errorf("an equal or uncovered subject: %v replacing %v", held, replaced)
}
a := Assertion{Manifest: "d", Items: []Item{{Kind: "stream", Stream: "LOG_m_registration-history",
Composed: Limits{Subjects: []string{"m.registration.>"}}, Held: &Limits{Subjects: []string{"m.registration.*"}}}}}
c := &adder{}
made, _ := createMissing(c, &creations{}, &a, catalogue.Manifest{Module: "m"}, nil, false)
const said = "replacing m.registration.*, which it covers"
if !slices.Equal(c.add, []string{"m.registration.>"}) || len(made) != 1 || !strings.Contains(made[0], said) || !strings.Contains(a.Items[0].Created, said) {
t.Errorf("added %v, said %v and %q: want the replacement said", c.add, made, a.Items[0].Created)
}
}