Files
mesh-catalog/modules/networkmanager/cmd/uplink-tools/publish_test.go
T
jochen 786a23638b M6 networkmanager: the bundle gives the resolver the check's name
The bundle calls this node's node-resolver uplink-names with the check address's host
at its start, at a changed check-address, at its bus connection's return and when this
node's holder of node-resolver runs, reading the holder from the assignments; no holder
is recorded and no failure, a failed call stands failed in names.json until the next
moment.

Builds: design 08 sign-in-page §12, §13, §22
2026-11-24 21:23:56 +01:00

132 lines
4.7 KiB
Go

package main
import (
"encoding/json"
"errors"
"strings"
"sync"
"testing"
"time"
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, eventSubject(event, f.Node), map[string]string{"node": f.Node}); 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].MessageID != "laptop.sign-in-asked.2" {
t.Fatalf("waiting %+v", kept)
}
if !strings.Contains(strings.Join(lines(), "\n"), "publishing laptop.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, eventSubject(eventAsked, "laptop"), 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)
}
}