diff --git a/hscontrol/servertest/client.go b/hscontrol/servertest/client.go index c0fdc544e..538cfa996 100644 --- a/hscontrol/servertest/client.go +++ b/hscontrol/servertest/client.go @@ -87,6 +87,9 @@ type TestClient struct { frames int stamped bool + // firstMap closes on the first netmap, without taking it from updates. + firstMap chan struct{} + // updates is a buffered channel that receives a signal // each time a new [netmap.NetworkMap] arrives. updates chan *netmap.NetworkMap @@ -188,7 +191,9 @@ func WithUser(user *types.User) ClientOption { } // NewClient creates a [TestClient], registers it with the [TestServer] -// using a pre-auth key, and starts long-polling for map updates. +// using a pre-auth key, starts long-polling for map updates, and returns +// once the first map arrives. That map is still queued for +// [TestClient.WaitForUpdate]. func NewClient(tb testing.TB, server *TestServer, name string, opts ...ClientOption) *TestClient { tb.Helper() @@ -265,6 +270,7 @@ func TryNewClient(tb testing.TB, server *TestServer, name string, opts ...Client endpoints: cc.endpoints, c2nHandler: cc.c2nHandler, updates: make(chan *netmap.NetworkMap, updateBuffer), + firstMap: make(chan struct{}), bus: bus, dialer: dialer, tracker: tracker, @@ -318,6 +324,16 @@ func TryNewClient(tb testing.TB, server *TestServer, name string, opts ...Client // Start long-polling in the background. tc.startPoll(tb) + // The first map follows [state.State.Connect], so clients created in + // turn open their control sessions in that order: the HA election + // ranks routers by session age. + select { + case <-tc.firstMap: + case <-tc.pollDone: + case <-time.After(10 * time.Second): + tb.Fatalf("servertest: NewClient(%s): no map after 10s", name) + } + return tc, nil } @@ -471,6 +487,11 @@ func (c *TestClient) UpdateFullNetmap(nm *netmap.NetworkMap) { c.updatesReceived++ c.streamMaps++ + // A client built without TryNewClient has no firstMap. + if c.updatesReceived == 1 && c.firstMap != nil { + close(c.firstMap) + } + if !c.noHistory { c.history = append(c.history, nm) } diff --git a/hscontrol/servertest/ha_health_test.go b/hscontrol/servertest/ha_health_test.go index 4d5222007..9da50eb81 100644 --- a/hscontrol/servertest/ha_health_test.go +++ b/hscontrol/servertest/ha_health_test.go @@ -34,7 +34,7 @@ func advertiseAndApproveRoute( ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - _ = c.Direct().SendUpdate(ctx) + require.NoError(t, c.Direct().SendUpdate(ctx)) nodeID := findNodeID(t, srv, c.Name) @@ -106,7 +106,7 @@ func TestHAHealthProbe_UnhealthyFailover(t *testing.T) { nodeID1 := advertiseAndApproveRoute(t, srv, c1, route) nodeID2 := advertiseAndApproveRoute(t, srv, c2, route) - // Node 1 should be primary (lower ID). + // Node 1 should be primary (older session). primaries := srv.State().GetNodePrimaryRoutes(nodeID1) require.Contains(t, primaries, route, "node 1 should be primary initially") @@ -310,7 +310,7 @@ func TestHAHealthProbe_ReconnectDuringProbeKeepsHealthy(t *testing.T) { nodeID1 := advertiseAndApproveRoute(t, srv, c1, route) advertiseAndApproveRoute(t, srv, c2, route) - // Node 1 is primary (lowest ID, healthy). + // Node 1 is primary (older session, healthy). require.Contains(t, srv.State().GetNodePrimaryRoutes(nodeID1), route, "node 1 should be primary initially")