Skip to content

Commit

Permalink
Merge pull request #1312 from bysomeone/fix-dht-mdns
Browse files Browse the repository at this point in the history
dht:manual start mdns service
  • Loading branch information
bysomeone authored Sep 13, 2023
2 parents 34bc9fa + 1817c11 commit 77916d3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
5 changes: 5 additions & 0 deletions system/p2p/dht/extension/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func NewMDNS(ctx context.Context, peerhost host.Host, serviceTag string) (*MDNS,
notifee := &discoveryNotifee{}
notifee.PeerChan = make(chan peer.AddrInfo, 1)
ser := discovery.NewMdnsService(peerhost, serviceTag, notifee)
err := ser.Start()
if err != nil {
log.Error("NewMDNS", "start mdns service err", err)
return nil, err
}
mnds := new(MDNS)
mnds.Service = ser
mnds.notifee = notifee
Expand Down
18 changes: 6 additions & 12 deletions system/p2p/dht/extension/mdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,26 @@ package extension

import (
"context"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/stretchr/testify/require"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func Test_mdns(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

hosts := getNetHosts(3, t)
connect(t, hosts[0], hosts[1])
hosts := getNetHosts(2, t)
_, err := NewMDNS(ctx, hosts[0], "33test123")
require.Nil(t, err)
_, err = NewMDNS(ctx, hosts[1], "33test123")
require.Nil(t, err)
tmdns, err := NewMDNS(ctx, hosts[2], "33test123")
tmdns, err := NewMDNS(ctx, hosts[1], "33test123")
require.Nil(t, err)

select {
case peerinfo := <-tmdns.PeerChan():
t.Log("findMdns", peerinfo.ID)
case <-time.After(time.Second * 5):
return
require.Equal(t, hosts[0].ID(), peerinfo.ID)
case <-time.After(time.Second * 10):
t.Error("mdns discovery failed, timeout")
}
tmdns.notifee.HandlePeerFound(peer.AddrInfo{})

}
1 change: 0 additions & 1 deletion system/p2p/dht/p2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,5 +471,4 @@ func Test_p2p(t *testing.T) {
tcfg.DbPath = filepath.Join(datadir, "addrbook")
testAddrbook(t, &tcfg)
dhtp2p.reStart()
p2p.CloseP2P()
}

0 comments on commit 77916d3

Please sign in to comment.