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.

74 lines
1.7 KiB

  1. package main_test
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. em "github.com/tendermint/go-event-meter"
  7. monitor "github.com/tendermint/netmon/tm-monitor"
  8. mock "github.com/tendermint/netmon/tm-monitor/mock"
  9. tmtypes "github.com/tendermint/tendermint/types"
  10. )
  11. func TestNodeStartStop(t *testing.T) {
  12. assert := assert.New(t)
  13. n, _ := setupNode(t)
  14. assert.Equal(true, n.Online)
  15. n.Stop()
  16. }
  17. func TestNodeNewBlockReceived(t *testing.T) {
  18. assert := assert.New(t)
  19. blockCh := make(chan tmtypes.Header, 100)
  20. n, emMock := setupNode(t)
  21. n.SendBlocksTo(blockCh)
  22. blockHeader := &tmtypes.Header{Height: 5}
  23. emMock.Call("eventCallback", &em.EventMetric{}, tmtypes.EventDataNewBlockHeader{blockHeader})
  24. assert.Equal(uint64(5), n.Height)
  25. assert.Equal(*blockHeader, <-blockCh)
  26. }
  27. func TestNodeNewBlockLatencyReceived(t *testing.T) {
  28. assert := assert.New(t)
  29. blockLatencyCh := make(chan float64, 100)
  30. n, emMock := setupNode(t)
  31. n.SendBlockLatenciesTo(blockLatencyCh)
  32. emMock.Call("latencyCallback", 1000000.0)
  33. assert.Equal(1.0, n.BlockLatency)
  34. assert.Equal(1000000.0, <-blockLatencyCh)
  35. }
  36. func TestNodeConnectionLost(t *testing.T) {
  37. assert := assert.New(t)
  38. disconnectCh := make(chan bool, 100)
  39. n, emMock := setupNode(t)
  40. n.NotifyAboutDisconnects(disconnectCh)
  41. emMock.Call("disconnectCallback")
  42. assert.Equal(true, <-disconnectCh)
  43. assert.Equal(false, <-disconnectCh)
  44. // we're back in a race
  45. assert.Equal(true, n.Online)
  46. }
  47. func setupNode(t *testing.T) (n *monitor.Node, emMock *mock.EventMeter) {
  48. emMock = &mock.EventMeter{}
  49. n = monitor.NewNodeWithEventMeter("tcp://127.0.0.1:46657", emMock)
  50. err := n.Start()
  51. require.Nil(t, err)
  52. return
  53. }