Skip to content

Agent Mesh

controlplane,router: wait for publisher admission and peer identify in tests

Antonio Ojea99546a3b5ac1
diff --git a/internal/controlplane/mesh.go b/internal/controlplane/mesh.go
index 3a5cf176..2a0dc80b 100644
--- a/internal/controlplane/mesh.go
+++ b/internal/controlplane/mesh.go
@@ -182,8 +182,12 @@ func NewMeshPublisher(ctx context.Context, store storage.Store, reconnect time.D
return
}
p.admittedMu.Lock()
+ _, wasAdmitted := p.admittedUntil[c.RemotePeer()]
delete(p.admittedUntil, c.RemotePeer())
p.admittedMu.Unlock()
+ if wasAdmitted {
+ logger.Infof("[Mesh] Disconnected from router %s", c.RemotePeer())
+ }
},
})
var wg sync.WaitGroup
@@ -202,6 +206,13 @@ func NewMeshPublisher(ctx context.Context, store storage.Store, reconnect time.D
return p, nil
}
+func (p *P2PMeshAdapter) admittedAt(router peer.ID) (time.Time, bool) {
+ p.admittedMu.Lock()
+ defer p.admittedMu.Unlock()
+ until, ok := p.admittedUntil[router]
+ return until, ok
+}
+
// keepRoutersConnected dials, now and every interval, each leased router the
// host is not connected to. A publish only reaches peers that have announced
// the topic, so connections are kept warm ahead of the events rather than
diff --git a/internal/controlplane/mesh_test.go b/internal/controlplane/mesh_test.go
index e827f2e3..cff7ea88 100644
--- a/internal/controlplane/mesh_test.go
+++ b/internal/controlplane/mesh_test.go
@@ -336,11 +336,20 @@ func TestMeshPublisherReachesLeasedRouter(t *testing.T) {
case <-ctx.Done():
t.Fatal("publisher never ran the handshake with the router")
}
- publisher.admittedMu.Lock()
- until, ok := publisher.admittedUntil[routerHost.ID()]
- publisher.admittedMu.Unlock()
- if !ok || time.Until(until) < publisherCredentialTTL/2 {
- t.Fatalf("publisher does not record its admission at the router (until %v, %v)", until, ok)
+ // admitted fires as soon as the router writes its reply; the publisher
+ // records the admission once it reads that reply off the stream.
+ for {
+ if until, ok := publisher.admittedAt(routerHost.ID()); ok {
+ if time.Until(until) < publisherCredentialTTL/2 {
+ t.Fatalf("publisher does not record its admission at the router (until %v, %v)", until, ok)
+ }
+ break
+ }
+ select {
+ case <-ctx.Done():
+ t.Fatal("publisher never recorded its admission at the router")
+ case <-time.After(10 * time.Millisecond):
+ }
}
_, target := newTestKey(t)
diff --git a/internal/router/metrics_test.go b/internal/router/metrics_test.go
index 2f98750a..e28fe5ff 100644
--- a/internal/router/metrics_test.go
+++ b/internal/router/metrics_test.go
@@ -143,13 +143,21 @@ func TestRouterMetricsListener(t *testing.T) {
t.Fatal(err)
}
r.authenticatedPeers.Store(other.ID(), time.Now().Add(time.Hour))
- code, body = getBody(t, base+"/debug/peer?peer_id="+other.ID().String())
- if code != http.StatusOK {
- t.Fatalf("/debug/peer = %d: %s", code, body)
- }
+ // Identify runs asynchronously on the receiving host after Connect
+ // returns; wait for /debug/peer to show the peer's listen addresses.
var view peerView
- if err := json.Unmarshal([]byte(body), &view); err != nil {
- t.Fatalf("/debug/peer: %v: %s", err, body)
+ for deadline := time.Now().Add(5 * time.Second); ; {
+ code, body = getBody(t, base+"/debug/peer?peer_id="+other.ID().String())
+ if code != http.StatusOK {
+ t.Fatalf("/debug/peer = %d: %s", code, body)
+ }
+ if err := json.Unmarshal([]byte(body), &view); err != nil {
+ t.Fatalf("/debug/peer: %v: %s", err, body)
+ }
+ if len(view.Addrs) > 0 || time.Now().After(deadline) {
+ break
+ }
+ time.Sleep(10 * time.Millisecond)
}
if view.Connectedness != "Connected" || !view.Authenticated || view.PeerRouter || len(view.Connections) != 1 || len(view.Addrs) == 0 {
t.Errorf("/debug/peer for a connected, authenticated member: %+v", view)