Skip to content

Commit

Permalink
Merge pull request #711 from libp2p/fix/findpeer-relay
Browse files Browse the repository at this point in the history
findpeer should work even on peers that are not part of DHT queries
  • Loading branch information
aschmahmann authored Apr 5, 2021
2 parents 03d4b62 + 0373409 commit f05ebb2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 62 deletions.
56 changes: 56 additions & 0 deletions dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,62 @@ func TestFindPeer(t *testing.T) {
}
}

func TestFindPeerWithQueryFilter(t *testing.T) {
// t.Skip("skipping test to debug another")
if testing.Short() {
t.SkipNow()
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

filteredPeer := bhost.New(swarmt.GenSwarm(t, ctx, swarmt.OptDisableReuseport))
dhts := setupDHTS(t, ctx, 4, QueryFilter(func(_ *IpfsDHT, ai peer.AddrInfo) bool {
return ai.ID != filteredPeer.ID()
}))
defer func() {
for i := 0; i < 4; i++ {
dhts[i].Close()
dhts[i].host.Close()
}
}()

connect(t, ctx, dhts[0], dhts[1])
connect(t, ctx, dhts[1], dhts[2])
connect(t, ctx, dhts[1], dhts[3])

err := filteredPeer.Connect(ctx, peer.AddrInfo{
ID: dhts[2].host.ID(),
Addrs: dhts[2].host.Addrs(),
})
if err != nil {
t.Fatal(err)
}
for i, maxLoops := 0, 5; i < maxLoops; i++ {
if len(dhts[2].host.Network().ConnsToPeer(filteredPeer.ID())) > 0 {
break
}
if i == maxLoops {
t.Fatal("failed to connect to the filtered peer")
}
}

ctxT, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
p, err := dhts[0].FindPeer(ctxT, filteredPeer.ID())
if err != nil {
t.Fatal(err)
}

if p.ID == "" {
t.Fatal("Failed to find peer.")
}

if p.ID != filteredPeer.ID() {
t.Fatal("Didnt find expected peer.")
}
}

func TestConnectCollision(t *testing.T) {
if testing.Short() {
t.SkipNow()
Expand Down
Loading

0 comments on commit f05ebb2

Please sign in to comment.