From cc833947bc96599d51a0e7dfcb0fa74e32a50bad Mon Sep 17 00:00:00 2001 From: jiangpeng <11565373+bysomeone@users.noreply.github.com> Date: Tue, 12 Sep 2023 19:34:10 +0800 Subject: [PATCH 1/3] dht:manual start mdns service --- system/p2p/dht/extension/mdns_test.go | 24 +++++++++++------------- system/p2p/dht/p2p_test.go | 1 - 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/system/p2p/dht/extension/mdns_test.go b/system/p2p/dht/extension/mdns_test.go index abca30bdb..f336a8107 100644 --- a/system/p2p/dht/extension/mdns_test.go +++ b/system/p2p/dht/extension/mdns_test.go @@ -2,32 +2,30 @@ 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]) - _, err := NewMDNS(ctx, hosts[0], "33test123") - require.Nil(t, err) - _, err = NewMDNS(ctx, hosts[1], "33test123") + hosts := getNetHosts(2, t) + tmdns, err := NewMDNS(ctx, hosts[0], "33test123") require.Nil(t, err) - tmdns, err := NewMDNS(ctx, hosts[2], "33test123") + require.Nil(t, tmdns.Service.Start()) + //_, err = NewMDNS(ctx, hosts[1], "33test123") + //require.Nil(t, err) + tmdns, err = NewMDNS(ctx, hosts[1], "33test123") require.Nil(t, err) + require.Nil(t, tmdns.Service.Start()) 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{}) } diff --git a/system/p2p/dht/p2p_test.go b/system/p2p/dht/p2p_test.go index 6a48959b2..290467909 100644 --- a/system/p2p/dht/p2p_test.go +++ b/system/p2p/dht/p2p_test.go @@ -471,5 +471,4 @@ func Test_p2p(t *testing.T) { tcfg.DbPath = filepath.Join(datadir, "addrbook") testAddrbook(t, &tcfg) dhtp2p.reStart() - p2p.CloseP2P() } From fa0e2108c5556fb4cc7b013c2f708f09ab59397d Mon Sep 17 00:00:00 2001 From: jiangpeng <11565373+bysomeone@users.noreply.github.com> Date: Wed, 13 Sep 2023 10:47:43 +0800 Subject: [PATCH 2/3] start mdns service when new --- system/p2p/dht/extension/mdns.go | 5 +++++ system/p2p/dht/extension/mdns_test.go | 4 ---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/system/p2p/dht/extension/mdns.go b/system/p2p/dht/extension/mdns.go index 5eae737f7..de3a7fbf7 100644 --- a/system/p2p/dht/extension/mdns.go +++ b/system/p2p/dht/extension/mdns.go @@ -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 diff --git a/system/p2p/dht/extension/mdns_test.go b/system/p2p/dht/extension/mdns_test.go index f336a8107..561685ba9 100644 --- a/system/p2p/dht/extension/mdns_test.go +++ b/system/p2p/dht/extension/mdns_test.go @@ -14,12 +14,8 @@ func Test_mdns(t *testing.T) { hosts := getNetHosts(2, t) tmdns, err := NewMDNS(ctx, hosts[0], "33test123") require.Nil(t, err) - require.Nil(t, tmdns.Service.Start()) - //_, err = NewMDNS(ctx, hosts[1], "33test123") - //require.Nil(t, err) tmdns, err = NewMDNS(ctx, hosts[1], "33test123") require.Nil(t, err) - require.Nil(t, tmdns.Service.Start()) select { case peerinfo := <-tmdns.PeerChan(): From 1817c11f2c5d7262d92354266e9ad2215ac301bc Mon Sep 17 00:00:00 2001 From: jiangpeng <11565373+bysomeone@users.noreply.github.com> Date: Wed, 13 Sep 2023 10:52:22 +0800 Subject: [PATCH 3/3] test:fmt code --- system/p2p/dht/extension/mdns_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/p2p/dht/extension/mdns_test.go b/system/p2p/dht/extension/mdns_test.go index 561685ba9..9f47381de 100644 --- a/system/p2p/dht/extension/mdns_test.go +++ b/system/p2p/dht/extension/mdns_test.go @@ -12,9 +12,9 @@ func Test_mdns(t *testing.T) { defer cancel() hosts := getNetHosts(2, t) - tmdns, err := NewMDNS(ctx, hosts[0], "33test123") + _, err := NewMDNS(ctx, hosts[0], "33test123") require.Nil(t, err) - tmdns, err = NewMDNS(ctx, hosts[1], "33test123") + tmdns, err := NewMDNS(ctx, hosts[1], "33test123") require.Nil(t, err) select {