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.

90 lines
2.4 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 = int64(1)
  15. )
  16. func TestNodeStartStop(t *testing.T) {
  17. n, _ := startValidatorNode(t)
  18. defer n.Stop()
  19. assert.Equal(t, true, n.Online)
  20. assert.Equal(t, true, n.IsValidator)
  21. }
  22. func TestNodeNewBlockReceived(t *testing.T) {
  23. blockCh := make(chan tmtypes.Header, 100)
  24. n, emMock := startValidatorNode(t)
  25. defer n.Stop()
  26. n.SendBlocksTo(blockCh)
  27. blockHeader := &tmtypes.Header{Height: 5}
  28. emMock.Call("eventCallback", &em.EventMetric{}, tmtypes.TMEventData{tmtypes.EventDataNewBlockHeader{blockHeader}})
  29. assert.Equal(t, int64(5), n.Height)
  30. assert.Equal(t, *blockHeader, <-blockCh)
  31. }
  32. func TestNodeNewBlockLatencyReceived(t *testing.T) {
  33. blockLatencyCh := make(chan float64, 100)
  34. n, emMock := startValidatorNode(t)
  35. defer n.Stop()
  36. n.SendBlockLatenciesTo(blockLatencyCh)
  37. emMock.Call("latencyCallback", 1000000.0)
  38. assert.Equal(t, 1.0, n.BlockLatency)
  39. assert.Equal(t, 1000000.0, <-blockLatencyCh)
  40. }
  41. func TestNodeConnectionLost(t *testing.T) {
  42. disconnectCh := make(chan bool, 100)
  43. n, emMock := startValidatorNode(t)
  44. defer n.Stop()
  45. n.NotifyAboutDisconnects(disconnectCh)
  46. emMock.Call("disconnectCallback")
  47. assert.Equal(t, true, <-disconnectCh)
  48. assert.Equal(t, false, n.Online)
  49. }
  50. func TestNumValidators(t *testing.T) {
  51. n, _ := startValidatorNode(t)
  52. defer n.Stop()
  53. height, num, err := n.NumValidators()
  54. assert.Nil(t, err)
  55. assert.Equal(t, blockHeight, height)
  56. assert.Equal(t, 1, num)
  57. }
  58. func startValidatorNode(t *testing.T) (n *monitor.Node, emMock *mock.EventMeter) {
  59. emMock = &mock.EventMeter{}
  60. stubs := make(map[string]interface{})
  61. pubKey := crypto.GenPrivKeyEd25519().PubKey()
  62. stubs["validators"] = ctypes.ResultValidators{BlockHeight: blockHeight, Validators: []*tmtypes.Validator{tmtypes.NewValidator(pubKey, 0)}}
  63. stubs["status"] = ctypes.ResultStatus{PubKey: pubKey}
  64. rpcClientMock := &mock.RpcClient{stubs}
  65. n = monitor.NewNodeWithEventMeterAndRpcClient("tcp://127.0.0.1:46657", emMock, rpcClientMock)
  66. err := n.Start()
  67. require.Nil(t, err)
  68. return
  69. }