Skip to content

Commit

Permalink
Merge pull request #941 from libp2p/test/peer-records-no-listen-addrs
Browse files Browse the repository at this point in the history
add test to demo missing peer records after listen
  • Loading branch information
Stebalien committed May 20, 2020
2 parents 15637fc + 82c11bc commit aa60461
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,16 @@ func NewHost(ctx context.Context, net network.Network, opts *HostOpts) (*BasicHo

net.SetStreamHandler(h.newStreamHandler)

// register to be notified when the network's listen addrs change,
// so we can update our address set and push events if needed
listenHandler := func(network.Network, ma.Multiaddr) {
h.SignalAddressChange()
}
net.Notify(&network.NotifyBundle{
ListenF: listenHandler,
ListenCloseF: listenHandler,
})

return h, nil
}

Expand Down
32 changes: 32 additions & 0 deletions p2p/host/basic/basic_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,38 @@ func TestMultipleClose(t *testing.T) {

}

func TestSignedPeerRecordWithNoListenAddrs(t *testing.T) {
ctx := context.Background()

h := New(swarmt.GenSwarm(t, ctx, swarmt.OptDialOnly))

if len(h.Addrs()) != 0 {
t.Errorf("expected no listen addrs, got %d", len(h.Addrs()))
}

// now add a listen addr
err := h.Network().Listen(ma.StringCast("/ip4/0.0.0.0/tcp/0"))
if err != nil {
t.Fatal(err)
}
if len(h.Addrs()) < 1 {
t.Errorf("expected at least 1 listen addr, got %d", len(h.Addrs()))
}

// we need to sleep for a moment, since the signed record with the new addr is
// added async
time.Sleep(20 * time.Millisecond)

cab, ok := peerstore.GetCertifiedAddrBook(h.Peerstore())
if !ok {
t.Fatalf("peerstore doesn't support certified addrs")
}
rec := cab.GetPeerRecord(h.ID())
if rec == nil {
t.Fatalf("no signed peer record in peerstore for new host %s", h.ID())
}
}

func TestProtocolHandlerEvents(t *testing.T) {
ctx := context.Background()
h := New(swarmt.GenSwarm(t, ctx))
Expand Down

0 comments on commit aa60461

Please sign in to comment.