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.

103 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.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, <-disconnectCh)
  53. // we're back in a race
  54. assert.Equal(true, n.Online)
  55. }
  56. func TestNumValidators(t *testing.T) {
  57. assert := assert.New(t)
  58. n, _ := startValidatorNode(t)
  59. defer n.Stop()
  60. height, num, err := n.NumValidators()
  61. assert.Nil(err)
  62. assert.Equal(uint64(blockHeight), height)
  63. assert.Equal(1, num)
  64. }
  65. func startValidatorNode(t *testing.T) (n *monitor.Node, emMock *mock.EventMeter) {
  66. emMock = &mock.EventMeter{}
  67. stubs := make(map[string]ctypes.TMResult)
  68. pubKey := crypto.GenPrivKeyEd25519().PubKey()
  69. stubs["validators"] = &ctypes.ResultValidators{BlockHeight: blockHeight, Validators: []*tmtypes.Validator{tmtypes.NewValidator(pubKey, 0)}}
  70. stubs["status"] = &ctypes.ResultStatus{PubKey: pubKey}
  71. rpcClientMock := &mock.RpcClient{stubs}
  72. n = monitor.NewNodeWithEventMeterAndRpcClient("tcp://127.0.0.1:46657", emMock, rpcClientMock)
  73. err := n.Start()
  74. require.Nil(t, err)
  75. return
  76. }