2017-05-25 11:23:01 +00:00
|
|
|
package api_test
|
|
|
|
|
|
|
|
import (
|
2017-05-27 20:26:07 +00:00
|
|
|
"io/ioutil"
|
2017-05-25 13:14:52 +00:00
|
|
|
"math/rand"
|
2017-05-27 20:26:07 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
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
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2017-05-25 11:23:01 +00:00
|
|
|
"github.com/status-im/status-go/geth/api"
|
|
|
|
"github.com/status-im/status-go/geth/params"
|
|
|
|
. "github.com/status-im/status-go/geth/testing"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAPI(t *testing.T) {
|
|
|
|
suite.Run(t, new(APITestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type APITestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
api *api.StatusAPI
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *APITestSuite) SetupTest() {
|
|
|
|
require := s.Require()
|
|
|
|
statusAPI := api.NewStatusAPI()
|
|
|
|
require.NotNil(statusAPI)
|
|
|
|
require.IsType(&api.StatusAPI{}, statusAPI)
|
|
|
|
s.api = statusAPI
|
|
|
|
}
|
2017-05-27 20:26:07 +00:00
|
|
|
|
|
|
|
func (s *APITestSuite) TestCHTUpdate() {
|
|
|
|
require := s.Require()
|
|
|
|
require.NotNil(s.api)
|
|
|
|
|
|
|
|
tmpDir, err := ioutil.TempDir(os.TempDir(), "cht-updates")
|
|
|
|
require.NoError(err)
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
url := "https://gist.githubusercontent.com/tiabc/83ed515fbb0c0e9d39700a6279072b6a/raw/a8c7b08488fab3c1d9139b18af33da3df823e3ff/cht.json?u=" + strconv.Itoa(int(time.Now().Unix()))
|
2017-05-27 20:26:07 +00:00
|
|
|
configJSON := `{
|
|
|
|
"NetworkId": ` + strconv.Itoa(params.RopstenNetworkID) + `,
|
|
|
|
"DataDir": "` + tmpDir + `",
|
|
|
|
"LogEnabled": true,
|
|
|
|
"LogLevel": "INFO",
|
2017-07-13 06:54:10 +00:00
|
|
|
"RPCEnabled": true,
|
2017-05-27 20:26:07 +00:00
|
|
|
"LightEthConfig": {
|
|
|
|
"CHTRootConfigURL": "` + url + `"
|
|
|
|
}
|
|
|
|
}`
|
2017-08-04 16:14:17 +00:00
|
|
|
//nodeConfig, err := params.LoadNodeConfig(configJSON)
|
|
|
|
_, err = params.LoadNodeConfig(configJSON)
|
2017-05-27 20:26:07 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// start node
|
2017-08-04 16:14:17 +00:00
|
|
|
//nodeConfig.DevMode = true
|
|
|
|
//s.api.StartNode(nodeConfig)
|
|
|
|
//s.api.StopNode()
|
|
|
|
// 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() {
|
2017-05-25 11:23:01 +00:00
|
|
|
require := s.Require()
|
2017-05-25 13:14:52 +00:00
|
|
|
require.NotNil(s.api)
|
|
|
|
|
|
|
|
cnt := 25
|
|
|
|
progress := make(chan struct{}, cnt)
|
|
|
|
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
|
|
|
nodeConfig1, err := MakeTestNodeConfig(params.RopstenNetworkID)
|
|
|
|
require.NoError(err)
|
2017-05-25 11:23:01 +00:00
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
nodeConfig2, err := MakeTestNodeConfig(params.RinkebyNetworkID)
|
2017-05-25 11:23:01 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
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) {
|
|
|
|
log.Info("StartNodeAsync()")
|
|
|
|
_, err := s.api.StartNodeAsync(config)
|
|
|
|
s.T().Logf("StartNodeAsync() for network: %d, error: %v", config.NetworkID, err)
|
|
|
|
progress <- struct{}{}
|
|
|
|
},
|
|
|
|
func(config *params.NodeConfig) {
|
|
|
|
log.Info("StopNodeAsync()")
|
|
|
|
_, err := s.api.StopNodeAsync()
|
|
|
|
s.T().Logf("StopNodeAsync(), error: %v", err)
|
|
|
|
progress <- struct{}{}
|
|
|
|
},
|
|
|
|
func(config *params.NodeConfig) {
|
|
|
|
log.Info("RestartNodeAsync()")
|
|
|
|
_, err := s.api.RestartNodeAsync()
|
|
|
|
s.T().Logf("RestartNodeAsync(), error: %v", err)
|
|
|
|
progress <- struct{}{}
|
|
|
|
},
|
|
|
|
func(config *params.NodeConfig) {
|
|
|
|
log.Info("ResetChainDataAsync()")
|
|
|
|
_, err := s.api.ResetChainDataAsync()
|
|
|
|
s.T().Logf("ResetChainDataAsync(), error: %v", err)
|
|
|
|
progress <- struct{}{}
|
|
|
|
},
|
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)
|
|
|
|
}
|
|
|
|
go randFunc(randConfig)
|
2017-05-25 11:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for range progress {
|
2017-05-25 13:14:52 +00:00
|
|
|
cnt -= 1
|
|
|
|
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
|
|
|
|
s.api.StopNode() // just in case we have a node running
|
2017-05-25 11:23:01 +00:00
|
|
|
}
|