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.

194 lines
4.9 KiB

8 years ago
8 years ago
7 years ago
  1. package node
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "os"
  7. "syscall"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. "github.com/tendermint/tendermint/abci/example/kvstore"
  13. cfg "github.com/tendermint/tendermint/config"
  14. "github.com/tendermint/tendermint/crypto/ed25519"
  15. cmn "github.com/tendermint/tendermint/libs/common"
  16. "github.com/tendermint/tendermint/libs/log"
  17. "github.com/tendermint/tendermint/p2p"
  18. "github.com/tendermint/tendermint/privval"
  19. sm "github.com/tendermint/tendermint/state"
  20. "github.com/tendermint/tendermint/types"
  21. tmtime "github.com/tendermint/tendermint/types/time"
  22. "github.com/tendermint/tendermint/version"
  23. )
  24. func TestNodeStartStop(t *testing.T) {
  25. config := cfg.ResetTestRoot("node_node_test")
  26. // create & start node
  27. n, err := DefaultNewNode(config, log.TestingLogger())
  28. require.NoError(t, err)
  29. err = n.Start()
  30. require.NoError(t, err)
  31. t.Logf("Started node %v", n.sw.NodeInfo())
  32. // wait for the node to produce a block
  33. blockCh := make(chan interface{})
  34. err = n.EventBus().Subscribe(context.Background(), "node_test", types.EventQueryNewBlock, blockCh)
  35. require.NoError(t, err)
  36. select {
  37. case <-blockCh:
  38. case <-time.After(10 * time.Second):
  39. t.Fatal("timed out waiting for the node to produce a block")
  40. }
  41. // stop the node
  42. go func() {
  43. n.Stop()
  44. }()
  45. select {
  46. case <-n.Quit():
  47. case <-time.After(5 * time.Second):
  48. pid := os.Getpid()
  49. p, err := os.FindProcess(pid)
  50. if err != nil {
  51. panic(err)
  52. }
  53. err = p.Signal(syscall.SIGABRT)
  54. fmt.Println(err)
  55. t.Fatal("timed out waiting for shutdown")
  56. }
  57. }
  58. func TestSplitAndTrimEmpty(t *testing.T) {
  59. testCases := []struct {
  60. s string
  61. sep string
  62. cutset string
  63. expected []string
  64. }{
  65. {"a,b,c", ",", " ", []string{"a", "b", "c"}},
  66. {" a , b , c ", ",", " ", []string{"a", "b", "c"}},
  67. {" a, b, c ", ",", " ", []string{"a", "b", "c"}},
  68. {" a, ", ",", " ", []string{"a"}},
  69. {" ", ",", " ", []string{}},
  70. }
  71. for _, tc := range testCases {
  72. assert.Equal(t, tc.expected, splitAndTrimEmpty(tc.s, tc.sep, tc.cutset), "%s", tc.s)
  73. }
  74. }
  75. func TestNodeDelayedStop(t *testing.T) {
  76. config := cfg.ResetTestRoot("node_delayed_node_test")
  77. now := tmtime.Now()
  78. // create & start node
  79. n, err := DefaultNewNode(config, log.TestingLogger())
  80. n.GenesisDoc().GenesisTime = now.Add(5 * time.Second)
  81. require.NoError(t, err)
  82. n.Start()
  83. startTime := tmtime.Now()
  84. assert.Equal(t, true, startTime.After(n.GenesisDoc().GenesisTime))
  85. }
  86. func TestNodeSetAppVersion(t *testing.T) {
  87. config := cfg.ResetTestRoot("node_app_version_test")
  88. // create & start node
  89. n, err := DefaultNewNode(config, log.TestingLogger())
  90. require.NoError(t, err)
  91. // default config uses the kvstore app
  92. var appVersion version.Protocol = kvstore.ProtocolVersion
  93. // check version is set in state
  94. state := sm.LoadState(n.stateDB)
  95. assert.Equal(t, state.Version.Consensus.App, appVersion)
  96. // check version is set in node info
  97. assert.Equal(t, n.nodeInfo.(p2p.DefaultNodeInfo).ProtocolVersion.App, appVersion)
  98. }
  99. func TestNodeSetPrivValTCP(t *testing.T) {
  100. addr := "tcp://" + testFreeAddr(t)
  101. config := cfg.ResetTestRoot("node_priv_val_tcp_test")
  102. config.BaseConfig.PrivValidatorListenAddr = addr
  103. rs := privval.NewRemoteSigner(
  104. log.TestingLogger(),
  105. config.ChainID(),
  106. addr,
  107. types.NewMockPV(),
  108. ed25519.GenPrivKey(),
  109. )
  110. privval.RemoteSignerConnDeadline(5 * time.Millisecond)(rs)
  111. go func() {
  112. err := rs.Start()
  113. if err != nil {
  114. panic(err)
  115. }
  116. }()
  117. defer rs.Stop()
  118. n, err := DefaultNewNode(config, log.TestingLogger())
  119. require.NoError(t, err)
  120. assert.IsType(t, &privval.TCPVal{}, n.PrivValidator())
  121. }
  122. // address without a protocol must result in error
  123. func TestPrivValidatorListenAddrNoProtocol(t *testing.T) {
  124. addrNoPrefix := testFreeAddr(t)
  125. config := cfg.ResetTestRoot("node_priv_val_tcp_test")
  126. config.BaseConfig.PrivValidatorListenAddr = addrNoPrefix
  127. _, err := DefaultNewNode(config, log.TestingLogger())
  128. assert.Error(t, err)
  129. }
  130. func TestNodeSetPrivValIPC(t *testing.T) {
  131. tmpfile := "/tmp/kms." + cmn.RandStr(6) + ".sock"
  132. defer os.Remove(tmpfile) // clean up
  133. config := cfg.ResetTestRoot("node_priv_val_tcp_test")
  134. config.BaseConfig.PrivValidatorListenAddr = "unix://" + tmpfile
  135. rs := privval.NewIPCRemoteSigner(
  136. log.TestingLogger(),
  137. config.ChainID(),
  138. tmpfile,
  139. types.NewMockPV(),
  140. )
  141. privval.IPCRemoteSignerConnDeadline(3 * time.Second)(rs)
  142. done := make(chan struct{})
  143. go func() {
  144. defer close(done)
  145. n, err := DefaultNewNode(config, log.TestingLogger())
  146. require.NoError(t, err)
  147. assert.IsType(t, &privval.IPCVal{}, n.PrivValidator())
  148. }()
  149. err := rs.Start()
  150. require.NoError(t, err)
  151. defer rs.Stop()
  152. <-done
  153. }
  154. // testFreeAddr claims a free port so we don't block on listener being ready.
  155. func testFreeAddr(t *testing.T) string {
  156. ln, err := net.Listen("tcp", "127.0.0.1:0")
  157. require.NoError(t, err)
  158. defer ln.Close()
  159. return fmt.Sprintf("127.0.0.1:%d", ln.Addr().(*net.TCPAddr).Port)
  160. }