Skip to content

Commit

Permalink
mdns: don't discover ourselves (#1661)
Browse files Browse the repository at this point in the history
* bug fixed in mdns.go

* TestSelfDiscovery removed

* TestOtherDiscovery modified

* TestOtherDiscovery modified

* gofmt unhappy fixed?

Co-authored-by: gaoshan <takayamachina@gmail.com>
  • Loading branch information
MGMCN and MGMCN committed Aug 2, 2022
1 parent 8a87343 commit 52e5a35
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 24 deletions.
3 changes: 3 additions & 0 deletions p2p/discovery/mdns/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ func (s *mdnsService) startResolver(ctx context.Context) {
continue
}
for _, info := range infos {
if info.ID == s.host.ID() {
continue
}
go s.notifee.HandlePeerFound(info)
}
}
Expand Down
30 changes: 6 additions & 24 deletions p2p/discovery/mdns/mdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,6 @@ func (n *notif) GetPeers() []peer.AddrInfo {
return infos
}

func TestSelfDiscovery(t *testing.T) {
notif := &notif{}
hostID := setupMDNS(t, notif)
assert.Eventuallyf(
t,
func() bool {
var found bool
for _, info := range notif.GetPeers() {
if info.ID == hostID {
found = true
break
}
}
return found
},
5*time.Second,
5*time.Millisecond,
"expected peer to find itself",
)
}

func TestOtherDiscovery(t *testing.T) {
const n = 4

Expand All @@ -78,9 +57,12 @@ func TestOtherDiscovery(t *testing.T) {
hostIDs[i] = setupMDNS(t, notif)
}

containsAllHostIDs := func(ids []peer.ID) bool {
containsAllHostIDs := func(ids []peer.ID, currentHostID peer.ID) bool {
for _, id := range hostIDs {
var found bool
if currentHostID == id {
continue
}
for _, i := range ids {
if id == i {
found = true
Expand All @@ -97,13 +79,13 @@ func TestOtherDiscovery(t *testing.T) {
assert.Eventuallyf(
t,
func() bool {
for _, notif := range notifs {
for i, notif := range notifs {
infos := notif.GetPeers()
ids := make([]peer.ID, 0, len(infos))
for _, info := range infos {
ids = append(ids, info.ID)
}
if !containsAllHostIDs(ids) {
if !containsAllHostIDs(ids, hostIDs[i]) {
return false
}
}
Expand Down

0 comments on commit 52e5a35

Please sign in to comment.