status-go/t/e2e/api/api_test.go

203 lines
5.9 KiB
Go
Raw Normal View History

package api_test
import (
"encoding/json"
"math/rand"
"testing"
2017-05-25 12:34:13 +00:00
"time"
"github.com/status-im/status-go/api"
"github.com/status-im/status-go/node"
"github.com/status-im/status-go/params"
2018-05-03 07:35:58 +00:00
"github.com/status-im/status-go/signal"
"github.com/status-im/status-go/t/utils"
"github.com/stretchr/testify/suite"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/storage"
)
func TestAPI(t *testing.T) {
utils.Init()
suite.Run(t, new(APITestSuite))
}
type APITestSuite struct {
suite.Suite
2018-06-19 07:49:24 +00:00
backend *api.StatusBackend
}
func (s *APITestSuite) ensureNodeStopped() {
2018-06-19 07:49:24 +00:00
if err := s.backend.StopNode(); err != node.ErrNoRunningNode && err != nil {
s.NoError(err, "unexpected error")
}
}
func (s *APITestSuite) SetupTest() {
2018-06-19 07:49:24 +00:00
s.backend = api.NewStatusBackend()
s.NotNil(s.backend)
}
2017-05-27 20:26:07 +00:00
func (s *APITestSuite) TestCHTUpdate() {
// TODO(tiabc): Test that CHT is really updated.
2017-05-27 20:26:07 +00:00
}
func (s *APITestSuite) TestRaceConditions() {
cnt := 25
progress := make(chan struct{}, cnt)
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
nodeConfig1, err := utils.MakeTestNodeConfig(utils.GetNetworkID())
s.NoError(err)
nodeConfig2, err := utils.MakeTestNodeConfig(utils.GetNetworkID())
s.NoError(err)
nodeConfigs := []*params.NodeConfig{nodeConfig1, nodeConfig2}
var funcsToTest = []func(*params.NodeConfig){
func(config *params.NodeConfig) {
2018-06-19 07:49:24 +00:00
s.T().Logf("async call to StartNode() for network: %d", config.NetworkID)
api.RunAsync(func() error { return s.backend.StartNode(config) })
progress <- struct{}{}
},
func(config *params.NodeConfig) {
2018-06-19 07:49:24 +00:00
s.T().Logf("async call to StopNode() for network: %d", config.NetworkID)
api.RunAsync(s.backend.StopNode)
progress <- struct{}{}
},
func(config *params.NodeConfig) {
2018-06-19 07:49:24 +00:00
s.T().Logf("async call to RestartNode() for network: %d", config.NetworkID)
api.RunAsync(s.backend.RestartNode)
progress <- struct{}{}
},
// TODO(adam): quarantined until it uses a different datadir
// as otherwise it wipes out cached blockchain data.
// func(config *params.NodeConfig) {
2018-06-19 07:49:24 +00:00
// s.T().Logf("async call to ResetChainData() for network: %d", config.NetworkID)
// _, err := s.api.ResetChainData()
// 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))]
2017-05-25 12:34:13 +00:00
if rnd.Intn(100) > 75 { // introduce random delays
time.Sleep(500 * time.Millisecond)
}
s.NoError(s.backend.AccountManager().InitKeystore(randConfig.KeyStoreDir))
go randFunc(randConfig)
}
for range progress {
cnt--
if cnt <= 0 {
break
}
}
time.Sleep(2 * time.Second) // so that we see some logs
// just in case we have a node running
s.ensureNodeStopped()
}
2018-02-14 16:32:36 +00:00
func (s *APITestSuite) TestEventsNodeStartStop() {
envelopes := make(chan signal.Envelope, 3)
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope)
s.NoError(err)
// whitelist types that we are interested in
switch envelope.Type {
case signal.EventNodeStarted:
case signal.EventNodeStopped:
case signal.EventNodeReady:
default:
return
}
2018-02-14 16:32:36 +00:00
envelopes <- envelope
})
nodeConfig, err := utils.MakeTestNodeConfig(utils.GetNetworkID())
2018-02-14 16:32:36 +00:00
s.NoError(err)
s.NoError(s.backend.AccountManager().InitKeystore(nodeConfig.KeyStoreDir))
s.Require().NoError(s.backend.StartNode(nodeConfig))
2018-06-19 07:49:24 +00:00
s.NoError(s.backend.StopNode())
2018-02-14 16:32:36 +00:00
s.verifyEnvelopes(envelopes, signal.EventNodeStarted, signal.EventNodeReady, signal.EventNodeStopped)
s.Require().NoError(s.backend.StartNode(nodeConfig))
2018-02-14 16:32:36 +00:00
s.verifyEnvelopes(envelopes, signal.EventNodeStarted, signal.EventNodeReady)
s.Require().NoError(s.backend.RestartNode())
2018-02-14 16:32:36 +00:00
s.verifyEnvelopes(envelopes, signal.EventNodeStopped, signal.EventNodeStarted, signal.EventNodeReady)
2018-06-19 07:49:24 +00:00
s.NoError(s.backend.StopNode())
2018-02-14 16:32:36 +00:00
s.verifyEnvelopes(envelopes, signal.EventNodeStopped)
}
func (s *APITestSuite) verifyEnvelopes(envelopes chan signal.Envelope, envelopeTypes ...string) {
for _, envelopeType := range envelopeTypes {
select {
case env := <-envelopes:
s.Equal(envelopeType, env.Type)
case <-time.After(1 * time.Second):
s.Fail("timeout waiting for envelope")
}
}
}
func (s *APITestSuite) TestNodeStartCrash() {
// let's listen for node.crashed signal
signalReceived := make(chan struct{})
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
var envelope signal.Envelope
err := json.Unmarshal([]byte(jsonEvent), &envelope)
s.NoError(err)
if envelope.Type == signal.EventNodeCrashed {
close(signalReceived)
}
})
defer signal.ResetDefaultNodeNotificationHandler()
nodeConfig, err := utils.MakeTestNodeConfig(utils.GetNetworkID())
s.NoError(err)
db, err := leveldb.Open(storage.NewMemStorage(), nil)
s.NoError(err)
defer func() { s.NoError(db.Close()) }()
// start node outside the manager (on the same port), so that manager node.Start() method fails
outsideNode, err := node.MakeNode(nodeConfig, nil, db)
s.NoError(err)
err = outsideNode.Start()
s.NoError(err)
// now try starting using node manager, it should fail (error is irrelevant as it is implementation detail)
s.NoError(s.backend.AccountManager().InitKeystore(nodeConfig.KeyStoreDir))
2018-06-19 07:49:24 +00:00
s.Error(<-api.RunAsync(func() error { return s.backend.StartNode(nodeConfig) }))
select {
case <-time.After(500 * time.Millisecond):
s.FailNow("timed out waiting for signal")
case <-signalReceived:
}
// stop outside node, and re-try
s.NoError(outsideNode.Stop())
signalReceived = make(chan struct{})
2018-06-19 07:49:24 +00:00
s.NoError(<-api.RunAsync(func() error { return s.backend.StartNode(nodeConfig) }))
select {
case <-time.After(500 * time.Millisecond):
case <-signalReceived:
s.FailNow("signal should not be received")
}
// cleanup
2018-06-19 07:49:24 +00:00
s.NoError(s.backend.StopNode())
}