mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 09:23:48 +00:00
Extract waiting loop for peers into a function
This commit is contained in:
parent
83b6cc4280
commit
852a6d669b
1 changed files with 19 additions and 12 deletions
|
|
@ -17,6 +17,7 @@
|
|||
package network
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
|
@ -113,18 +114,9 @@ func TestStreamerRegisterIncoming(t *testing.T) {
|
|||
}, nil
|
||||
})
|
||||
|
||||
tick := time.NewTicker(10 * time.Millisecond)
|
||||
timeout := time.NewTimer(1 * time.Second)
|
||||
WAIT:
|
||||
for {
|
||||
select {
|
||||
case <-tick.C:
|
||||
if len(streamer.peers) > 0 {
|
||||
break WAIT
|
||||
}
|
||||
case <-timeout.C:
|
||||
t.Fatal("timeout")
|
||||
}
|
||||
err = waitForPeers(streamer, 1*time.Second)
|
||||
if err != nil {
|
||||
t.Fatal("timeout: peer is not created")
|
||||
}
|
||||
|
||||
err = streamer.Subscribe(tester.IDs[0], "foo", nil, 0, 0, Top, true)
|
||||
|
|
@ -132,3 +124,18 @@ WAIT:
|
|||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func waitForPeers(streamer *Streamer, timeout time.Duration) error {
|
||||
ticker := time.NewTicker(10 * time.Millisecond)
|
||||
timeoutTimer := time.NewTimer(timeout)
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
if len(streamer.peers) > 0 {
|
||||
return nil
|
||||
}
|
||||
case <-timeoutTimer.C:
|
||||
return errors.New("timeout")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue