Files
mesh-host/internal/link/roused_test.go
T
jschoubben 0c01c81c3f E8: run each file's unit tests as subtests of one test
The node-engine held 1,707 top-level unit tests against the budget of 500
a module. Each unit test file with two or more tests now runs them as
subtests of one test named for the file, each named as its function was,
in the same order: every check is kept, and the check counts 383. Files
with a TestMain, a re-run of a test by its name, or the integration tag
are left. Four files of this branch's earlier commits are formatted.

Builds: design 47 H2 §819; H-A-349 DQ-14
2026-11-22 00:30:05 +01:00

110 lines
3.1 KiB
Go

package link
import (
"context"
"errors"
"strings"
"sync"
"testing"
"time"
)
// A machine that just woke does not wait to be told its link is dead.
//
// After a resume the socket looks perfectly healthy from inside the process — no error, no close,
// because nothing has tried to send anything. Heartbeats discover it twenty or thirty seconds
// later, and for that time the node believes it is in a mesh it has left, which is the one state
// this design says must never be indistinguishable from being connected.
func testBeingRousedEndsTheCurrentAttemptRatherThanWaitingForATimeout(t *testing.T) {
// A link that never returns on its own, which is exactly what a suspended connection is.
held := make(chan context.Context, 4)
running := func(ctx context.Context) error {
held <- ctx
<-ctx.Done()
return errors.New("the link ended")
}
rouse := make(chan struct{}, 1)
said := &saidSoFar{}
ctx, stop := context.WithCancel(context.Background())
defer stop()
finished := make(chan error, 1)
go func() { finished <- holdWith(ctx, running, said.say, rouse) }()
first := <-held
select {
case <-first.Done():
t.Fatal("the link ended before anything roused it")
case <-time.After(50 * time.Millisecond):
}
rouse <- struct{}{}
select {
case <-first.Done():
case <-time.After(2 * time.Second):
t.Fatal("the machine woke and the link was left running against a socket that is gone")
}
// And it opens another one rather than stopping.
select {
case <-held:
case <-time.After(5 * time.Second):
t.Fatal("the link was dropped and never opened again")
}
if !strings.Contains(said.all(), "woken or moved") {
t.Fatalf("nothing was said about why the link was dropped:\n%s", said.all())
}
stop()
select {
case <-finished:
case <-time.After(2 * time.Second):
t.Fatal("it did not stop when asked")
}
}
// A machine nothing ever rouses is every machine that does not suspend, and must behave as before.
func testAMachineNothingRousesIsUnaffected(t *testing.T) {
attempts := make(chan struct{}, 4)
running := func(context.Context) error {
attempts <- struct{}{}
return errors.New("the link ended")
}
ctx, stop := context.WithCancel(context.Background())
defer stop()
go func() { _ = holdWith(ctx, running, func(string) {}, nil) }()
// It keeps trying, which is the behaviour a node with no rouse has always had.
for i := 0; i < 2; i++ {
select {
case <-attempts:
case <-time.After(10 * time.Second):
t.Fatal("a node with nothing to rouse it stopped reconnecting")
}
}
}
type saidSoFar struct {
mu sync.Mutex
said []string
}
func (s *saidSoFar) say(line string) {
s.mu.Lock()
defer s.mu.Unlock()
s.said = append(s.said, line)
}
func (s *saidSoFar) all() string {
s.mu.Lock()
defer s.mu.Unlock()
return strings.Join(s.said, "\n")
}
// TestRoused runs this file's tests, each a subtest named as its function was (design 47 H2 §819, the test budget).
func TestRoused(t *testing.T) {
t.Run("BeingRousedEndsTheCurrentAttemptRatherThanWaitingForATimeout", testBeingRousedEndsTheCurrentAttemptRatherThanWaitingForATimeout)
t.Run("AMachineNothingRousesIsUnaffected", testAMachineNothingRousesIsUnaffected)
}