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
1.9 KiB

  1. package monitor_test
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. crypto "github.com/tendermint/go-crypto"
  8. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  9. tmtypes "github.com/tendermint/tendermint/types"
  10. mock "github.com/tendermint/tools/tm-monitor/mock"
  11. monitor "github.com/tendermint/tools/tm-monitor/monitor"
  12. )
  13. func TestMonitorUpdatesNumberOfValidators(t *testing.T) {
  14. assert := assert.New(t)
  15. m := startMonitor(t)
  16. defer m.Stop()
  17. n, _ := createValidatorNode(t)
  18. m.Monitor(n)
  19. assert.Equal(1, m.Network.NumNodesMonitored)
  20. assert.Equal(1, m.Network.NumNodesMonitoredOnline)
  21. time.Sleep(1 * time.Second)
  22. assert.Equal(1, m.Network.NumValidators)
  23. }
  24. func TestMonitorRecalculatesNetworkUptime(t *testing.T) {
  25. assert := assert.New(t)
  26. m := startMonitor(t)
  27. defer m.Stop()
  28. assert.Equal(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(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{PubKey: pubKey}
  52. rpcClientMock := &mock.RpcClient{stubs}
  53. n = monitor.NewNodeWithEventMeterAndRpcClient("tcp://127.0.0.1:46657", emMock, rpcClientMock)
  54. return
  55. }