Files
mesh-delivery/modules/mesh-delivery-teller/cmd/mesh-delivery-teller/widens_test.go
T
jschoubben b79fd19c84 Teller: hear registration-changed on the views reader's one subscription
Since mesh-sdk heard a seat's event on its bare subject too (H-A-166), the teller's
own hub handler of registration-changed overlapped the one the views reader makes
for the catalog half's followers: the hub refused the reader's, said "a message has
one taker", and registered none of the reader's adopted events after it, so the
catalog half's registrations and source branches were their first views' alone.
The widenings now hear the event through ProcessReader.AdoptEvent beside the
followers; a failed handling is given back for the bus to ask again, as before.
The carry test now fails when any of the reader's events is refused.

issue 1960 (H1), test stability.

Builds: design 47 G A-199, A-222, §532, §335
2026-11-21 19:24:22 +01:00

217 lines
8.0 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"slices"
"sort"
"strings"
"testing"
"github.com/nats-io/nats.go/jetstream"
mpr "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
stdio "git.novox.be/novox/mesh-sdk/go"
)
// widensState is memStore with the delete and the keys the state widens-said needs.
type widensMem struct{ memStore }
func (m *widensMem) Create(key string, value any) (uint64, error) {
if _, ok := m.entries[key]; ok {
return 0, stdio.ErrChanged
}
return m.put(key, value)
}
func (m *widensMem) Update(key string, value any, rev uint64) (uint64, error) {
if e, ok := m.entries[key]; !ok || e.Revision != rev {
return 0, stdio.ErrChanged
}
return m.put(key, value)
}
func (m *widensMem) Delete(key string) error { delete(m.entries, key); return nil }
func (m *widensMem) Keys() ([]string, error) {
var out []string
for k := range m.entries {
out = append(out, k)
}
sort.Strings(out)
return out, nil
}
// calls records the seat verbs the teller calls and answers the registrations.
type calls struct {
notified []map[string]any
fail error
branches []string
}
func (c *calls) call(key string, body any) (json.RawMessage, error) {
switch key {
case notifyKey:
if c.fail != nil {
return nil, c.fail
}
c.notified = append(c.notified, body.(map[string]any))
return json.RawMessage(`{}`), nil
case registrationsKey:
return json.Marshal(map[string]any{"registrations": []map[string]any{{"module": "nats", "branches": c.branches}}})
}
return nil, errors.New("not called: " + key)
}
func widenEnd(commit string, what ...string) mpr.PipelineEnded {
return mpr.PipelineEnded{Branch: "main", Movers: []mpr.MoverEnded{widenMover("nats", commit, what...)}}
}
// widenMover is a mover at its pipeline's end, its commit and its widens (H-A-119).
func widenMover(module, commit string, what ...string) mpr.MoverEnded {
m := mpr.MoverEnded{Module: module, Commit: commit}
for _, w := range what {
m.Widens = append(m.Widens, mpr.Widening{What: w, Name: "name-of-" + strings.ReplaceAll(w, " ", "-")})
}
return m
}
// Design 47 G §71, §335, §522 (TL1): a pipeline's widening is said once on every channel, in the operator's words.
func TestAWideningIsSaidOnceOnEveryChannelAndKept(t *testing.T) {
c := &calls{}
w := &Widens{State: &widensMem{memStore{entries: map[string]*stdio.StateEntry{}}}, Call: c.call}
end := widenEnd("0123456789abcdef0123456789abcdef01234567", "grant", "serving part")
steps := []struct {
name string
end mpr.PipelineEnded
want int // messages said so far
}{
{"the first end says one message", end, 1},
{"the same end again says nothing", end, 1},
{"an end with no widening says nothing", widenEnd("fffffffffffffffffffffffffffffffffffffff"), 1},
{"the next commit says one more", widenEnd("89abcdef0123456789abcdef0123456789abcdef", "credential"), 2},
// H-A-119: one message per mover that widens, each mover once for its commit.
{"two movers say one message for the one not said yet", mpr.PipelineEnded{Branch: "main", Movers: []mpr.MoverEnded{
widenMover("nats", "89abcdef0123456789abcdef0123456789abcdef", "credential"),
widenMover("mesh-sdk", "0123456789abcdef0123456789abcdef01234567", "acting rule")}}, 3},
}
for _, s := range steps {
if err := w.Said(s.end); err != nil {
t.Fatalf("%s: %v", s.name, err)
}
if len(c.notified) != s.want {
t.Fatalf("%s: %d messages, want %d", s.name, len(c.notified), s.want)
}
}
first := c.notified[0]
if first["every-channel"] != true {
t.Errorf("the message is not for every channel: %v", first)
}
wantWords := "Widening: nats 0123456 on main widens grant name-of-grant.\nWidening: nats 0123456 on main widens serving part name-of-serving-part."
if first["headline"] != wantWords {
t.Errorf("the words are %q, want %q", first["headline"], wantWords)
}
if k := first["key"]; k != "widens.nats.main.0123456" {
t.Errorf("the key is %v", k)
}
if e, _ := w.State.Get("nats.main"); e == nil || string(e.Value) != `"89abcdef0123456789abcdef0123456789abcdef"` {
t.Errorf("the state holds %v, want the last commit said", e)
}
}
// A message that is not told keeps nothing, so the end is heard again and told.
func TestAWideningNotToldIsNotKept(t *testing.T) {
c := &calls{fail: errors.New("no channel")}
st := &widensMem{memStore{entries: map[string]*stdio.StateEntry{}}}
w := &Widens{State: st, Call: c.call}
end := widenEnd("0123456789abcdef0123456789abcdef01234567", "grant")
if err := w.Said(end); err == nil {
t.Fatal("a say that failed returned no error")
}
if len(st.entries) != 0 {
t.Fatalf("kept %v though it was not told", st.entries)
}
c.fail = nil
if err := w.Said(end); err != nil || len(c.notified) != 1 {
t.Fatalf("heard again: %v, %d messages", err, len(c.notified))
}
}
// fakeMsg is a jetstream message of a subject and a body that records how it was answered.
type fakeMsg struct {
jetstream.Msg
data []byte
acked bool
naked bool
}
func (f *fakeMsg) Data() []byte { return f.data }
func (f *fakeMsg) Subject() string { return "s" }
func (f *fakeMsg) Ack() error { f.acked = true; return nil }
func (f *fakeMsg) Nak() error { f.naked = true; return nil }
// A registration-changed drops the keys of the branches the registration no longer watches, and only of its module.
func TestARegistrationChangedDropsTheBranchesNoLongerWatched(t *testing.T) {
c := &calls{branches: []string{"main"}}
st := &widensMem{memStore{entries: map[string]*stdio.StateEntry{}}}
for _, k := range []string{"nats.main", "nats.next", "natsx.next", "other.next"} {
st.put(k, "c")
}
w := &Widens{State: st, Call: c.call}
if err := w.HearRegistration(context.Background(), []byte(`{"module":"nats","moved":[],"version":2,"warrant":"w"}`)); err != nil {
t.Errorf("the registration-changed is given back: %v", err)
}
keys, _ := st.Keys()
if want := []string{"nats.main", "natsx.next", "other.next"}; !slices.Equal(keys, want) {
t.Errorf("keys %v, want %v", keys, want)
}
}
// A pipeline-ended whose say fails is given back, and one that is told is acknowledged.
func TestAPipelineEndedIsAcknowledgedOnlyWhenTold(t *testing.T) {
c := &calls{fail: errors.New("no channel")}
w := &Widens{State: &widensMem{memStore{entries: map[string]*stdio.StateEntry{}}}, Call: c.call}
body, _ := json.Marshal(widenEnd("0123456789abcdef0123456789abcdef01234567", "grant"))
m := &fakeMsg{data: body}
w.HearEnded(m)
if m.acked || !m.naked {
t.Errorf("a failed say: acked %v naked %v", m.acked, m.naked)
}
c.fail = nil
m = &fakeMsg{data: body}
w.HearEnded(m)
if !m.acked || m.naked {
t.Errorf("a told say: acked %v naked %v", m.acked, m.naked)
}
}
// Design 47 G §81, §522 (TL2): a recovery is told on every channel in the operator's words, with its fields filled; a merge
// made at the forge is no record of its own (H §763, H-A-119).
func TestARecoveryAndAForgeMadeMergeAreToldInTheOperatorsWords(t *testing.T) {
commit := "0123456789abcdef0123456789abcdef01234567"
for _, tc := range []struct {
name, body string
hear func(*Widens, *fakeMsg)
key, words string
}{
{"a recovery", `{"v":1,"repository":"novox/mesh-sdk","module":"mesh-sdk","branch":"main","commit":"` + commit + `","why":"the live delivery was broken","by":"jochen"}`,
func(w *Widens, m *fakeMsg) { w.HearRecovery(m) }, "recovery.novox/mesh-sdk.0123456",
"Recovery merged: mesh-sdk 0123456 on main, named by jochen: the live delivery was broken."},
} {
c := &calls{}
w := &Widens{State: &widensMem{memStore{entries: map[string]*stdio.StateEntry{}}}, Call: c.call}
m := &fakeMsg{data: []byte(tc.body)}
tc.hear(w, m)
if !m.acked || m.naked {
t.Errorf("%s: acked %v naked %v", tc.name, m.acked, m.naked)
}
if tc.words == "" {
if len(c.notified) != 0 {
t.Errorf("%s: told %v", tc.name, c.notified)
}
continue
}
if len(c.notified) != 1 || c.notified[0]["headline"] != tc.words || c.notified[0]["key"] != tc.key || c.notified[0]["every-channel"] != true {
t.Errorf("%s: told %v, want %q under %s on every channel", tc.name, c.notified, tc.words, tc.key)
}
}
}