tendermint/tools/tm-monitor/monitor/node_test.go

94 lines
2.5 KiB
Go
Raw Normal View History

package monitor_test
2017-02-24 06:54:36 -08:00
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto/ed25519"
2017-03-06 06:35:52 -08:00
ctypes "github.com/tendermint/tendermint/rpc/core/types"
em "github.com/tendermint/tendermint/tools/tm-monitor/eventmeter"
mock "github.com/tendermint/tendermint/tools/tm-monitor/mock"
monitor "github.com/tendermint/tendermint/tools/tm-monitor/monitor"
2017-02-24 06:54:36 -08:00
tmtypes "github.com/tendermint/tendermint/types"
)
2017-03-06 06:35:52 -08:00
const (
2018-04-03 04:02:09 -07:00
blockHeight = int64(1)
2017-03-06 06:35:52 -08:00
)
2017-02-24 06:54:36 -08:00
func TestNodeStartStop(t *testing.T) {
2017-03-06 06:35:52 -08:00
n, _ := startValidatorNode(t)
defer n.Stop()
2017-02-24 06:54:36 -08:00
2018-04-03 02:06:34 -07:00
assert.Equal(t, true, n.Online)
assert.Equal(t, true, n.IsValidator)
2017-02-24 06:54:36 -08:00
}
func TestNodeNewBlockReceived(t *testing.T) {
blockCh := make(chan tmtypes.Header, 100)
2017-03-06 06:35:52 -08:00
n, emMock := startValidatorNode(t)
defer n.Stop()
2017-02-24 06:54:36 -08:00
n.SendBlocksTo(blockCh)
blockHeader := tmtypes.Header{Height: 5}
emMock.Call("eventCallback", &em.EventMetric{}, tmtypes.EventDataNewBlockHeader{blockHeader})
2017-02-24 06:54:36 -08:00
2018-04-03 04:02:09 -07:00
assert.Equal(t, int64(5), n.Height)
assert.Equal(t, blockHeader, <-blockCh)
2017-02-24 06:54:36 -08:00
}
func TestNodeNewBlockLatencyReceived(t *testing.T) {
blockLatencyCh := make(chan float64, 100)
2017-03-06 06:35:52 -08:00
n, emMock := startValidatorNode(t)
defer n.Stop()
2017-02-24 06:54:36 -08:00
n.SendBlockLatenciesTo(blockLatencyCh)
emMock.Call("latencyCallback", 1000000.0)
2018-04-03 02:06:34 -07:00
assert.Equal(t, 1.0, n.BlockLatency)
assert.Equal(t, 1000000.0, <-blockLatencyCh)
2017-02-24 06:54:36 -08:00
}
func TestNodeConnectionLost(t *testing.T) {
disconnectCh := make(chan bool, 100)
2017-03-06 06:35:52 -08:00
n, emMock := startValidatorNode(t)
defer n.Stop()
2017-02-24 06:54:36 -08:00
n.NotifyAboutDisconnects(disconnectCh)
emMock.Call("disconnectCallback")
2018-04-03 02:06:34 -07:00
assert.Equal(t, true, <-disconnectCh)
assert.Equal(t, false, n.Online)
2017-02-24 06:54:36 -08:00
}
2017-03-06 06:35:52 -08:00
func TestNumValidators(t *testing.T) {
n, _ := startValidatorNode(t)
defer n.Stop()
height, num, err := n.NumValidators()
2018-04-03 02:06:34 -07:00
assert.Nil(t, err)
2018-04-03 04:02:09 -07:00
assert.Equal(t, blockHeight, height)
2018-04-03 02:06:34 -07:00
assert.Equal(t, 1, num)
2017-03-06 06:35:52 -08:00
}
func startValidatorNode(t *testing.T) (n *monitor.Node, emMock *mock.EventMeter) {
2017-02-24 06:54:36 -08:00
emMock = &mock.EventMeter{}
2017-03-06 06:35:52 -08:00
2017-07-29 10:50:33 -07:00
stubs := make(map[string]interface{})
pubKey := ed25519.GenPrivKey().PubKey()
2017-07-29 10:50:33 -07:00
stubs["validators"] = ctypes.ResultValidators{BlockHeight: blockHeight, Validators: []*tmtypes.Validator{tmtypes.NewValidator(pubKey, 0)}}
stubs["status"] = ctypes.ResultStatus{ValidatorInfo: ctypes.ValidatorInfo{PubKey: pubKey}}
cdc := amino.NewCodec()
rpcClientMock := &mock.RpcClient{Stubs: stubs}
rpcClientMock.SetCodec(cdc)
2017-03-06 06:35:52 -08:00
n = monitor.NewNodeWithEventMeterAndRpcClient("tcp://127.0.0.1:26657", emMock, rpcClientMock)
2017-02-24 06:54:36 -08:00
err := n.Start()
require.Nil(t, err)
return
}