From d04ca6d6ad1e14dd9c71535bb4858c276743644f Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sun, 3 Oct 2021 14:10:41 +0100 Subject: [PATCH] properly close hosts in mDNS tests --- p2p/discovery/mdns/mdns_test.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/p2p/discovery/mdns/mdns_test.go b/p2p/discovery/mdns/mdns_test.go index 0b7de3086a..d599e721db 100644 --- a/p2p/discovery/mdns/mdns_test.go +++ b/p2p/discovery/mdns/mdns_test.go @@ -6,20 +6,23 @@ import ( "time" "github.com/libp2p/go-libp2p" - "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/peer" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) -func setupMDNS(t *testing.T, notifee Notifee) (host.Host, *mdnsService) { +func setupMDNS(t *testing.T, notifee Notifee) peer.ID { t.Helper() host, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0")) require.NoError(t, err) s := NewMdnsService(host, "") s.RegisterNotifee(notifee) - return host, s + t.Cleanup(func() { + host.Close() + s.Close() + }) + return host.ID() } type notif struct { @@ -45,14 +48,13 @@ func (n *notif) GetPeers() []peer.AddrInfo { func TestSelfDiscovery(t *testing.T) { notif := ¬if{} - host, s := setupMDNS(t, notif) - defer s.Close() + hostID := setupMDNS(t, notif) assert.Eventuallyf( t, func() bool { var found bool for _, info := range notif.GetPeers() { - if info.ID == host.ID() { + if info.ID == hostID { found = true break } @@ -73,10 +75,7 @@ func TestOtherDiscovery(t *testing.T) { for i := 0; i < n; i++ { notif := ¬if{} notifs[i] = notif - var s *mdnsService - host, s := setupMDNS(t, notif) - hostIDs[i] = host.ID() - defer s.Close() + hostIDs[i] = setupMDNS(t, notif) } containsAllHostIDs := func(ids []peer.ID) bool {