Files
mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/branches_test.go
T
jschoubben 8e50fab811 Item 1 (join 11): an entry the catalog holder says it holds none of is not-found, so a held want is passed again
A process's call gives the holder's own answer as its error (busretry.Answered); the entries kept by
fingerprint took that for a call that reached no holder and kept the entry asked, so §298's order edge
on the unread entry held the want for good and no pass ran again (the assigner's
TestWantsAreWrittenAndNoDifferenceIs, rule 7 "waits in place for the catalog entry").

Builds: design 47 G §614, §298, A-259, A-260

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

271 lines
9.1 KiB
Go

package meshcatalogclient
import (
"context"
"encoding/json"
"fmt"
"slices"
"strings"
"sync"
"testing"
"time"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/sourcebranches"
"git.novox.be/novox/mesh-sdk/go/busretry"
"git.novox.be/novox/mesh-sdk/go/views"
"github.com/nats-io/nats.go"
)
// fakeWiring is a part's reader: it keeps the views adopted, by `<owner>.<state>`, and says each drop.
type fakeWiring struct {
mu sync.Mutex
adopted map[string]*views.Follower
hears map[string]func(context.Context, []byte) error
dropped []string
}
func (w *fakeWiring) Adopt(f *views.Follower, _, _ string, hear func(context.Context, []byte) error) error {
w.mu.Lock()
defer w.mu.Unlock()
name := f.Owner + "." + f.State
if w.adopted[name] != nil {
return fmt.Errorf("%s is followed already", name)
}
w.adopted[name], w.hears[name] = f, hear
return nil
}
func (w *fakeWiring) Drop(owner, state string) error {
w.mu.Lock()
defer w.mu.Unlock()
name := owner + "." + state
if w.adopted[name] == nil {
return fmt.Errorf("%s is not followed", name)
}
delete(w.adopted, name)
delete(w.hears, name)
w.dropped = append(w.dropped, name)
return nil
}
func (w *fakeWiring) names() []string {
w.mu.Lock()
defer w.mu.Unlock()
out := []string{}
for n := range w.adopted {
out = append(out, n)
}
slices.Sort(out)
return out
}
func (w *fakeWiring) get(name string) (*views.Follower, func(context.Context, []byte) error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.adopted[name], w.hears[name]
}
// fakeSeats answers source, registrations, revision and entry from what it is told.
type fakeSeats struct {
mu sync.Mutex
source []string
regs map[string][]string // module -> branches
version int64
entries map[string]seat.CatalogEntry
entryAsk int
}
func (f *fakeSeats) call(key string, body any) (json.RawMessage, error) {
f.mu.Lock()
defer f.mu.Unlock()
switch key {
case sourcebranches.SourceVerb:
return json.Marshal(map[string][]string{"branches": f.source})
case RegistrationsVerb:
a := seat.RegistrationsAnswer{Registrations: []seat.Registration{}}
for m, br := range f.regs {
a.Registrations = append(a.Registrations, seat.Registration{Module: m, Version: f.version, Branches: br})
}
return json.Marshal(a)
case RevisionVerb:
return json.Marshal(seat.RevisionAnswer{NoneYet: true})
case EntryVerb:
f.entryAsk++
e, ok := f.entries[body.(seat.EntryInput).Fingerprint]
if !ok {
return json.RawMessage(`{"error":"no such entry"}`), nil
}
return json.Marshal(seat.EntryAnswer{Entry: e})
}
return nil, fmt.Errorf("asked %s", key)
}
// CT16 (design 47 G §614, A-5): the branch follower follows the gate's source branches (read at the start, then
// source-moved) before the first registration-changed, adopts a view per branch and drops the view of a branch that goes:
// the first registration-changed ends the gate's source, and the registrations' branches are the views then.
func TestBranchesFollowSourceThenRegistrations(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
said := make(chan string, 64)
waitFor := func(part string) {
t.Helper()
for {
select {
case s := <-said:
if strings.Contains(s, part) {
return
}
case <-ctx.Done():
t.Fatalf("never said %q", part)
}
}
}
seats := &fakeSeats{source: []string{"main"}, regs: map[string][]string{}, 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)
}
b.Logf = func(format string, args ...any) { said <- fmt.Sprintf(format, args...) }
adopted := make(chan string, 8)
b.Adopted = func(branch string, c *Catalog) {
if c.Branch == branch {
adopted <- branch
}
}
want := func(names ...string) {
t.Helper()
if got := w.names(); !slices.Equal(got, names) {
t.Fatalf("views = %v, want %v", got, names)
}
}
want("mesh-catalog.registrations", "mesh-delivery.source")
// before the first registration: the gate's source says main, and source-moved brings feat/x
src, hearSource := w.get("mesh-delivery.source")
if err := src.Start(ctx); err != nil {
t.Fatal(err)
}
want("mesh-catalog.catalog-revisions.main", "mesh-catalog.registrations", "mesh-delivery.source")
seats.mu.Lock()
seats.source = []string{"main", "feat/x"}
seats.mu.Unlock()
src.Wait = time.Nanosecond
if err := hearSource(ctx, []byte(`{"branch":"feat/x","revision":4}`)); err != nil {
t.Fatal(err)
}
waitFor(`"feat/x" is followed`)
told := []string{} // each adopted branch is told, with its view (a part waits for its first one's first view), in no set order
for range 2 {
select {
case got := <-adopted:
told = append(told, got)
case <-ctx.Done():
t.Fatalf("told of %v, want main and feat/x", told)
}
}
if slices.Sort(told); !slices.Equal(told, []string{"feat/x", "main"}) {
t.Fatalf("told of %v", told)
}
if got := b.Followed(); !slices.Equal(got, []string{"main", "feat/x"}) {
t.Fatalf("followed = %v", got)
}
// the registrations hold one branch, and the first registration-changed ends the source
seats.mu.Lock()
seats.regs["nats"] = []string{"main"}
seats.version = 2
seats.mu.Unlock()
reg, hearReg := w.get("mesh-catalog.registrations")
if err := reg.Start(ctx); err != nil {
t.Fatal(err)
}
if err := hearReg(ctx, []byte(`{"module":"nats","version":2,"moved":["branches"],"warrant":"a"}`)); err != nil {
t.Fatal(err)
}
waitFor(`"feat/x" is followed no more`)
want("mesh-catalog.catalog-revisions.main", "mesh-catalog.registrations")
if !slices.Contains(w.dropped, "mesh-delivery.source") {
t.Fatalf("the source is still followed: dropped %v", w.dropped)
}
// a branch the registrations add is adopted, one they remove is dropped
seats.mu.Lock()
seats.regs["nats"] = []string{"dev"}
seats.version = 3
seats.mu.Unlock()
if err := hearReg(ctx, []byte(`{"module":"nats","version":3,"moved":["branches"],"warrant":"b"}`)); err != nil {
t.Fatal(err)
}
waitFor(`"main" is followed no more`)
want("mesh-catalog.catalog-revisions.dev", "mesh-catalog.registrations")
}
// CT17 (design 47 G §614, An older build's entry): an entry no branch lists is asked of `entry` once and read as asked,
// then answered (the part is told, to start a pass); one the holder does not hold is not-found; a released one is
// asked again when next needed.
func TestEntriesAreAskedOnceAnsweredOrNotFound(t *testing.T) {
ctx := context.Background()
seats := &fakeSeats{entries: map[string]seat.CatalogEntry{"src2:a": {Module: "m", Fp: "src2:a"}}}
e := NewEntries(seats.call)
told := make(chan EntryRow, 4)
e.Changed = func(r EntryRow) { told <- r }
if _, st := e.Need(ctx, "src2:a"); st != EntryAsked {
t.Fatalf("state = %s, want asked", st)
}
if _, st := e.Need(ctx, "src2:a"); st != EntryAsked && st != EntryAnswered {
t.Fatalf("state = %s", st)
}
if r := <-told; r.State != EntryAnswered || r.Entry == nil || r.Entry.Module != "m" {
t.Fatalf("told %+v", r)
}
if got, st := e.Need(ctx, "src2:a"); st != EntryAnswered || got.Module != "m" {
t.Fatalf("kept entry = %s %s", st, got.Module)
}
if seats.entryAsk != 1 {
t.Fatalf("entry asked %d times, want once", seats.entryAsk)
}
e.Need(ctx, "src2:none")
if r := <-told; r.State != EntryNotFound || r.Fingerprint != "src2:none" || r.Detail == "" {
t.Fatalf("told %+v", r)
}
if rows := e.Rows(); len(rows) != 2 || rows[1].State != EntryNotFound {
t.Fatalf("rows %+v", rows)
}
e.Release("src2:none")
if rows := e.Rows(); len(rows) != 1 {
t.Fatalf("rows after release %+v", rows)
}
if err := e.Again(ctx, "src2:a"); err != nil || seats.entryAsk != 3 {
t.Fatalf("again: %v, asks %d", err, seats.entryAsk)
}
// A process's call gives the holder's answer that it holds none as its error (busretry.Answered): not-found too, and
// the part is told, so a pass waiting on it runs (design 47 G §614, §298); a call that reached no holder stays asked.
viaConn := NewEntries(func(string, any) (json.RawMessage, error) {
return nil, &busretry.Answered{Words: "no catalog entry has the fingerprint src2:old"}
})
toldConn := make(chan EntryRow, 2)
viaConn.Changed = func(r EntryRow) { toldConn <- r }
viaConn.Need(ctx, "src2:old")
if err := viaConn.Again(ctx, "src2:old"); err != nil {
t.Fatal(err)
}
if rows := viaConn.Rows(); len(rows) != 1 || rows[0].State != EntryNotFound || rows[0].Detail == "" || len(toldConn) == 0 {
t.Fatalf("the holder's answer through a process's call: rows %+v, told %d", rows, len(toldConn))
}
unreached := NewEntries(func(string, any) (json.RawMessage, error) { return nil, nats.ErrNoResponders })
unreached.Changed = func(r EntryRow) { t.Errorf("a call that reached no holder told %+v", r) }
if err := unreached.Again(ctx, "src2:x"); err == nil {
t.Fatal("again of an entry never needed answers no error")
}
unreached.Need(ctx, "src2:x")
if err := unreached.Again(ctx, "src2:x"); err != nil {
t.Fatal(err)
}
if rows := unreached.Rows(); len(rows) != 1 || rows[0].State != EntryAsked || rows[0].Detail == "" {
t.Fatalf("a call that reached no holder: rows %+v", rows)
}
}