Files
mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/entry_test.go
T
jschoubben fc51bf4832
mesh/delivery ready: it is merged on the Approve
mesh/delivery-group group fix/47-G-start-2 checking: 2 of 2 member(s) ready
Round 2: grants says the absent stream in its status and pass result; tests that fail without A-372's settle call and without Ready's reading of the first views
Builds: design 47 G A-371 health clause, A-372, A-368 (G-22-lead-answers-2.md)

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-13 21:03:59 +01:00

175 lines
6.1 KiB
Go

package meshcatalogclient
import (
"context"
"sync"
"testing"
"time"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-sdk/go/keys"
"git.novox.be/novox/mesh-sdk/go/views"
)
// wireFindings is a part's finding writer in memory: the findings by `<kind>/<id>` it holds.
type wireFindings struct {
mu sync.Mutex
held map[string]bool
}
func (w *wireFindings) WriteWire(_ context.Context, f map[string]any) error {
w.mu.Lock()
defer w.mu.Unlock()
w.held[f["kind"].(string)+"/"+f["id"].(string)] = true
return nil
}
func (w *wireFindings) Clear(_ context.Context, _, id, kind, _ string) error {
w.mu.Lock()
defer w.mu.Unlock()
delete(w.held, kind+"/"+id)
return nil
}
func (w *wireFindings) ModuleNode() (string, string) { return "part", "n1" }
func (w *wireFindings) has(key string) bool {
w.mu.Lock()
defer w.mu.Unlock()
return w.held[key]
}
// A-103 (design 47 G §614): the helpers every part uses over the branch follower: Ready and Entry before a first view, the
// entries by fingerprint as `first_views` rows, the finding entry-not-found written and cleared, and `<module>_entry_again`.
func TestTheBranchFollowerGivesEveryPartItsEntryHelpers(t *testing.T) {
seats := &fakeSeats{entries: map[string]seat.CatalogEntry{"fp:a": {Module: "m", Fp: "fp:a"}}}
b, err := NewBranches(&fakeWiring{adopted: map[string]*views.Follower{}, hears: map[string]func(context.Context, []byte) error{}}, seats.call)
if err != nil {
t.Fatal(err)
}
if err := b.Ready(); err == nil {
t.Error("Ready said ready with no watched branch")
}
if _, _, err := b.Entry("m"); err == nil {
t.Error("Entry answered with no first view: that is an error, never no module")
}
told := make(chan EntryRow, 4)
w := &wireFindings{held: map[string]bool{}}
b.WriteEntryFindings(w, func(r EntryRow) { told <- r })
b.Entries.Need(context.Background(), "fp:none")
<-told
if !w.has(views.EntryNotFoundKind + "/" + keys.EscapeName("fp:none")) {
t.Errorf("entry-not-found not written: %v", w.held)
}
if st := b.EntryStatuses(); len(st) != 1 || st[0].State != EntryNotFound {
t.Errorf("entry statuses = %+v", st)
}
seats.mu.Lock()
seats.entries["fp:none"] = seat.CatalogEntry{Module: "late", Fp: "fp:none"}
seats.mu.Unlock()
var acts []string
tool := b.EntryAgainTool("part", func(what, why, cause string) error { acts = append(acts, what+"|"+why+"|"+cause); return nil })
if tool.Name != "part_entry_again" {
t.Errorf("tool name %s", tool.Name)
}
if _, err := tool.Run(map[string]any{"fingerprint": "fp:none"}); err == nil {
t.Error("entry_again ran without a why")
}
if _, err := tool.Run(map[string]any{"fingerprint": "fp:none", "why": "late"}); err != nil {
t.Fatal(err)
}
if len(acts) != 1 || w.has(views.EntryNotFoundKind+"/"+keys.EscapeName("fp:none")) {
t.Errorf("hand acts %v, findings %v", acts, w.held)
}
select {
case <-told:
case <-time.After(5 * time.Second):
t.Error("the part was not told of the answer")
}
b.Entries.Need(context.Background(), "fp:gone")
<-told
b.Entries.Release("fp:gone")
if w.has(views.EntryNotFoundKind + "/" + keys.EscapeName("fp:gone")) {
t.Error("a released entry kept its finding")
}
}
// A-368 (design 47 G, C6): on a fresh mesh, where neither the registrations nor the gate's source name a branch, the catalog half
// counts the trunk as `none-yet`: Ready is nil once both are answered, and the view is empty (Entry finds no module); until
// both answer, no part is ready.
func TestAMeshThatNamesNoBranchIsReadyWithAnEmptyView(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
for _, c := range []struct {
name string
start []string // the followers started, by the name they are adopted under
ready bool
}{
{"nothing answered", nil, false},
{"only the registrations answered", []string{"mesh-catalog.registrations"}, false},
{"only the gate's source answered", []string{"mesh-delivery.source"}, false},
{"both answered", []string{"mesh-catalog.registrations", "mesh-delivery.source"}, true},
} {
t.Run(c.name, func(t *testing.T) {
seats := &fakeSeats{regs: map[string][]string{}}
w := &fakeWiring{adopted: map[string]*views.Follower{}, hears: map[string]func(context.Context, []byte) error{}}
b, err := NewBranches(w, seats.call)
if err != nil {
t.Fatal(err)
}
for _, name := range c.start {
f, _ := w.get(name)
if err := f.Start(ctx); err != nil {
t.Fatal(err)
}
}
if err := b.Ready(); (err == nil) != c.ready {
t.Fatalf("Ready = %v, want ready %v", err, c.ready)
}
if c.ready {
if e, found, err := b.Entry("m"); err != nil || found {
t.Errorf("Entry = %v, %v, %v: want an empty view, no error", e, found, err)
}
}
})
}
}
// A-368: a first view closes before the follower's change reaches the branches followed, so a registration or a gate's source that
// names a branch is no mesh that names none, though Followed holds nothing yet.
func TestAFirstViewThatNamesABranchIsNotReadyBeforeItIsFollowed(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
for _, c := range []struct {
name string
source []string
regs map[string][]string
}{
{"a registration names main", nil, map[string][]string{"nats": {"main"}}},
{"the gate's source names main", []string{"main"}, map[string][]string{}},
} {
t.Run(c.name, func(t *testing.T) {
seats := &fakeSeats{source: c.source, regs: c.regs, version: 1}
w := &fakeWiring{adopted: map[string]*views.Follower{}, hears: map[string]func(context.Context, []byte) error{}}
b, err := NewBranches(w, seats.call)
if err != nil {
t.Fatal(err)
}
for _, name := range []string{"mesh-catalog.registrations", "mesh-delivery.source"} {
f, _ := w.get(name)
f.Changed = nil // the change is not yet taken into the branches followed
if err := f.Start(ctx); err != nil {
t.Fatal(err)
}
}
if len(b.Followed()) != 0 {
t.Fatalf("followed %v: the test means no branch is followed yet", b.Followed())
}
if err := b.Ready(); err == nil {
t.Error("Ready said ready with a branch named and not yet followed")
}
})
}
}