Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

properly close hosts in mDNS tests #1216

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions p2p/discovery/mdns/mdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -45,14 +48,13 @@ func (n *notif) GetPeers() []peer.AddrInfo {

func TestSelfDiscovery(t *testing.T) {
notif := &notif{}
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
}
Expand All @@ -73,10 +75,7 @@ func TestOtherDiscovery(t *testing.T) {
for i := 0; i < n; i++ {
notif := &notif{}
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 {
Expand Down