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.

100 lines
2.5 KiB

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