You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.6 KiB

  1. package monitor_test
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. amino "github.com/tendermint/go-amino"
  7. "github.com/tendermint/tendermint/crypto/ed25519"
  8. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  9. em "github.com/tendermint/tendermint/tools/tm-monitor/eventmeter"
  10. mock "github.com/tendermint/tendermint/tools/tm-monitor/mock"
  11. monitor "github.com/tendermint/tendermint/tools/tm-monitor/monitor"
  12. tmtypes "github.com/tendermint/tendermint/types"
  13. )
  14. const (
  15. blockHeight = int64(1)
  16. )
  17. func TestNodeStartStop(t *testing.T) {
  18. n, _ := startValidatorNode(t)
  19. defer n.Stop()
  20. assert.Equal(t, true, n.Online)
  21. assert.Equal(t, true, n.IsValidator)
  22. }
  23. func TestNodeNewBlockReceived(t *testing.T) {
  24. blockCh := make(chan *tmtypes.Block, 100)
  25. n, emMock := startValidatorNode(t)
  26. defer n.Stop()
  27. n.SendBlocksTo(blockCh)
  28. block := &tmtypes.Block{Header: tmtypes.Header{Height: 5}}
  29. emMock.Call("eventCallback", &em.EventMetric{}, tmtypes.EventDataNewBlock{Block: block})
  30. assert.Equal(t, int64(5), n.Height)
  31. assert.Equal(t, block, <-blockCh)
  32. }
  33. func TestNodeNewBlockLatencyReceived(t *testing.T) {
  34. blockLatencyCh := make(chan float64, 100)
  35. n, emMock := startValidatorNode(t)
  36. defer n.Stop()
  37. n.SendBlockLatenciesTo(blockLatencyCh)
  38. emMock.Call("latencyCallback", 1000000.0)
  39. assert.Equal(t, 1.0, n.BlockLatency)
  40. assert.Equal(t, 1000000.0, <-blockLatencyCh)
  41. }
  42. func TestNodeConnectionLost(t *testing.T) {
  43. disconnectCh := make(chan bool, 100)
  44. n, emMock := startValidatorNode(t)
  45. defer n.Stop()
  46. n.NotifyAboutDisconnects(disconnectCh)
  47. emMock.Call("disconnectCallback")
  48. assert.Equal(t, true, <-disconnectCh)
  49. assert.Equal(t, false, n.Online)
  50. }
  51. func TestNumValidators(t *testing.T) {
  52. n, _ := startValidatorNode(t)
  53. defer n.Stop()
  54. height, num, err := n.NumValidators()
  55. assert.Nil(t, err)
  56. assert.Equal(t, blockHeight, height)
  57. assert.Equal(t, 1, num)
  58. }
  59. func startValidatorNode(t *testing.T) (n *monitor.Node, emMock *mock.EventMeter) {
  60. emMock = &mock.EventMeter{}
  61. stubs := make(map[string]interface{})
  62. pubKey := ed25519.GenPrivKey().PubKey()
  63. stubs["validators"] = ctypes.ResultValidators{
  64. BlockHeight: blockHeight,
  65. Validators: []*tmtypes.Validator{tmtypes.NewValidator(pubKey, 0)},
  66. }
  67. stubs["status"] = ctypes.ResultStatus{ValidatorInfo: ctypes.ValidatorInfo{PubKey: pubKey}}
  68. cdc := amino.NewCodec()
  69. rpcClientMock := &mock.RPCClient{Stubs: stubs}
  70. rpcClientMock.SetCodec(cdc)
  71. n = monitor.NewNodeWithEventMeterAndRPCClient("tcp://127.0.0.1:26657", emMock, rpcClientMock)
  72. err := n.Start()
  73. require.Nil(t, err)
  74. return
  75. }