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
|
package network
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -113,18 +114,9 @@ func TestStreamerRegisterIncoming(t *testing.T) {
|
||||||
}, nil
|
}, nil
|
||||||
})
|
})
|
||||||
|
|
||||||
tick := time.NewTicker(10 * time.Millisecond)
|
err = waitForPeers(streamer, 1*time.Second)
|
||||||
timeout := time.NewTimer(1 * time.Second)
|
if err != nil {
|
||||||
WAIT:
|
t.Fatal("timeout: peer is not created")
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-tick.C:
|
|
||||||
if len(streamer.peers) > 0 {
|
|
||||||
break WAIT
|
|
||||||
}
|
|
||||||
case <-timeout.C:
|
|
||||||
t.Fatal("timeout")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = streamer.Subscribe(tester.IDs[0], "foo", nil, 0, 0, Top, true)
|
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)
|
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