The delivery repository's contracts and SDK live under modules/ as mesh-delivery-contracts and mesh-delivery-sdk, their Go module paths renamed to match the folders (the TypeScript names already did), and every importer in mesh-delivery, mesh-catalog, mesh-host, mesh-tools and mesh-sdk follows in the same group: a design as it should be, one path per module, checked by the group's check building every importer. Builds: design 47 G §427, §88 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
206 lines
8.5 KiB
Go
206 lines
8.5 KiB
Go
package index
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
)
|
|
|
|
// Design 47 G §675 (Kept while named), §693 (a removed want): the state named holds, per fingerprint, the set of
|
|
// `<node>.<module>` whose want or running build names it.
|
|
|
|
var namedBase = time.Date(2026, 11, 11, 20, 0, 0, 0, time.UTC)
|
|
|
|
func namedFp(c string) string { return "src2:" + strings.Repeat(c, 64) }
|
|
|
|
type namedEvent struct {
|
|
wanted bool
|
|
node string
|
|
module string
|
|
fp string
|
|
min int
|
|
removed bool
|
|
stopped bool
|
|
}
|
|
|
|
func (e namedEvent) apply(t *testing.T, n *Named) {
|
|
t.Helper()
|
|
at := namedBase.Add(time.Duration(e.min) * time.Minute)
|
|
var data []byte
|
|
var err error
|
|
if e.wanted {
|
|
data, err = json.Marshal(meshwants.WantedEvent{V: 1, Node: e.node, Module: e.module, Fp: e.fp, Revision: 1, Removed: e.removed, At: &at})
|
|
if err == nil {
|
|
err = n.HearWanted(context.Background(), data)
|
|
}
|
|
} else {
|
|
data, err = json.Marshal(map[string]any{"v": 1, "node": e.node, "module": e.module, "fingerprint": e.fp, "stopped": e.stopped, "at": at})
|
|
if err == nil {
|
|
err = n.HearRunning(context.Background(), data)
|
|
}
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("%+v: %v", e, err)
|
|
}
|
|
}
|
|
|
|
func TestNamedHoldsTheMembersWhoseWantOrRunningBuildNamesAFingerprint(t *testing.T) {
|
|
a, b := namedFp("a"), namedFp("b")
|
|
for _, c := range []struct {
|
|
name string
|
|
events []namedEvent
|
|
want map[string][]string
|
|
}{
|
|
{"a want and a running build of one member name one fingerprint once", []namedEvent{
|
|
{wanted: true, node: "n1", module: "m", fp: a, min: 1}, {node: "n1", module: "m", fp: a, min: 2}}, map[string][]string{a: {"n1.m"}, b: nil}},
|
|
{"two nodes of a module are two members", []namedEvent{
|
|
{wanted: true, node: "n1", module: "m", fp: a, min: 1}, {wanted: true, node: "n2", module: "m", fp: a, min: 1}}, map[string][]string{a: {"n1.m", "n2.m"}}},
|
|
{"a want that moves on leaves the old fingerprint's set", []namedEvent{
|
|
{wanted: true, node: "n1", module: "m", fp: a, min: 1}, {wanted: true, node: "n1", module: "m", fp: b, min: 2}}, map[string][]string{a: nil, b: {"n1.m"}}},
|
|
{"a running build still naming the old one keeps it there", []namedEvent{
|
|
{wanted: true, node: "n1", module: "m", fp: a, min: 1}, {node: "n1", module: "m", fp: a, min: 1},
|
|
{wanted: true, node: "n1", module: "m", fp: b, min: 2}}, map[string][]string{a: {"n1.m"}, b: {"n1.m"}}},
|
|
{"a stopped running names nothing", []namedEvent{
|
|
{node: "n1", module: "m", fp: a, min: 1}, {node: "n1", module: "m", stopped: true, min: 2}}, map[string][]string{a: nil}},
|
|
{"a want removed empties the member whatever its node last said running (§693)", []namedEvent{
|
|
{wanted: true, node: "n1", module: "m", fp: a, min: 1}, {node: "n1", module: "m", fp: a, min: 1},
|
|
{wanted: true, node: "n1", module: "m", fp: a, min: 3, removed: true}}, map[string][]string{a: nil}},
|
|
{"a running older than the removal does not bring the member back", []namedEvent{
|
|
{wanted: true, node: "n1", module: "m", fp: a, min: 1, removed: true}, {node: "n1", module: "m", fp: a, min: 0}}, map[string][]string{a: nil}},
|
|
{"an event older than the one heard changes nothing", []namedEvent{
|
|
{wanted: true, node: "n1", module: "m", fp: b, min: 5}, {wanted: true, node: "n1", module: "m", fp: a, min: 4}}, map[string][]string{a: nil, b: {"n1.m"}}},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
st := newMem()
|
|
n := NewNamed(st, nil)
|
|
for _, e := range c.events {
|
|
e.apply(t, n)
|
|
}
|
|
for fp, want := range c.want {
|
|
got, err := n.Members(context.Background(), fp)
|
|
if err != nil || !slices.Equal(got, want) {
|
|
t.Errorf("the set of %.9s = %v, %v; want %v", fp, got, err, want)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// §675: an entry no kept revision lists is removed only when its set is empty, and not before the start's read of both streams
|
|
// ended; its entry-index key goes with it.
|
|
func TestAnEntryNoRevisionListsStaysWhileAWantNamesItAndGoesAfterTheWantMovesOn(t *testing.T) {
|
|
ctx := context.Background()
|
|
fp := namedFp("c")
|
|
n := NewNamed(newMem(), nil)
|
|
ix := &Indexer{Revisions: newMemObjects(1 << 20), EntryIndex: newMem(), NamedSets: n, Now: func() time.Time { return namedBase }}
|
|
n.Changed = ix.RemoveUnlisted
|
|
if err := ix.keepEntries(ctx, map[string]meshcatalog.CatalogEntry{"avahi": {Module: "avahi", Repository: "novox/r", Commit: "c", Fp: fp, Manifest: map[string]any{}}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := ix.Revisions.Delete(ctx, "trunk.1"); err != nil { // the revision that listed it is removed
|
|
t.Fatal(err)
|
|
}
|
|
found := func() bool {
|
|
_, err := Entry(ctx, ix.Revisions, ix.EntryIndex, meshcatalog.EntryInput{Fingerprint: fp})
|
|
return err == nil
|
|
}
|
|
|
|
ix.RemoveUnlisted(ctx)
|
|
if !found() {
|
|
t.Fatal("an entry was removed before the start's read of the wants and the running builds ended")
|
|
}
|
|
namedEvent{wanted: true, node: "n1", module: "avahi", fp: fp, min: 1}.apply(t, n)
|
|
if err := n.ReadDone(ctx, NamedWanted); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !found() {
|
|
t.Fatal("an entry was removed with one stream not read")
|
|
}
|
|
if err := n.ReadDone(ctx, NamedRunning); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !found() {
|
|
t.Fatal("an entry a want names was removed")
|
|
}
|
|
namedEvent{wanted: true, node: "n1", module: "avahi", fp: namedFp("d"), min: 2}.apply(t, n)
|
|
if found() {
|
|
t.Error("the entry stays after the want moved on")
|
|
}
|
|
if keys, _ := ix.EntryIndex.Keys(ctx); len(keys) != 0 {
|
|
t.Errorf("entry-index still holds %v for the removed entry", keys)
|
|
}
|
|
}
|
|
|
|
// §675: a restart reads named whole before it removes anything, and a member whose build moved while the indexer was away
|
|
// leaves its old set when both streams are read.
|
|
func TestARestartReconcilesTheSetsWithWhatTheStreamsHoldNow(t *testing.T) {
|
|
ctx := context.Background()
|
|
st := newMem()
|
|
old, now := namedFp("e"), namedFp("f")
|
|
before := NewNamed(st, nil)
|
|
namedEvent{wanted: true, node: "n1", module: "m", fp: old, min: 1}.apply(t, before)
|
|
namedEvent{wanted: true, node: "n2", module: "m", fp: old, min: 1}.apply(t, before)
|
|
|
|
after := NewNamed(st, nil) // the next start: the state kept, nothing in memory
|
|
namedEvent{wanted: true, node: "n1", module: "m", fp: now, min: 2}.apply(t, after)
|
|
if got, _ := after.Members(ctx, old); !slices.Equal(got, []string{"n1.m", "n2.m"}) {
|
|
t.Fatalf("a restart removed from %v before it read both streams", got)
|
|
}
|
|
namedEvent{wanted: true, node: "n2", module: "m", fp: old, min: 1}.apply(t, after)
|
|
_ = after.ReadDone(ctx, NamedWanted)
|
|
_ = after.ReadDone(ctx, NamedRunning)
|
|
if got, _ := after.Members(ctx, old); !slices.Equal(got, []string{"n2.m"}) {
|
|
t.Errorf("the old set holds %v after the read, want only the member still naming it", got)
|
|
}
|
|
if !after.Ready() {
|
|
t.Error("the sets are not ready after both streams were read")
|
|
}
|
|
}
|
|
|
|
// §675: indexer_named_drop removes one member by hand, says the hand act first, and indexer_entries reads the set.
|
|
func TestNamedDropRemovesAMemberByHandAndEntriesReadsTheSet(t *testing.T) {
|
|
ctx := context.Background()
|
|
fp := namedFp("9")
|
|
st := newMem()
|
|
n := NewNamed(st, nil)
|
|
namedEvent{wanted: true, node: "n1", module: "m", fp: fp, min: 1}.apply(t, n)
|
|
namedEvent{wanted: true, node: "n2", module: "m", fp: fp, min: 1}.apply(t, n)
|
|
ix := &Indexer{NamedSets: n, Logf: func(string, ...any) {}}
|
|
var acts []string
|
|
asked := func(key string, value []byte) (TakeInAsk, error) {
|
|
a, err := DecodeTakeIn(value)
|
|
if err != nil {
|
|
return a, err
|
|
}
|
|
r := ix.NamedDrop(ctx, a.NamedFingerprint, a.NamedMember, a.By, a.Why)
|
|
a.Result = &r
|
|
return a, nil
|
|
}
|
|
tool := NamedTools(asked, func() time.Time { return namedBase }, func(what, why string) error { acts = append(acts, what+" | "+why); return nil })[0]
|
|
if _, err := tool.Run(map[string]any{"fingerprint": fp, "member": "n1.m"}); err == nil {
|
|
t.Error("a drop with no why was asked")
|
|
}
|
|
if _, err := tool.Run(map[string]any{"fingerprint": fp, "member": "n1.m", "why": "its node is gone"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(acts) != 1 || !strings.Contains(acts[0], "n1.m") {
|
|
t.Errorf("the hand acts = %v, want the drop of n1.m said once", acts)
|
|
}
|
|
if got, _ := n.Members(ctx, fp); !slices.Equal(got, []string{"n2.m"}) {
|
|
t.Errorf("the set holds %v, want n2.m alone", got)
|
|
}
|
|
read, err := EntriesTool(newMemObjects(1<<10), newMem(), st).Run(map[string]any{"fingerprint": fp})
|
|
if err != nil || fmt.Sprint(read.(EntriesAnswer).Named) != "[n2.m]" {
|
|
t.Errorf("indexer_entries = %+v, %v, want the named set [n2.m]", read, err)
|
|
}
|
|
var _ stdio.Tool = tool
|
|
}
|