The builder, its tools bundle, the assigner (process and bundle) and the view's tools bundle answered their seat list's again tool to the modules node-tools and mesh-healers. Their lists are now the operator, seat:node-tool-runner and seat:mesh-healers, resolved by a holder function: - meshcatalogclient's Branches.Claimant (moved from the builder's versions.HolderOf, which now asks it) names the claimant of mesh-module-assigner from the trunk's half, and Branches.SeatHolders follows that claimant's seat-holders view through the part's wiring. It drops the view and follows the new claimant's on a catalog change, asks nothing on a timer, and answers seatlist.HoldersIn. - the assigner answers HoldersIn from its own seat-holders picture (its world in the process, the watched copy in the bundle). The builder's and the view's manifests consume seat:mesh-module-assigner.views.seat-holders. The assigner's seat-holders first-view tool answers both, by module as A-131 has it. Builds: design 47 G A-298, A-295, A-296, A-210, A-131, §717, §616, §614 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
361 lines
13 KiB
Go
361 lines
13 KiB
Go
package meshcatalogclient
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"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"
|
|
)
|
|
|
|
// 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
|
|
// follows are the plain views followed (SeatHolders), values what each answers.
|
|
follows []string
|
|
values map[string]map[string]json.RawMessage
|
|
followed chan string
|
|
}
|
|
|
|
func (w *fakeWiring) Follow(owner, state, tool string) (func() map[string]json.RawMessage, error) {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
name := owner + "." + state
|
|
w.adopted[name] = &views.Follower{Owner: owner, State: state, Tool: tool}
|
|
w.follows = append(w.follows, tool)
|
|
if w.followed != nil {
|
|
w.followed <- owner
|
|
}
|
|
return func() map[string]json.RawMessage {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
return w.values[owner]
|
|
}, nil
|
|
}
|
|
|
|
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
|
|
// trunk, when set, is the revision answered for every branch.
|
|
trunk *seat.RevisionAnswer
|
|
}
|
|
|
|
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:
|
|
if f.trunk != nil {
|
|
return json.Marshal(f.trunk)
|
|
}
|
|
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))
|
|
}
|
|
// A call that reached no holder, in the words a runtime passes the bus's refusal on with (busretry.Undelivered): a client's
|
|
// test imports only its seat's contract and mesh-sdk's runtime (ADR 0625 §3), never the bus's client.
|
|
unreached := NewEntries(func(string, any) (json.RawMessage, error) {
|
|
return nil, errors.New("nats: no responders available for request")
|
|
})
|
|
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)
|
|
}
|
|
}
|
|
|
|
// A-298 (§717, A-295): the part's holder function follows the seat-holders view of the module the trunk's half names as the
|
|
// claimant of mesh-module-assigner, asked of its tool seat-holders; a revision naming another claimant drops it and follows the
|
|
// new one's; nothing is followed while no claimant is named.
|
|
func TestSeatHoldersFollowsTheClaimantOfTheAssignerSeat(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
claims := func(rev int64, assigner string) *seat.RevisionAnswer {
|
|
return &seat.RevisionAnswer{Revision: &seat.CatalogRevision{Kind: KindTrunk, Branch: "main", Revision: rev}, Entries: []seat.CatalogEntry{
|
|
{Module: assigner, ManifestDigest: assigner, Manifest: map[string]any{"module": assigner, "claims": []any{map[string]any{"name": "mesh-module-assigner", "scope": "mesh"}}}},
|
|
{Module: "app", ManifestDigest: "app", Manifest: map[string]any{"module": "app"}}}}
|
|
}
|
|
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{},
|
|
values: map[string]map[string]json.RawMessage{
|
|
"asg-a": {"node-tool-runner.ace": json.RawMessage(`{"holders":[{"module":"node-tools","node":"ace"}]}`)},
|
|
"asg-b": {"node-tool-runner.ace": json.RawMessage(`{"holders":[{"module":"tool-runner-2","node":"ace"}]}`)}},
|
|
followed: make(chan string, 4)}
|
|
b, err := NewBranches(w, seats.call)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
holders := b.SeatHolders(ctx)
|
|
src, _ := w.get("mesh-delivery.source")
|
|
if err := src.Start(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := holders("node-tool-runner"); len(got) != 0 {
|
|
t.Fatalf("holders before any claimant = %v", got)
|
|
}
|
|
// The follow runs on its own goroutine: each Follow is told, and the holder function answers once that follow is held.
|
|
answered := func(owner, want string) {
|
|
t.Helper()
|
|
if got := <-w.followed; got != owner {
|
|
t.Fatalf("followed %s, want %s", got, owner)
|
|
}
|
|
if got := holders("node-tool-runner"); !slices.Equal(got, []string{want}) {
|
|
t.Fatalf("holders = %v, want %s", got, want)
|
|
}
|
|
}
|
|
main, _ := w.get("mesh-catalog.catalog-revisions.main")
|
|
seats.mu.Lock()
|
|
seats.trunk = claims(1, "asg-a")
|
|
seats.mu.Unlock()
|
|
if err := main.Start(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
answered("asg-a", "node-tools")
|
|
seats.mu.Lock()
|
|
seats.trunk = claims(2, "asg-b")
|
|
seats.mu.Unlock()
|
|
if err := main.Again(ctx, "a revision naming another claimant"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
answered("asg-b", "tool-runner-2")
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
if !slices.Equal(w.follows, []string{"asg-a.seat-holders", "asg-b.seat-holders"}) || !slices.Equal(w.dropped, []string{"asg-a.seat-holders"}) {
|
|
t.Errorf("followed %v and dropped %v, want asg-a's then asg-b's, asg-a's dropped", w.follows, w.dropped)
|
|
}
|
|
}
|