Files
mesh-delivery/contracts/go/clients/meshcatalogclient/branches_test.go
T

225 lines
7.1 KiB
Go

package meshcatalogclient
import (
"context"
"encoding/json"
"fmt"
"slices"
"strings"
"sync"
"testing"
"time"
seat "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog"
delivery "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshdelivery"
"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
}
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 SourceVerb:
return json.Marshal(delivery.SourceAnswer{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...) }
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`)
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)
}
}