Files
mesh-delivery/modules/mesh-grants/internal/grants/statecap_test.go
T
jschoubben abaddd3ef8 Grants: a held subject the composed one covers is replaced by it (H-A-470 (4))
The live registration history is held with `….registration.*`; the bus
refuses adding `….registration.>` beside it (JetStream 10052, overlapping
subjects), so df5a8895's subject never reached the live stream. A held
subject a composed one covers is now replaced by it, the messages kept and
no other subject dropped, and the assertion says the replacement.

Builds: design 47 H H-A-470 (third addendum)
2026-11-22 13:04:06 +01:00

71 lines
2.9 KiB
Go

package grants
import (
"slices"
"strings"
"testing"
"git.novox.be/novox/mesh-sdk/go/catalogue"
)
// 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)
}
}
// 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)
}
}