2018-02-19 15:32:58 +00:00
|
|
|
package destructive
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-03-20 18:35:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-02-19 15:32:58 +00:00
|
|
|
"github.com/status-im/status-go/geth/api"
|
|
|
|
. "github.com/status-im/status-go/t/utils"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultTimeout = 40 * time.Second
|
|
|
|
)
|
|
|
|
|
2018-02-23 07:03:55 +00:00
|
|
|
func TestPeersSuiteNetworkConnection(t *testing.T) {
|
2018-02-19 15:32:58 +00:00
|
|
|
suite.Run(t, &PeersTestSuite{controller: new(NetworkConnectionController)})
|
|
|
|
}
|
|
|
|
|
|
|
|
type PeersTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
backend *api.StatusBackend
|
|
|
|
controller *NetworkConnectionController
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PeersTestSuite) SetupTest() {
|
|
|
|
s.backend = api.NewStatusBackend()
|
2018-03-02 09:25:30 +00:00
|
|
|
config, err := MakeTestNodeConfig(GetNetworkID())
|
2018-02-19 15:32:58 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
// we need to enable atleast 1 protocol, otherwise peers won't connect
|
|
|
|
config.LightEthConfig.Enabled = false
|
|
|
|
config.WhisperConfig.Enabled = true
|
|
|
|
s.Require().NoError(s.backend.StartNode(config))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PeersTestSuite) TearDownTest() {
|
|
|
|
s.Require().NoError(s.backend.StopNode())
|
|
|
|
}
|
|
|
|
|
|
|
|
func consumeUntil(events <-chan *p2p.PeerEvent, f func(ev *p2p.PeerEvent) bool, timeout time.Duration) error {
|
|
|
|
timer := time.After(timeout)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ev := <-events:
|
|
|
|
if f(ev) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case <-timer:
|
|
|
|
return errors.New("timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
// TestStaticPeersReconnect : it tests how long it takes to reconnect with
|
|
|
|
// peers after losing connection. This is something we will have to support
|
|
|
|
// in order for mobile devices to reconnect fast if network connectivity
|
|
|
|
// is lost for ~30s.
|
2018-02-19 15:32:58 +00:00
|
|
|
func (s *PeersTestSuite) TestStaticPeersReconnect() {
|
|
|
|
// both on rinkeby and ropsten we can expect atleast 2 peers connected
|
|
|
|
expectedPeersCount := 2
|
|
|
|
events := make(chan *p2p.PeerEvent, 10)
|
2018-04-16 12:36:09 +00:00
|
|
|
node := s.backend.StatusNode().GethNode()
|
|
|
|
s.Require().NotNil(node)
|
2018-02-19 15:32:58 +00:00
|
|
|
|
|
|
|
subscription := node.Server().SubscribeEvents(events)
|
|
|
|
defer subscription.Unsubscribe()
|
|
|
|
peers := map[discover.NodeID]struct{}{}
|
|
|
|
before := time.Now()
|
|
|
|
s.Require().NoError(consumeUntil(events, func(ev *p2p.PeerEvent) bool {
|
|
|
|
log.Info("tests", "event", ev)
|
|
|
|
if ev.Type == p2p.PeerEventTypeAdd {
|
|
|
|
peers[ev.Peer] = struct{}{}
|
|
|
|
}
|
|
|
|
return len(peers) == expectedPeersCount
|
|
|
|
}, defaultTimeout))
|
|
|
|
s.WithinDuration(time.Now(), before, 5*time.Second)
|
|
|
|
|
|
|
|
s.Require().NoError(s.controller.Enable())
|
|
|
|
before = time.Now()
|
|
|
|
|
|
|
|
s.Require().NoError(consumeUntil(events, func(ev *p2p.PeerEvent) bool {
|
|
|
|
log.Info("tests", "event", ev)
|
|
|
|
if ev.Type == p2p.PeerEventTypeDrop {
|
|
|
|
delete(peers, ev.Peer)
|
|
|
|
}
|
|
|
|
return len(peers) == 0
|
|
|
|
}, defaultTimeout))
|
|
|
|
s.WithinDuration(time.Now(), before, 31*time.Second)
|
|
|
|
|
|
|
|
s.Require().NoError(s.controller.Disable())
|
|
|
|
before = time.Now()
|
|
|
|
go func() {
|
2018-04-05 09:45:26 +00:00
|
|
|
s.NoError(s.backend.StatusNode().ReconnectStaticPeers())
|
2018-02-19 15:32:58 +00:00
|
|
|
}()
|
|
|
|
s.Require().NoError(consumeUntil(events, func(ev *p2p.PeerEvent) bool {
|
|
|
|
log.Info("tests", "event", ev)
|
|
|
|
if ev.Type == p2p.PeerEventTypeAdd {
|
|
|
|
peers[ev.Peer] = struct{}{}
|
|
|
|
}
|
|
|
|
return len(peers) == expectedPeersCount
|
|
|
|
}, defaultTimeout))
|
|
|
|
s.WithinDuration(time.Now(), before, 31*time.Second)
|
|
|
|
}
|