Files
mesh-delivery/modules/mesh-grants/internal/grants/loadedusers_size_test.go
T
jschoubben 21b53f5dfb mesh-grants: read the loaded list one user per answer, so no answer exceeds one bus message
lead: principle 7, live hold 02:0x (nats_users for the whole list was 1086642 bytes, over 1048576).
Builds: design 47 G, lead's handover (no design line beyond principle 7)

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-14 02:01:49 +01:00

62 lines
1.7 KiB
Go

package grants
import (
"encoding/json"
"errors"
"fmt"
"strings"
"testing"
)
// sizeBus answers nats_users as the bus module does, and refuses an answer over one bus message.
type sizeBus struct {
users []User
limit int
}
func (b sizeBus) Call(key string, body any) (json.RawMessage, error) {
part, _ := body.(map[string]any)["user"].(string)
if part == "" {
var names []map[string]any
for _, u := range b.users {
names = append(names, map[string]any{"user": u.User, "publish_grants": len(u.Publish)})
}
return json.Marshal(map[string]any{"users": names})
}
var out []User
for _, u := range b.users {
if strings.Contains(u.User, part) {
out = append(out, u)
}
}
raw, _ := json.Marshal(map[string]any{"users": out})
if len(raw) > b.limit {
return nil, errors.New("the answer is more than the bus carries in one message: ask for less")
}
return raw, nil
}
// lead: principle 7, live hold 02:0x: the loaded list is read one user per answer, never the whole list in one.
func TestLoadedUsersReadsOneUserPerAnswer(t *testing.T) {
var users []User
for i := 0; i < 20; i++ {
var pub []string
for j := 0; j < 50; j++ {
pub = append(pub, fmt.Sprintf("subject.%d.%d.>", i, j))
}
users = append(users, User{User: fmt.Sprintf("mod.u%02d", i), Publish: pub, Subscribe: []string{"x.>"}})
}
users = append(users, User{User: "controller", Publish: []string{"a"}})
got, err := LoadedUsers(sizeBus{users: users, limit: 5000})
if err != nil {
t.Fatal(err)
}
if len(got) != len(users) {
t.Fatalf("read %d users, want %d", len(got), len(users))
}
// a user whose own answer is too large stays a loud hold
if _, err := LoadedUsers(sizeBus{users: users, limit: 100}); err == nil {
t.Fatal("an unreadable user must hold loudly")
}
}