Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/registrations_tools_test.go
T
jschoubben cc5f803f87 I5: the registrations are read in one call, filtered and paged
The verb registrations and the new tool indexer_registrations filter by
module and repository while they read, at most 768 KB a page, capped with
the cursor next: the tools bundle reads the state from the bus's cursor
(mesh/state.page), a store that pages none by its keys in order; total is
said when one page holds the whole read.

Builds: design 47 H2 §806, §811, §779
2026-11-22 01:12:04 +01:00

129 lines
5.0 KiB
Go

package index
import (
"context"
"encoding/json"
"errors"
"fmt"
"slices"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
stdio "git.novox.be/novox/mesh-sdk/go"
)
// pagedStore is a store whose Page answers the pages given, by cursor, and records the cursors asked.
type pagedStore struct {
Store
pages map[string]stdio.StatePage
asked []string
}
func (p *pagedStore) Page(cursor string, _ int, _ time.Duration) (stdio.StatePage, error) {
p.asked = append(p.asked, cursor)
return p.pages[cursor], nil
}
// Design 47 G A-370: the verb registrations answers from the state at once; it never asks the indexer (whose take-in may be
// running), but for a module's history, which the indexer reads from its stream.
func TestTheRegistrationsVerbIsAnsweredFromItsStateNotAskedOfTheIndexer(t *testing.T) {
store := MemoryStore()
data, _ := json.Marshal(meshcatalog.Registration{Module: "a"})
if err := store.Create(context.Background(), RegistrationKey("a"), data); err != nil {
t.Fatal(err)
}
asked := 0
ask := func(string, []byte) (TakeInAsk, error) {
asked++
return TakeInAsk{}, errors.New("the take-in is still running")
}
var tool func(map[string]any) (any, error)
for _, tl := range RegistrationTools(store, MemoryStore(), ask, time.Now, nil) {
if tl.Name == meshcatalog.Seat+"."+meshcatalog.VerbRegistrations {
tool = tl.Run
}
}
for _, args := range []map[string]any{{}, {"module": "a"}} {
got, err := tool(args)
if err != nil || len(got.(meshcatalog.RegistrationsAnswer).Registrations) != 1 {
t.Errorf("registrations %v = %+v, %v, want the one registration", args, got, err)
}
}
if asked != 0 {
t.Errorf("the indexer was asked %d times for a read of the state", asked)
}
if _, err := tool(map[string]any{"module": "a", "history": true}); err == nil || asked != 1 {
t.Errorf("a history is asked of the indexer: err %v, asked %d", err, asked)
}
// Design 47 H2 §806: indexer_registrations answers as the verb, filtered by module and repository while it reads, at most
// 768 KB a page, capped with the cursor next; a state that pages is read from the bus's cursor.
t.Run("filtered and paged", func(t *testing.T) {
ctx := context.Background()
big := MemoryStore()
var want []string
for i := range 12 {
origin := "novox/mesh-catalog"
if i%3 == 0 {
origin = "novox/mesh-sdk"
}
module := fmt.Sprintf("m%02d", i)
data, _ := json.Marshal(meshcatalog.Registration{Module: module, Origin: origin, Path: strings.Repeat("p", 100<<10)})
if err := big.Create(ctx, RegistrationKey(module), data); err != nil {
t.Fatal(err)
}
if origin == "novox/mesh-catalog" {
want = append(want, module)
}
}
named := func(tools []stdio.Tool) func(map[string]any) (any, error) {
for _, tl := range tools {
if tl.Name == RegistrationsTool {
return tl.Run
}
}
t.Fatalf("no tool %s", RegistrationsTool)
return nil
}
run := named(RegistrationTools(big, MemoryStore(), ask, time.Now, nil))
var got []string
pages := 0
for next := ""; ; pages++ {
raw, err := run(map[string]any{"repository": "novox/mesh-catalog", "next": next})
a, _ := raw.(meshcatalog.RegistrationsAnswer)
if err != nil || a.Capped != (a.Next != "") {
t.Fatalf("page %d = %+v (%v), want capped exactly when a cursor follows", pages, a, err)
}
for _, r := range a.Registrations {
got = append(got, r.Module)
}
if next = a.Next; next == "" {
break
}
}
if !slices.Equal(got, want) || pages == 0 {
t.Errorf("the pages hold %v in %d pages after the first, want %v over more than one page", got, pages, want)
}
if a, _ := run(map[string]any{"module": "m00", "repository": "novox/mesh-catalog"}); len(a.(meshcatalog.RegistrationsAnswer).Registrations) != 0 {
t.Errorf("m00 of novox/mesh-sdk answered for the repository novox/mesh-catalog: %+v", a)
}
reg := func(module, origin string) stdio.StateEntry {
data, _ := json.Marshal(meshcatalog.Registration{Module: module, Origin: origin})
return stdio.StateEntry{Key: RegistrationKey(module), Value: data}
}
pager := &pagedStore{Store: MemoryStore(), pages: map[string]stdio.StatePage{
"": {Entries: []stdio.StateEntry{reg("a", "novox/x"), reg("b", "novox/y")}, Next: "c1"},
"c1": {Entries: []stdio.StateEntry{reg("c", "novox/x")}}}}
run = named(RegistrationTools(pager, MemoryStore(), ask, time.Now, nil))
first, _ := run(map[string]any{"repository": "novox/x"})
second, _ := run(map[string]any{"repository": "novox/x", "next": "c1"})
f, g := first.(meshcatalog.RegistrationsAnswer), second.(meshcatalog.RegistrationsAnswer)
if len(f.Registrations) != 1 || f.Registrations[0].Module != "a" || f.Next != "c1" || !f.Capped ||
len(g.Registrations) != 1 || g.Registrations[0].Module != "c" || g.Next != "" || !slices.Equal(pager.asked, []string{"", "c1"}) {
t.Errorf("the paged read answered %+v then %+v, asking %v, want a then c of novox/x by the bus's cursor", f, g, pager.asked)
}
})
}