prefer tickers to time.Sleep (Refs #790)

This commit is contained in:
Anton Kaliaev
2017-11-07 15:02:42 -05:00
parent ec87c740a7
commit 2d4ad02356
4 changed files with 84 additions and 83 deletions

View File

@@ -1,6 +1,7 @@
package p2p
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
@@ -98,15 +99,7 @@ func TestPEXReactorRunning(t *testing.T) {
require.Nil(err)
}
time.Sleep(1 * time.Second)
// check peers are connected after some time
for _, s := range switches {
outbound, inbound, _ := s.NumPeers()
if outbound+inbound == 0 {
t.Errorf("%v expected to be connected to at least one peer", s.NodeInfo().ListenAddr)
}
}
assertSomePeersWithTimeout(t, switches, 10*time.Millisecond, 10*time.Second)
// stop them
for _, s := range switches {
@@ -114,6 +107,31 @@ func TestPEXReactorRunning(t *testing.T) {
}
}
func assertSomePeersWithTimeout(t *testing.T, switches []*Switch, checkPeriod, timeout time.Duration) {
ticker := time.NewTicker(checkPeriod)
select {
case <-ticker.C:
// check peers are connected
allGood := true
for _, s := range switches {
outbound, inbound, _ := s.NumPeers()
if outbound+inbound == 0 {
allGood = false
}
}
if allGood {
return
}
case <-time.After(timeout):
numPeersStr := ""
for i, s := range switches {
outbound, inbound, _ := s.NumPeers()
numPeersStr += fmt.Sprintf("%d => {outbound: %d, inbound: %d}, ", i, outbound, inbound)
}
t.Errorf("expected all switches to be connected to at least one peer (switches: %s)", numPeersStr)
}
}
func TestPEXReactorReceive(t *testing.T) {
assert, require := assert.New(t), require.New(t)