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) } }