The timer was rearmed on a write of proposals or retiring only, so a rename window opened after start, with nothing else waiting on a time, left it blocked and its seven-day held finding never came; a window opened under a later armed deadline came late. The condition is now armsDeadline (proposals, retiring, renames), and a test shows a written window rearms it. The comment on Proposal.Pull cites ADR 0681 §5. Builds: ADR 0681 §1; lead answer A-490l (hq main, 03-DESIGN/01-to-be/47-milestones/G-22-lead-answers-2.md: the held finding comes in a quiet mesh too). Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
354 lines
13 KiB
Go
354 lines
13 KiB
Go
package main
|
|
|
|
// What mesh-config asks of the bus per input (issue 1982): its own states are read from the copy their watches keep, an event
|
|
// that moves nothing reads nothing, and the one timer left is a deadline.
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
|
|
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
)
|
|
|
|
// countingStore counts the reads of the states by state: what the bus would be asked.
|
|
type countingStore struct {
|
|
*memStore
|
|
mu sync.Mutex
|
|
reads map[string]int
|
|
}
|
|
|
|
func (s *countingStore) Get(state, key string) (*Entry, error) {
|
|
s.mu.Lock()
|
|
s.reads[state]++
|
|
s.mu.Unlock()
|
|
return s.memStore.Get(state, key)
|
|
}
|
|
|
|
func (s *countingStore) Keys(state string) ([]string, error) {
|
|
s.mu.Lock()
|
|
s.reads[state]++
|
|
s.mu.Unlock()
|
|
return s.memStore.Keys(state)
|
|
}
|
|
|
|
// readsOf is the reads of every state but those named.
|
|
func (s *countingStore) readsExcept(states ...string) map[string]int {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
out := map[string]int{}
|
|
for k, n := range s.reads {
|
|
out[k] = n
|
|
}
|
|
for _, k := range states {
|
|
delete(out, k)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// **An event that moves nothing reads nothing but what it judges** (design 32 *A config value renamed*, *A value no longer
|
|
// read*): with no rename window open, `delivered` and `manifest-reads-changed` read no source, and a `manifest-accepted`
|
|
// whose rows are all marked or read again reads none either. Each was a read of every key of every state of the module
|
|
// (about 4,000 requests) under a delivery's burst of events.
|
|
func TestAnEventThatMovesNothingReadsNoSource(t *testing.T) {
|
|
c, m, d, _, _, _ := retiringMesh(t)
|
|
cs := &countingStore{memStore: m, reads: map[string]int{}}
|
|
c.Store = cs
|
|
d.rows = []ReaderRow{{Module: "player", Key: "volume", Kind: KindValue, Readers: []string{"g14"}}} // read: nothing to retire
|
|
for name, event := range map[string]func(){
|
|
"delivered": func() { c.Windows("") },
|
|
"manifest-reads-changed": func() { c.Windows("g14") },
|
|
"manifest-accepted": func() { c.Retirements("player") },
|
|
} {
|
|
cs.reads = map[string]int{}
|
|
event()
|
|
if got := cs.readsExcept(RenamesState, RetiringState); len(got) != 0 {
|
|
t.Errorf("%s read %v", name, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
// An open window is judged, and reads what judging it needs: the same event with a window open reads the sources.
|
|
func TestAnEventWithAWindowOpenJudgesIt(t *testing.T) {
|
|
c, m, _, _ := aRenamingMesh(t)
|
|
m.declare(t, "player", newPlayer)
|
|
if _, err := c.Reconcile(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cs := &countingStore{memStore: m, reads: map[string]int{}}
|
|
c.Store = cs
|
|
c.Windows("")
|
|
if cs.reads[ValuesState] == 0 {
|
|
t.Errorf("an open window was judged on no source: %v", cs.reads)
|
|
}
|
|
}
|
|
|
|
// **An ask of a retirement that ends with no choice is taken when the router says so** (design 32 *A value no longer read*):
|
|
// with no minute's look at the router's record, the `decided` event of an ask that expired moves the retirement.
|
|
func TestARetirementWhoseAskExpiredIsTakenOnTheRoutersWord(t *testing.T) {
|
|
c, m, d, r, _, _ := retiringMesh(t)
|
|
for _, node := range []string{"g14", "novox"} {
|
|
m.want(t, node, "player", fp3, donePlayer)
|
|
d.haves[node+".player"] = fp3
|
|
}
|
|
c.Windows("")
|
|
c.Retirements("player")
|
|
rows := retiringOf(t, c)
|
|
if len(rows) != 1 || rows[0].State != RetireAsked {
|
|
t.Fatalf("retiring %+v, want asked", rows)
|
|
}
|
|
r.choose(t, rows[0].Ask, OptionApprove, func(w *oc.Warrant) { w.Outcome = occ.OutcomeExpired })
|
|
c.HeardEnd(oc.Warrant{Ask: rows[0].Ask, Asker: Module, Outcome: occ.OutcomeExpired})
|
|
if rows = retiringOf(t, c); rows[0].State != RetireUnanswered {
|
|
t.Errorf("after the router said the ask expired: %+v", rows)
|
|
}
|
|
}
|
|
|
|
// The next deadline is the earliest of a waiting proposal's (an hour past its ask lapsing, with no record at the router)
|
|
// and an unanswered retirement's (asked once more after HeldFinding); none when nothing waits on a time.
|
|
func TestTheNextDeadlineIsTheEarliestOfWhatWaitsOnATime(t *testing.T) {
|
|
m := newStore()
|
|
c := &Config{Store: m, Catalog: m, Logf: t.Logf}
|
|
if _, ok := c.NextDeadline(); ok {
|
|
t.Fatal("a deadline with nothing waiting")
|
|
}
|
|
t0 := time.Date(2026, 11, 19, 10, 0, 0, 0, time.UTC)
|
|
put := func(state, key string, v any) {
|
|
if err := m.Put(state, key, v); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
put(ProposalsState, "p1", Proposal{ID: "p1", State: ProposalWaiting, At: t0.Add(48 * time.Hour)})
|
|
put(ProposalsState, "p2", Proposal{ID: "p2", State: ProposalEnded, At: t0})
|
|
want := t0.Add(48*time.Hour + ProposalLasts + time.Hour)
|
|
if at, ok := c.NextDeadline(); !ok || !at.Equal(want) {
|
|
t.Errorf("deadline %v, %v; want %v", at, ok, want)
|
|
}
|
|
put(RetiringState, "player.volume", Retiring{Module: "player", Key: "volume", State: RetireUnanswered, Asked: t0.Add(-6 * 24 * time.Hour)})
|
|
want = t0.Add(HeldFinding - 6*24*time.Hour)
|
|
if at, ok := c.NextDeadline(); !ok || !at.Equal(want) {
|
|
t.Errorf("deadline %v, %v; want the retirement's %v", at, ok, want)
|
|
}
|
|
}
|
|
|
|
// busOf is a bus of states for the copy's tests: what each state holds, the watches handed out and the requests asked.
|
|
type busOf struct {
|
|
mu sync.Mutex
|
|
held map[string]map[string][]byte
|
|
handles map[string]func(stdio.StateChange) error
|
|
watches int
|
|
gets int
|
|
writes int
|
|
revision uint64
|
|
}
|
|
|
|
func (b *busOf) ownCopy() *ownCopy {
|
|
return &ownCopy{
|
|
copy: stdio.WatchedStatesOver(func(state string, handle func(stdio.StateChange) error) error {
|
|
b.mu.Lock()
|
|
b.watches++
|
|
b.handles[state] = handle
|
|
current := map[string][]byte{}
|
|
for k, v := range b.held[state] {
|
|
current[k] = v
|
|
}
|
|
b.mu.Unlock()
|
|
for k, v := range current {
|
|
if err := handle(stdio.StateChange{State: state, Key: k, Op: stdio.OpPut, Value: v, Revision: 1, Current: true}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}, func(state, key string) ([]byte, bool, error) {
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
b.gets++
|
|
v, ok := b.held[state][key]
|
|
return v, ok, nil
|
|
}),
|
|
put: func(state, key string, value any) (uint64, error) {
|
|
raw, _ := json.Marshal(value)
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
b.writes++
|
|
b.revision++
|
|
if b.held[state] == nil {
|
|
b.held[state] = map[string][]byte{}
|
|
}
|
|
b.held[state][key] = raw
|
|
return b.revision + 1, nil
|
|
},
|
|
del: func(state, key string) error {
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
b.writes++
|
|
delete(b.held[state], key)
|
|
return nil
|
|
}}
|
|
}
|
|
|
|
// **The module's own states are read from a copy, never key by key** (ADR 0580 §1, issue 1982): a whole state read of 1,176
|
|
// keys is one watch and no get, again and again; this process's write is in the copy at once, another's arrives by the watch.
|
|
func TestAnOwnStateIsReadFromItsWatchedCopyNotKeyByKey(t *testing.T) {
|
|
b := &busOf{held: map[string]map[string][]byte{ValuesState: {}}, handles: map[string]func(stdio.StateChange) error{}}
|
|
for i := 0; i < 1176; i++ {
|
|
b.held[ValuesState]["mesh.key-"+time.Duration(i).String()] = []byte(`{"value":1}`)
|
|
}
|
|
n := len(b.held[ValuesState])
|
|
own := b.ownCopy()
|
|
for pass := 0; pass < 3; pass++ {
|
|
keys, err := own.keys(ValuesState)
|
|
if err != nil || len(keys) != n {
|
|
t.Fatalf("keys %d, %v; want %d", len(keys), err, n)
|
|
}
|
|
for _, k := range keys {
|
|
if e, err := own.get(ValuesState, k); err != nil || e == nil {
|
|
t.Fatalf("get %s: %v, %v", k, e, err)
|
|
}
|
|
}
|
|
}
|
|
if b.watches != 1 || b.gets != 0 {
|
|
t.Errorf("3 passes over %d keys asked the bus for %d watches and %d gets; want 1 and 0", n, b.watches, b.gets)
|
|
}
|
|
// Its own write and removal are in the copy at once.
|
|
if err := own.write(ValuesState, "mine", map[string]string{"x": "mine"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if e, _ := own.get(ValuesState, "mine"); e == nil {
|
|
t.Error("a key just written is not read")
|
|
}
|
|
if err := own.remove(ValuesState, "mine"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if e, _ := own.get(ValuesState, "mine"); e != nil {
|
|
t.Error("a key just removed is still read")
|
|
}
|
|
// Another writer's change arrives by the watch.
|
|
if err := b.handles[ValuesState](stdio.StateChange{State: ValuesState, Key: "theirs", Op: stdio.OpPut, Value: []byte(`{"value":2}`), Revision: 99}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if e, _ := own.get(ValuesState, "theirs"); e == nil {
|
|
t.Error("a change another wrote is not read")
|
|
}
|
|
if b.gets != 0 {
|
|
t.Errorf("%d gets", b.gets)
|
|
}
|
|
}
|
|
|
|
// A write and a removal tell what waits on a deadline, once each.
|
|
func TestAWriteTellsWhatWaitsOnADeadline(t *testing.T) {
|
|
b := &busOf{held: map[string]map[string][]byte{}, handles: map[string]func(stdio.StateChange) error{}}
|
|
own := b.ownCopy()
|
|
var told []string
|
|
own.changed = func(state string) { told = append(told, state) }
|
|
if err := own.write(ProposalsState, "p", Proposal{ID: "p"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := own.remove(ProposalsState, "p"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(told) != 2 || told[0] != ProposalsState {
|
|
t.Errorf("told %v", told)
|
|
}
|
|
}
|
|
|
|
// The mesh knows no pull request (design 47 G; ADR 0502 ended by the operator's word, record pending): the verb takes no `pull`,
|
|
// `head`, `declares` or `pulls`, and does neither `pull-declares` nor `held`.
|
|
func TestTheVerbKnowsNoPullRequest(t *testing.T) {
|
|
for _, arg := range []string{"pull", "head", "declares", "pulls"} {
|
|
if _, err := callOf(map[string]any{"action": "propose", arg: "x"}); err == nil || !strings.Contains(err.Error(), arg) {
|
|
t.Errorf("an argument %s was taken: %v", arg, err)
|
|
}
|
|
}
|
|
c, _ := aMesh(t)
|
|
for _, action := range []string{"pull-declares", "held"} {
|
|
if _, err := c.Do(Call{Action: action}); err == nil || !strings.Contains(err.Error(), action) {
|
|
t.Errorf("the action %s was done: %v", action, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The deadline timer's floor is the setting deadline-floor-seconds, the default for a value that is not a positive number.
|
|
func TestTheDeadlineFloorIsTheSettingOrItsDefault(t *testing.T) {
|
|
for _, c := range []struct {
|
|
raw string
|
|
err error
|
|
want time.Duration
|
|
}{
|
|
{`5`, nil, 5 * time.Second}, {`0`, nil, DefaultDeadlineFloor}, {`"x"`, nil, DefaultDeadlineFloor},
|
|
{``, errors.New("not declared"), DefaultDeadlineFloor}, {`90`, nil, 90 * time.Second},
|
|
} {
|
|
if got := floorOf(json.RawMessage(c.raw), c.err); got != c.want {
|
|
t.Errorf("floor of %q (%v) = %v, want %v", c.raw, c.err, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// **A rename window held seven days comes as a finding in a quiet mesh** (lead answer A-490l): the deadline is the window's
|
|
// seventh day, the finding is written at it with no resolution pass, and a window that is not yet seven days old writes none.
|
|
func TestAHeldWindowIsAFindingAtItsDeadlineWithNoPass(t *testing.T) {
|
|
c, m, _, clock := aRenamingMesh(t)
|
|
m.declare(t, "player", newPlayer)
|
|
if _, err := c.Reconcile(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
at, ok := c.NextDeadline()
|
|
if !ok || !at.Equal(clock.Add(HeldFinding)) {
|
|
t.Fatalf("deadline %v, %v; want the window's seventh day %v", at, ok, clock.Add(HeldFinding))
|
|
}
|
|
if err := c.JudgeHeldWindows(); err != nil || len(findingsOf(t, m, FindingWindowHeld)) != 0 {
|
|
t.Fatalf("a young window wrote a finding (%v)", err)
|
|
}
|
|
*clock = at
|
|
cs := &countingStore{memStore: m, reads: map[string]int{}}
|
|
c.Store = cs
|
|
if err := c.JudgeHeldWindows(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if fs := findingsOf(t, m, FindingWindowHeld); len(fs) != 2 {
|
|
t.Errorf("findings %+v, want one for each node holding the window", fs)
|
|
}
|
|
if got := cs.readsExcept(RenamesState); len(got) != 0 {
|
|
t.Errorf("judging the held windows read %v", got)
|
|
}
|
|
if _, ok := c.NextDeadline(); ok {
|
|
t.Error("a deadline is still set once the finding is written")
|
|
}
|
|
}
|
|
|
|
// A write of a proposal, a retirement or a rename window sets the deadline timer again, and no other state does: a window opened
|
|
// after start, with nothing else waiting on a time, must not leave the timer blocked (A-490l).
|
|
func TestAWriteOfARenameWindowRearmsTheDeadlineTimer(t *testing.T) {
|
|
for state, want := range map[string]bool{ProposalsState: true, RetiringState: true, RenamesState: true,
|
|
ValuesState: false, ResolvedState: false, DeclaredState: false} {
|
|
if got := armsDeadline(state); got != want {
|
|
t.Errorf("a write of %s arms the timer: %v, want %v", state, got, want)
|
|
}
|
|
}
|
|
// Wired as main wires it: the copy's write of a window tells the channel the timer waits on.
|
|
b := &busOf{held: map[string]map[string][]byte{}, handles: map[string]func(stdio.StateChange) error{}}
|
|
own := b.ownCopy()
|
|
rearm := make(chan struct{}, 1)
|
|
own.changed = func(state string) {
|
|
if armsDeadline(state) {
|
|
select {
|
|
case rearm <- struct{}{}:
|
|
default:
|
|
}
|
|
}
|
|
}
|
|
if err := own.write(RenamesState, "player.level", Rename{Module: "player"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
select {
|
|
case <-rearm:
|
|
default:
|
|
t.Error("a rename window written after start did not rearm the timer")
|
|
}
|
|
}
|