Commit 9bd5c5d7 authored by Jeromy's avatar Jeromy

fix filtering bug in find closest peers

parent d124b897
......@@ -824,3 +824,36 @@ func TestClientModeConnect(t *testing.T) {
t.Fatal("expected it to be our test peer")
}
}
func TestFindClosestPeers(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
nDHTs := 30
_, _, dhts := setupDHTS(ctx, nDHTs, t)
defer func() {
for i := 0; i < nDHTs; i++ {
dhts[i].Close()
defer dhts[i].host.Close()
}
}()
t.Logf("connecting %d dhts in a ring", nDHTs)
for i := 0; i < nDHTs; i++ {
connect(t, ctx, dhts[i], dhts[(i+1)%len(dhts)])
}
peers, err := dhts[1].GetClosestPeers(ctx, "foo")
if err != nil {
t.Fatal(err)
}
var out []peer.ID
for p := range peers {
out = append(out, p)
}
if len(out) != KValue {
t.Fatalf("got wrong number of peers (got %d, expected %d)", len(out), KValue)
}
}
......@@ -66,7 +66,7 @@ func (dht *IpfsDHT) GetClosestPeers(ctx context.Context, key string) (<-chan pee
var filtered []pstore.PeerInfo
for _, clp := range closer {
if kb.Closer(clp, dht.self, key) && peerset.TryAdd(clp) {
if peerset.TryAdd(clp) {
select {
case out <- clp:
case <-ctx.Done():
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment