2017-05-25 11:23:01 +00:00
|
|
|
package api_test
|
|
|
|
|
|
|
|
import (
|
2018-02-09 13:37:56 +00:00
|
|
|
"encoding/json"
|
2017-05-25 13:14:52 +00:00
|
|
|
"math/rand"
|
2017-05-25 11:23:01 +00:00
|
|
|
"testing"
|
2017-05-25 12:34:13 +00:00
|
|
|
"time"
|
2017-05-25 11:23:01 +00:00
|
|
|
|
2018-06-08 11:29:50 +00:00
|
|
|
"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"
|
2019-10-04 15:21:24 +00:00
|
|
|
"github.com/status-im/status-go/t/utils"
|
2017-05-25 11:23:01 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2018-05-02 12:14:08 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/storage"
|
2017-05-25 11:23:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAPI(t *testing.T) {
|
2019-10-04 15:21:24 +00:00
|
|
|
utils.Init()
|
2017-05-25 11:23:01 +00:00
|
|
|
suite.Run(t, new(APITestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type APITestSuite struct {
|
|
|
|
suite.Suite
|
2018-06-19 07:49:24 +00:00
|
|
|
backend *api.StatusBackend
|
2017-05-25 11:23:01 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 13:37:56 +00:00
|
|
|
func (s *APITestSuite) ensureNodeStopped() {
|
2018-06-19 07:49:24 +00:00
|
|
|
if err := s.backend.StopNode(); err != node.ErrNoRunningNode && err != nil {
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(err, "unexpected error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 11:23:01 +00:00
|
|
|
func (s *APITestSuite) SetupTest() {
|
2018-06-19 07:49:24 +00:00
|
|
|
s.backend = api.NewStatusBackend()
|
|
|
|
s.NotNil(s.backend)
|
2017-05-25 11:23:01 +00:00
|
|
|
}
|
2017-05-27 20:26:07 +00:00
|
|
|
|
|
|
|
func (s *APITestSuite) TestCHTUpdate() {
|
2017-08-04 16:14:17 +00:00
|
|
|
// TODO(tiabc): Test that CHT is really updated.
|
2017-05-27 20:26:07 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
func (s *APITestSuite) TestRaceConditions() {
|
|
|
|
cnt := 25
|
|
|
|
progress := make(chan struct{}, cnt)
|
|
|
|
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
nodeConfig1, err := utils.MakeTestNodeConfig(utils.GetNetworkID())
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
2017-05-25 11:23:01 +00:00
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
nodeConfig2, err := utils.MakeTestNodeConfig(utils.GetNetworkID())
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
2017-05-25 11:23:01 +00:00
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
nodeConfigs := []*params.NodeConfig{nodeConfig1, nodeConfig2}
|
2017-05-25 11:23:01 +00:00
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
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) })
|
2017-05-25 13:14:52 +00:00
|
|
|
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)
|
2017-05-25 13:14:52 +00:00
|
|
|
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)
|
2017-05-25 13:14:52 +00:00
|
|
|
progress <- struct{}{}
|
|
|
|
},
|
2017-10-11 14:20:51 +00:00
|
|
|
// 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()
|
2018-02-09 13:37:56 +00:00
|
|
|
// progress <- struct{}{}
|
2017-10-11 14:20:51 +00:00
|
|
|
// },
|
2017-05-25 11:23:01 +00:00
|
|
|
}
|
2017-05-25 13:14:52 +00:00
|
|
|
|
|
|
|
// increase StartNode()/StopNode() population
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
funcsToTest = append(funcsToTest, funcsToTest[0], funcsToTest[1])
|
2017-05-25 11:23:01 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
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
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
if rnd.Intn(100) > 75 { // introduce random delays
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
}
|
2019-08-20 15:38:40 +00:00
|
|
|
s.NoError(s.backend.AccountManager().InitKeystore(randConfig.KeyStoreDir))
|
2017-05-25 13:14:52 +00:00
|
|
|
go randFunc(randConfig)
|
2017-05-25 11:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for range progress {
|
2018-06-12 16:50:25 +00:00
|
|
|
cnt--
|
2017-05-25 13:14:52 +00:00
|
|
|
if cnt <= 0 {
|
2017-05-25 11:23:01 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 13:14:52 +00:00
|
|
|
|
|
|
|
time.Sleep(2 * time.Second) // so that we see some logs
|
2017-10-20 09:06:22 +00:00
|
|
|
// just in case we have a node running
|
2018-02-09 13:37:56 +00:00
|
|
|
s.ensureNodeStopped()
|
2017-05-25 11:23:01 +00:00
|
|
|
}
|
2017-10-16 21:07:42 +00:00
|
|
|
|
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)
|
2018-04-25 07:13:59 +00:00
|
|
|
// 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
|
|
|
|
})
|
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
nodeConfig, err := utils.MakeTestNodeConfig(utils.GetNetworkID())
|
2018-02-14 16:32:36 +00:00
|
|
|
s.NoError(err)
|
2019-08-20 15:38:40 +00:00
|
|
|
s.NoError(s.backend.AccountManager().InitKeystore(nodeConfig.KeyStoreDir))
|
2018-09-13 16:31:29 +00:00
|
|
|
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)
|
2018-09-13 16:31:29 +00:00
|
|
|
s.Require().NoError(s.backend.StartNode(nodeConfig))
|
2018-02-14 16:32:36 +00:00
|
|
|
s.verifyEnvelopes(envelopes, signal.EventNodeStarted, signal.EventNodeReady)
|
2018-09-13 16:31:29 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 13:37:56 +00:00
|
|
|
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()
|
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
nodeConfig, err := utils.MakeTestNodeConfig(utils.GetNetworkID())
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
2018-05-02 12:14:08 +00:00
|
|
|
db, err := leveldb.Open(storage.NewMemStorage(), nil)
|
|
|
|
s.NoError(err)
|
|
|
|
defer func() { s.NoError(db.Close()) }()
|
|
|
|
|
2018-02-09 13:37:56 +00:00
|
|
|
// start node outside the manager (on the same port), so that manager node.Start() method fails
|
2019-08-20 15:38:40 +00:00
|
|
|
outsideNode, err := node.MakeNode(nodeConfig, nil, db)
|
2018-02-09 13:37:56 +00:00
|
|
|
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)
|
2019-08-20 15:38:40 +00:00
|
|
|
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) }))
|
2018-02-09 13:37:56 +00:00
|
|
|
|
|
|
|
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) }))
|
2018-02-09 13:37:56 +00:00
|
|
|
|
|
|
|
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())
|
2018-02-09 13:37:56 +00:00
|
|
|
}
|