The node-engine gives the watcher's process no MESH_NODE, so the watcher refused to start and crash-looped on g14. It needs none: a kept change holds its event, its sequence and its body, and the bundle fills its own node into the subject, the message id <node>.<event>.<sequence> and the body when it publishes, the same at every publish. Builds: design 08 sign-in-page §11, §12, §20; lead answer L-3 (hq 09c9a3e3)
162 lines
6.3 KiB
Go
162 lines
6.3 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/nodeuplink"
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
)
|
|
|
|
// The bus stood in: every publish asked, the ids it refuses once, as the runtime answers a publish the stream did not
|
|
// acknowledge.
|
|
type fakeStream struct {
|
|
mu sync.Mutex
|
|
asked []string
|
|
refuse map[string]bool
|
|
}
|
|
|
|
func (s *fakeStream) publish(subject string, body json.RawMessage, id string) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.asked = append(s.asked, id)
|
|
if s.refuse[id] {
|
|
delete(s.refuse, id)
|
|
return errors.New("no acknowledgement")
|
|
}
|
|
if !json.Valid(body) || !strings.HasPrefix(subject, "mesh.seat.node-uplink.event.") {
|
|
return errors.New("not an event of the seat")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *fakeStream) publishes() string {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
return strings.Join(s.asked, " ")
|
|
}
|
|
|
|
func keepThree(t *testing.T, f Folder) {
|
|
t.Helper()
|
|
for _, event := range []string{eventEnded, eventAsked, eventEnded} {
|
|
if _, err := f.keep(event, map[string]string{"link": "wlan0"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The bundle publishes the kept events in order with their message ids and deletes each once acknowledged; a refused
|
|
// one stays, with the ones after it, and is published at the next moment under the same id (design 08 sign-in-page
|
|
// §20, §25).
|
|
func TestTheBundlePublishesTheKeptEventsInOrder(t *testing.T) {
|
|
lines := journalLines(t)
|
|
f := aFolder(t)
|
|
keepThree(t, f)
|
|
stream := &fakeStream{refuse: map[string]bool{"laptop.sign-in-asked.2": true}}
|
|
p := &publisher{folder: f, publish: stream.publish}
|
|
if r := p.publishKept("the bundle's start"); r.Published != 1 || r.Waiting != 2 || p.refusedSince() != 1 {
|
|
t.Fatalf("first round %+v, refused %d", r, p.refusedSince())
|
|
}
|
|
if kept, _ := f.pending(); len(kept) != 2 || kept[0].Name() != "sign-in-asked.2" {
|
|
t.Fatalf("waiting %+v", kept)
|
|
}
|
|
if !strings.Contains(strings.Join(lines(), "\n"), "publishing sign-in-asked.2 at the bundle's start was refused") {
|
|
t.Fatalf("the refusal was not written to the journal: %q", lines())
|
|
}
|
|
if r := p.publishKept("a kept event"); r.Published != 2 || r.Waiting != 0 {
|
|
t.Fatalf("second round %+v", r)
|
|
}
|
|
if got := stream.publishes(); got != "laptop.sign-in-ended.1 laptop.sign-in-asked.2 laptop.sign-in-asked.2 laptop.sign-in-ended.3" {
|
|
t.Fatalf("published %s", got)
|
|
}
|
|
if kept, _ := f.pending(); len(kept) != 0 {
|
|
t.Fatalf("left %+v", kept)
|
|
}
|
|
}
|
|
|
|
// The bundle publishes at its start, at each file event on pending/ and when its bus connection comes back: the first
|
|
// heartbeat answered after a missed one; an older heartbeat arriving late changes nothing (§20, DMC-4).
|
|
func TestTheBundlePublishesAtEachMoment(t *testing.T) {
|
|
journalLines(t)
|
|
f := aFolder(t)
|
|
keepThree(t, f)
|
|
stream := &fakeStream{refuse: map[string]bool{"laptop.sign-in-ended.1": true}}
|
|
b := &bundle{folder: f, pub: &publisher{folder: f, publish: stream.publish}, names: &namer{folder: f, now: time.Now,
|
|
ask: func(string, any) (json.RawMessage, error) { return json.RawMessage(`{"seats":[]}`), nil }}}
|
|
files := make(chan struct{})
|
|
var hook func(stdio.Heartbeat)
|
|
done := make(chan struct{})
|
|
go func() {
|
|
b.start(bundleRuns{files: func(dirs []string, _ <-chan struct{}) <-chan struct{} {
|
|
if len(dirs) != 1 || !strings.HasSuffix(dirs[0], "/pending") {
|
|
t.Errorf("watched %v", dirs)
|
|
}
|
|
return files
|
|
}, fileMade: func(string, string, <-chan struct{}) <-chan struct{} { return nil },
|
|
onHeartbeat: func(h func(stdio.Heartbeat)) error { hook = h; return nil },
|
|
subscribe: func(string, func(stdio.Envelope) error) error { return nil }})
|
|
close(done)
|
|
}()
|
|
files <- struct{}{} // the start's round ran and was refused; this one publishes what waits
|
|
if _, err := f.keep(eventAsked, map[string]string{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
files <- struct{}{}
|
|
close(files)
|
|
<-done
|
|
if kept, _ := f.pending(); len(kept) != 0 {
|
|
t.Fatalf("a file event published nothing: %+v", kept)
|
|
}
|
|
keepThree(t, f)
|
|
at := time.Date(2026, 11, 24, 20, 0, 0, 0, time.UTC)
|
|
hook(stdio.Heartbeat{At: at})
|
|
if kept, _ := f.pending(); len(kept) != 3 {
|
|
t.Fatal("a heartbeat with none missed published")
|
|
}
|
|
hook(stdio.Heartbeat{At: at.Add(time.Second), Missed: true, Why: "no heartbeat"})
|
|
hook(stdio.Heartbeat{At: at.Add(-time.Second)})
|
|
if kept, _ := f.pending(); len(kept) != 3 {
|
|
t.Fatal("an older heartbeat arriving late was taken for the connection's return")
|
|
}
|
|
hook(stdio.Heartbeat{At: at.Add(2 * time.Second)})
|
|
if kept, _ := f.pending(); len(kept) != 0 {
|
|
t.Fatalf("the connection's return published nothing: %+v", kept)
|
|
}
|
|
if got := stream.publishes(); !strings.HasPrefix(got, "laptop.sign-in-ended.1 laptop.sign-in-ended.1 laptop.sign-in-asked.2") {
|
|
t.Fatalf("published %s", got)
|
|
}
|
|
}
|
|
|
|
// The bundle publishes a change the watcher kept with no node under its own: the subject `<event>.<node>`, the message
|
|
// id `<node>.<event>.<sequence>` and the body's node, the same at every publish of one file; a bundle that names no node
|
|
// publishes nothing and keeps the change (design 08 sign-in-page §20; lead answer L-3).
|
|
func TestTheBundleFillsItsNodeIntoAKeptChange(t *testing.T) {
|
|
journalLines(t)
|
|
watcher := Folder{Dir: t.TempDir()}
|
|
if _, err := watcher.keep(eventAsked, nodeuplink.SignInAsked{Link: "wlan0", Network: "guest-example",
|
|
Address: "http://check.example.net/", At: "2026-11-24T10:00:00Z"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var said []string
|
|
publish := func(subject string, body json.RawMessage, id string) error {
|
|
said = append(said, subject+" "+id+" "+string(body))
|
|
return errors.New("no acknowledgement")
|
|
}
|
|
unnamed := &publisher{folder: watcher, publish: publish}
|
|
if r := unnamed.publishKept("a test"); r.Waiting != 1 || !strings.Contains(r.Refused, "no node named") || len(said) != 0 {
|
|
t.Fatalf("a bundle with no node: %+v, said %q", r, said)
|
|
}
|
|
p := &publisher{folder: Folder{Dir: watcher.Dir, Node: "laptop"}, publish: publish}
|
|
p.publishKept("a test")
|
|
p.publishKept("a test")
|
|
want := `mesh.seat.node-uplink.event.sign-in-asked.laptop laptop.sign-in-asked.1 ` +
|
|
`{"address":"http://check.example.net/","at":"2026-11-24T10:00:00Z","link":"wlan0","network":"guest-example","node":"laptop"}`
|
|
if len(said) != 2 || said[0] != want || said[1] != want {
|
|
t.Fatalf("published %q, want twice %s", said, want)
|
|
}
|
|
}
|