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.

72 lines
2.1 KiB

  1. package monitor_test
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/go-amino"
  8. crypto "github.com/tendermint/tendermint/crypto"
  9. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  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. func TestMonitorUpdatesNumberOfValidators(t *testing.T) {
  15. m := startMonitor(t)
  16. defer m.Stop()
  17. n, _ := createValidatorNode(t)
  18. m.Monitor(n)
  19. assert.Equal(t, 1, m.Network.NumNodesMonitored)
  20. assert.Equal(t, 1, m.Network.NumNodesMonitoredOnline)
  21. time.Sleep(1 * time.Second)
  22. // DATA RACE
  23. // assert.Equal(t, 1, m.Network.NumValidators())
  24. }
  25. func TestMonitorRecalculatesNetworkUptime(t *testing.T) {
  26. m := startMonitor(t)
  27. defer m.Stop()
  28. assert.Equal(t, 100.0, m.Network.Uptime())
  29. n, _ := createValidatorNode(t)
  30. m.Monitor(n)
  31. m.Network.NodeIsDown(n.Name) // simulate node failure
  32. time.Sleep(200 * time.Millisecond)
  33. m.Network.NodeIsOnline(n.Name)
  34. time.Sleep(1 * time.Second)
  35. assert.True(t, m.Network.Uptime() < 100.0, "Uptime should be less than 100%")
  36. }
  37. func startMonitor(t *testing.T) *monitor.Monitor {
  38. m := monitor.NewMonitor(
  39. monitor.SetNumValidatorsUpdateInterval(200*time.Millisecond),
  40. monitor.RecalculateNetworkUptimeEvery(200*time.Millisecond),
  41. )
  42. err := m.Start()
  43. require.Nil(t, err)
  44. return m
  45. }
  46. func createValidatorNode(t *testing.T) (n *monitor.Node, emMock *mock.EventMeter) {
  47. emMock = &mock.EventMeter{}
  48. stubs := make(map[string]interface{})
  49. pubKey := crypto.GenPrivKeyEd25519().PubKey()
  50. stubs["validators"] = ctypes.ResultValidators{BlockHeight: blockHeight, Validators: []*tmtypes.Validator{tmtypes.NewValidator(pubKey, 0)}}
  51. stubs["status"] = ctypes.ResultStatus{ValidatorInfo: ctypes.ValidatorInfo{PubKey: pubKey}}
  52. cdc := amino.NewCodec()
  53. rpcClientMock := &mock.RpcClient{Stubs: stubs}
  54. rpcClientMock.SetCodec(cdc)
  55. n = monitor.NewNodeWithEventMeterAndRpcClient("tcp://127.0.0.1:26657", emMock, rpcClientMock)
  56. return
  57. }