Makes random panics on node manager tests less frequent (#433)

This commit is contained in:
b00ris 2017-11-07 20:43:49 +03:00 committed by Ivan Tomilov
parent 086747a695
commit 9a3302ac36
1 changed files with 136 additions and 126 deletions

View File

@ -2,7 +2,6 @@ package node_test
import (
"encoding/json"
"math/rand"
"testing"
"time"
@ -13,7 +12,6 @@ import (
"github.com/ethereum/go-ethereum/rpc"
whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
"github.com/status-im/status-go/e2e"
"github.com/status-im/status-go/geth/log"
"github.com/status-im/status-go/geth/node"
"github.com/status-im/status-go/geth/params"
"github.com/status-im/status-go/geth/signal"
@ -127,7 +125,10 @@ func (s *ManagerTestSuite) TestReferencesWithoutStartedNode() {
func (s *ManagerTestSuite) TestReferencesWithStartedNode() {
s.StartTestNode()
defer s.StopTestNode()
defer func() {
time.Sleep(100 * time.Millisecond)
s.StopTestNode()
}()
var testCases = []struct {
name string
@ -199,6 +200,8 @@ func (s *ManagerTestSuite) TestNodeStartStop() {
// try stopping non-started node
s.False(s.NodeManager.IsNodeRunning())
time.Sleep(100 * time.Millisecond) //https://github.com/status-im/status-go/issues/429#issuecomment-339663163
_, err = s.NodeManager.StopNode()
s.Equal(err, node.ErrNoRunningNode)
@ -215,6 +218,7 @@ func (s *ManagerTestSuite) TestNodeStartStop() {
s.Equal(err, node.ErrNodeExists)
// now stop node
time.Sleep(100 * time.Millisecond) //https://github.com/status-im/status-go/issues/429#issuecomment-339663163
nodeStopped, err := s.NodeManager.StopNode()
s.NoError(err)
<-nodeStopped
@ -228,6 +232,7 @@ func (s *ManagerTestSuite) TestNodeStartStop() {
s.True(s.NodeManager.IsNodeRunning())
// finally stop the node
time.Sleep(100 * time.Millisecond) //https://github.com/status-im/status-go/issues/429#issuecomment-339663163
nodeStopped, err = s.NodeManager.StopNode()
s.NoError(err)
<-nodeStopped
@ -249,6 +254,7 @@ func (s *ManagerTestSuite) TestNetworkSwitching() {
s.Equal(GetHeadHash(), firstHash)
// now stop node, and make sure that a new node, on different network can be started
time.Sleep(100 * time.Millisecond) //https://github.com/status-im/status-go/issues/429#issuecomment-339663163
nodeStopped, err := s.NodeManager.StopNode()
s.NoError(err)
<-nodeStopped
@ -268,6 +274,7 @@ func (s *ManagerTestSuite) TestNetworkSwitching() {
s.NoError(err)
s.Equal(GetHeadHashFromNetworkID(params.RinkebyNetworkID), firstHash)
time.Sleep(100 * time.Millisecond) //https://github.com/status-im/status-go/issues/429#issuecomment-339663163
nodeStopped, err = s.NodeManager.StopNode()
s.NoError(err)
<-nodeStopped
@ -291,6 +298,8 @@ func (s *ManagerTestSuite) TestStartNodeWithUpstreamEnabled() {
s.NoError(err)
<-nodeStarted
s.True(s.NodeManager.IsNodeRunning())
time.Sleep(100 * time.Millisecond) //https://github.com/status-im/status-go/issues/429#issuecomment-339663163
nodeStopped, err := s.NodeManager.StopNode()
s.NoError(err)
<-nodeStopped
@ -336,129 +345,129 @@ func (s *ManagerTestSuite) TestRestartNode() {
}
// TODO(adam): race conditions should be tested with -race flag and unit tests, if possible.
// TODO(boris): going via https://github.com/status-im/status-go/pull/433#issuecomment-342232645 . Testing should be with -race flag
// Research if it's possible to do the same with unit tests.
func (s *ManagerTestSuite) TestRaceConditions() {
cnt := 25
progress := make(chan struct{}, cnt)
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
nodeConfig1, e := e2e.MakeTestNodeConfig(GetNetworkID())
s.NoError(e)
nodeConfig2, e := e2e.MakeTestNodeConfig(GetNetworkID())
s.NoError(e)
nodeConfigs := []*params.NodeConfig{nodeConfig1, nodeConfig2}
var funcsToTest = []func(*params.NodeConfig){
func(config *params.NodeConfig) {
log.Info("StartNode()")
_, err := s.NodeManager.StartNode(config)
s.T().Logf("StartNode() for network: %d, error: %v", config.NetworkID, err)
progress <- struct{}{}
},
func(config *params.NodeConfig) {
log.Info("StopNode()")
_, err := s.NodeManager.StopNode()
s.T().Logf("StopNode() for network: %d, error: %v", config.NetworkID, err)
progress <- struct{}{}
},
func(config *params.NodeConfig) {
log.Info("Node()")
_, err := s.NodeManager.Node()
s.T().Logf("Node(), error: %v", err)
progress <- struct{}{}
},
func(config *params.NodeConfig) {
log.Info("IsNodeRunning()")
s.T().Logf("IsNodeRunning(), result: %v", s.NodeManager.IsNodeRunning())
progress <- struct{}{}
},
func(config *params.NodeConfig) {
log.Info("PopulateStaticPeers()")
s.T().Logf("PopulateStaticPeers(), error: %v", s.NodeManager.PopulateStaticPeers())
progress <- struct{}{}
},
// TODO(adam): quarantined until it uses a different datadir
// as otherwise it wipes out cached blockchain data.
// func(config *params.NodeConfig) {
// log.Info("ResetChainData()")
// _, err := s.NodeManager.ResetChainData()
// s.T().Logf("ResetChainData(), error: %v", err)
// progress <- struct{}{}
// },
func(config *params.NodeConfig) {
log.Info("RestartNode()")
_, err := s.NodeManager.RestartNode()
s.T().Logf("RestartNode(), error: %v", err)
progress <- struct{}{}
},
func(config *params.NodeConfig) {
log.Info("NodeConfig()")
_, err := s.NodeManager.NodeConfig()
s.T().Logf("NodeConfig(), error: %v", err)
progress <- struct{}{}
},
func(config *params.NodeConfig) {
log.Info("LightEthereumService()")
_, err := s.NodeManager.LightEthereumService()
s.T().Logf("LightEthereumService(), error: %v", err)
progress <- struct{}{}
},
func(config *params.NodeConfig) {
log.Info("WhisperService()")
_, err := s.NodeManager.WhisperService()
s.T().Logf("WhisperService(), error: %v", err)
progress <- struct{}{}
},
func(config *params.NodeConfig) {
log.Info("AccountManager()")
_, err := s.NodeManager.AccountManager()
s.T().Logf("AccountManager(), error: %v", err)
progress <- struct{}{}
},
func(config *params.NodeConfig) {
log.Info("AccountKeyStore()")
_, err := s.NodeManager.AccountKeyStore()
s.T().Logf("AccountKeyStore(), error: %v", err)
progress <- struct{}{}
},
func(config *params.NodeConfig) {
log.Info("RPCClient()")
s.NodeManager.RPCClient()
progress <- struct{}{}
},
}
// increase StartNode()/StopNode() population
for i := 0; i < 5; i++ {
funcsToTest = append(funcsToTest, funcsToTest[0], funcsToTest[1])
}
for i := 0; i < cnt; i++ {
randConfig := nodeConfigs[rnd.Intn(len(nodeConfigs))]
randFunc := funcsToTest[rnd.Intn(len(funcsToTest))]
if rnd.Intn(100) > 75 { // introduce random delays
time.Sleep(500 * time.Millisecond)
}
go randFunc(randConfig)
}
for range progress {
cnt -= 1
if cnt <= 0 {
break
}
}
time.Sleep(2 * time.Second) // so that we see some logs
nodeStopped, _ := s.NodeManager.StopNode() // just in case we have a node running
if nodeStopped != nil {
<-nodeStopped
}
}
//func (s *ManagerTestSuite) TestRaceConditions() {
// cnt := 25
// progress := make(chan struct{}, cnt)
// rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
//
// nodeConfig1, e := e2e.MakeTestNodeConfig(GetNetworkID())
// s.NoError(e)
//
// nodeConfig2, e := e2e.MakeTestNodeConfig(GetNetworkID())
// s.NoError(e)
//
// nodeConfigs := []*params.NodeConfig{nodeConfig1, nodeConfig2}
//
// var funcsToTest = []func(*params.NodeConfig){
// func(config *params.NodeConfig) {
// log.Info("StartNode()")
// _, err := s.NodeManager.StartNode(config)
// s.T().Logf("StartNode() for network: %d, error: %v", config.NetworkID, err)
// progress <- struct{}{}
// },
// func(config *params.NodeConfig) {
// _, err := s.NodeManager.StopNode()
// s.T().Logf("StopNode() for network: %d, error: %v", config.NetworkID, err)
// progress <- struct{}{}
// },
// func(config *params.NodeConfig) {
// log.Info("Node()")
// _, err := s.NodeManager.Node()
// s.T().Logf("Node(), error: %v", err)
// progress <- struct{}{}
// },
// func(config *params.NodeConfig) {
// log.Info("IsNodeRunning()")
// s.T().Logf("IsNodeRunning(), result: %v", s.NodeManager.IsNodeRunning())
// progress <- struct{}{}
// },
// func(config *params.NodeConfig) {
// log.Info("PopulateStaticPeers()")
// s.T().Logf("PopulateStaticPeers(), error: %v", s.NodeManager.PopulateStaticPeers())
// progress <- struct{}{}
// },
// // TODO(adam): quarantined until it uses a different datadir
// // as otherwise it wipes out cached blockchain data.
// // func(config *params.NodeConfig) {
// // log.Info("ResetChainData()")
// // _, err := s.NodeManager.ResetChainData()
// // s.T().Logf("ResetChainData(), error: %v", err)
// // progress <- struct{}{}
// // },
// func(config *params.NodeConfig) {
// log.Info("RestartNode()")
// _, err := s.NodeManager.RestartNode()
// s.T().Logf("RestartNode(), error: %v", err)
// progress <- struct{}{}
// },
// func(config *params.NodeConfig) {
// log.Info("NodeConfig()")
// _, err := s.NodeManager.NodeConfig()
// s.T().Logf("NodeConfig(), error: %v", err)
// progress <- struct{}{}
// },
// func(config *params.NodeConfig) {
// log.Info("LightEthereumService()")
// _, err := s.NodeManager.LightEthereumService()
// s.T().Logf("LightEthereumService(), error: %v", err)
// progress <- struct{}{}
// },
// func(config *params.NodeConfig) {
// log.Info("WhisperService()")
// _, err := s.NodeManager.WhisperService()
// s.T().Logf("WhisperService(), error: %v", err)
// progress <- struct{}{}
// },
// func(config *params.NodeConfig) {
// log.Info("AccountManager()")
// _, err := s.NodeManager.AccountManager()
// s.T().Logf("AccountManager(), error: %v", err)
// progress <- struct{}{}
// },
// func(config *params.NodeConfig) {
// log.Info("AccountKeyStore()")
// _, err := s.NodeManager.AccountKeyStore()
// s.T().Logf("AccountKeyStore(), error: %v", err)
// progress <- struct{}{}
// },
// func(config *params.NodeConfig) {
// log.Info("RPCClient()")
// s.NodeManager.RPCClient()
// progress <- struct{}{}
// },
// }
//
// // increase StartNode()/StopNode() population
// for i := 0; i < 5; i++ {
// funcsToTest = append(funcsToTest, funcsToTest[0], funcsToTest[1])
// }
//
// for i := 0; i < cnt; i++ {
// randConfig := nodeConfigs[rnd.Intn(len(nodeConfigs))]
// randFunc := funcsToTest[rnd.Intn(len(funcsToTest))]
//
// if rnd.Intn(100) > 75 { // introduce random delays
// time.Sleep(500 * time.Millisecond)
// }
// go randFunc(randConfig)
// }
//
// for range progress {
// cnt -= 1
// if cnt <= 0 {
// break
// }
// }
//
// time.Sleep(2 * time.Second) // so that we see some logs
// nodeStopped, _ := s.NodeManager.StopNode() // just in case we have a node running
//
// if nodeStopped != nil {
// <-nodeStopped
// }
//}
func (s *ManagerTestSuite) TestNodeStartCrash() {
// let's listen for node.crashed signal
@ -510,6 +519,7 @@ func (s *ManagerTestSuite) TestNodeStartCrash() {
}
// cleanup
s.NodeManager.StopNode() //nolint: errcheck
time.Sleep(100 * time.Millisecond) //https://github.com/status-im/status-go/issues/429#issuecomment-339663163
s.NodeManager.StopNode() //nolint: errcheck
signal.ResetDefaultNodeNotificationHandler()
}