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.

115 lines
2.9 KiB

8 years ago
8 years ago
7 years ago
  1. package node
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "syscall"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/tendermint/tendermint/abci/example/kvstore"
  11. "github.com/tendermint/tendermint/libs/log"
  12. "github.com/tendermint/tendermint/p2p"
  13. sm "github.com/tendermint/tendermint/state"
  14. "github.com/tendermint/tendermint/version"
  15. cfg "github.com/tendermint/tendermint/config"
  16. "github.com/tendermint/tendermint/types"
  17. tmtime "github.com/tendermint/tendermint/types/time"
  18. )
  19. func TestNodeStartStop(t *testing.T) {
  20. config := cfg.ResetTestRoot("node_node_test")
  21. // create & start node
  22. n, err := DefaultNewNode(config, log.TestingLogger())
  23. assert.NoError(t, err, "expected no err on DefaultNewNode")
  24. err1 := n.Start()
  25. if err1 != nil {
  26. t.Error(err1)
  27. }
  28. t.Logf("Started node %v", n.sw.NodeInfo())
  29. // wait for the node to produce a block
  30. blockCh := make(chan interface{})
  31. err = n.EventBus().Subscribe(context.Background(), "node_test", types.EventQueryNewBlock, blockCh)
  32. assert.NoError(t, err)
  33. select {
  34. case <-blockCh:
  35. case <-time.After(10 * time.Second):
  36. t.Fatal("timed out waiting for the node to produce a block")
  37. }
  38. // stop the node
  39. go func() {
  40. n.Stop()
  41. }()
  42. select {
  43. case <-n.Quit():
  44. case <-time.After(5 * time.Second):
  45. pid := os.Getpid()
  46. p, err := os.FindProcess(pid)
  47. if err != nil {
  48. panic(err)
  49. }
  50. err = p.Signal(syscall.SIGABRT)
  51. fmt.Println(err)
  52. t.Fatal("timed out waiting for shutdown")
  53. }
  54. }
  55. func TestSplitAndTrimEmpty(t *testing.T) {
  56. testCases := []struct {
  57. s string
  58. sep string
  59. cutset string
  60. expected []string
  61. }{
  62. {"a,b,c", ",", " ", []string{"a", "b", "c"}},
  63. {" a , b , c ", ",", " ", []string{"a", "b", "c"}},
  64. {" a, b, c ", ",", " ", []string{"a", "b", "c"}},
  65. {" a, ", ",", " ", []string{"a"}},
  66. {" ", ",", " ", []string{}},
  67. }
  68. for _, tc := range testCases {
  69. assert.Equal(t, tc.expected, splitAndTrimEmpty(tc.s, tc.sep, tc.cutset), "%s", tc.s)
  70. }
  71. }
  72. func TestNodeDelayedStop(t *testing.T) {
  73. config := cfg.ResetTestRoot("node_delayed_node_test")
  74. now := tmtime.Now()
  75. // create & start node
  76. n, err := DefaultNewNode(config, log.TestingLogger())
  77. n.GenesisDoc().GenesisTime = now.Add(5 * time.Second)
  78. assert.NoError(t, err)
  79. n.Start()
  80. startTime := tmtime.Now()
  81. assert.Equal(t, true, startTime.After(n.GenesisDoc().GenesisTime))
  82. }
  83. func TestNodeSetAppVersion(t *testing.T) {
  84. config := cfg.ResetTestRoot("node_app_version_test")
  85. // create & start node
  86. n, err := DefaultNewNode(config, log.TestingLogger())
  87. assert.NoError(t, err, "expected no err on DefaultNewNode")
  88. // default config uses the kvstore app
  89. var appVersion version.Protocol = kvstore.ProtocolVersion
  90. // check version is set in state
  91. state := sm.LoadState(n.stateDB)
  92. assert.Equal(t, state.Version.Consensus.App, appVersion)
  93. // check version is set in node info
  94. assert.Equal(t, n.nodeInfo.(p2p.DefaultNodeInfo).ProtocolVersion.App, appVersion)
  95. }