Commit f1b9804d authored by acud's avatar acud Committed by GitHub

kademlia: TestClosestPeer wait for peers to be added to the backing data structure (#707)

* kademlia: wait for peers to be added to the backing data structure
parent 3a877f50
......@@ -477,9 +477,8 @@ func TestClosestPeer(t *testing.T) {
disc := mock.NewDiscovery()
ab := addressbook.New(mockstate.NewStateStore())
var conns int32
kad := kademlia.New(base, ab, disc, p2pMock(ab, nil, &conns, nil), logger, kademlia.Options{})
kad := kademlia.New(base, ab, disc, p2pMock(ab, nil, nil, nil), logger, kademlia.Options{})
if err := kad.Start(context.Background()); err != nil {
t.Fatal(err)
}
......@@ -489,7 +488,8 @@ func TestClosestPeer(t *testing.T) {
for _, v := range connectedPeers {
addOne(t, beeCrypto.NewDefaultSigner(pk), kad, ab, v.Address)
}
waitCounter(t, &conns, 3)
waitPeers(t, kad, 3)
for _, tc := range []struct {
chunkAddress swarm.Address // chunk address to test
......@@ -863,6 +863,26 @@ func waitCounter(t *testing.T, conns *int32, exp int32) {
t.Fatalf("timed out waiting for counter to reach expected value. got %d want %d", got, exp)
}
func waitPeers(t *testing.T, k *kademlia.Kad, peers int) {
timeout := time.After(3 * time.Second)
for {
select {
case <-timeout:
t.Fatal("timed out waiting for peers")
default:
}
i := 0
_ = k.EachPeer(func(_ swarm.Address, _ uint8) (bool, bool, error) {
i++
return false, false, nil
})
if i == peers {
return
}
time.Sleep(50 * time.Millisecond)
}
}
// wait for discovery BroadcastPeers to happen
func waitBcast(t *testing.T, d *mock.Discovery, pivot swarm.Address, addrs ...swarm.Address) {
t.Helper()
......
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