networkmanager: TestUplinkNamesIsCalledAtEachMoment waits for each call, no race with the bundle's start #756

Merged
mesh-tools merged 1 commits from fix/uplink-names-moment-test into main 2026-11-25 00:38:39 +00:00
@@ -19,6 +19,8 @@ type fakeMesh struct {
fail error
calls []string
routes string
// given, when set, hears each uplink-names call once made, so a test acts after it and never on a guess of when.
given chan string
}
func (m *fakeMesh) ask(key string, body any) (json.RawMessage, error) {
@@ -38,6 +40,9 @@ func (m *fakeMesh) ask(key string, body any) (json.RawMessage, error) {
case "seat:node-resolver.uplink-names@laptop":
raw, _ := json.Marshal(body)
m.calls = append(m.calls, string(raw))
if m.given != nil {
m.given <- string(raw)
}
if m.fail != nil {
return nil, m.fail
}
@@ -66,10 +71,13 @@ func TestUplinkNamesIsCalledAtEachMoment(t *testing.T) {
if err := f.writeCheckAddress("http://Check.Example.net/connecttest"); err != nil {
t.Fatal(err)
}
mesh := &fakeMesh{holder: "systemd-resolved"}
mesh := &fakeMesh{holder: "systemd-resolved", given: make(chan string, 16)}
b := &bundle{folder: f, pub: &publisher{folder: f, publish: (&fakeStream{}).publish},
names: &namer{folder: f, ask: mesh.ask, now: time.Now}}
check := make(chan struct{})
// The bundle runs beside the test: each check address is changed only once the call made from the one before was
// heard, so the test never races the bundle's read of check-address (a call never made ends the run at its bound).
heard := func() { <-mesh.given }
var running func(stdio.Envelope) error
var hook func(stdio.Heartbeat)
done := make(chan struct{})
@@ -91,10 +99,12 @@ func TestUplinkNamesIsCalledAtEachMoment(t *testing.T) {
}})
close(done)
}()
heard()
if err := f.writeCheckAddress(""); err != nil {
t.Fatal(err)
}
check <- struct{}{}
heard()
f.writeCheckAddress("https://other.example.net/")
close(check)
<-done