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.

93 lines
2.1 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/libs/log"
  11. cfg "github.com/tendermint/tendermint/config"
  12. "github.com/tendermint/tendermint/types"
  13. tmtime "github.com/tendermint/tendermint/types/time"
  14. )
  15. func TestNodeStartStop(t *testing.T) {
  16. config := cfg.ResetTestRoot("node_node_test")
  17. // create & start node
  18. n, err := DefaultNewNode(config, log.TestingLogger())
  19. assert.NoError(t, err, "expected no err on DefaultNewNode")
  20. err1 := n.Start()
  21. if err1 != nil {
  22. t.Error(err1)
  23. }
  24. t.Logf("Started node %v", n.sw.NodeInfo())
  25. // wait for the node to produce a block
  26. blockCh := make(chan interface{})
  27. err = n.EventBus().Subscribe(context.Background(), "node_test", types.EventQueryNewBlock, blockCh)
  28. assert.NoError(t, err)
  29. select {
  30. case <-blockCh:
  31. case <-time.After(10 * time.Second):
  32. t.Fatal("timed out waiting for the node to produce a block")
  33. }
  34. // stop the node
  35. go func() {
  36. n.Stop()
  37. }()
  38. select {
  39. case <-n.Quit():
  40. case <-time.After(5 * time.Second):
  41. pid := os.Getpid()
  42. p, err := os.FindProcess(pid)
  43. if err != nil {
  44. panic(err)
  45. }
  46. err = p.Signal(syscall.SIGABRT)
  47. fmt.Println(err)
  48. t.Fatal("timed out waiting for shutdown")
  49. }
  50. }
  51. func TestSplitAndTrimEmpty(t *testing.T) {
  52. testCases := []struct {
  53. s string
  54. sep string
  55. cutset string
  56. expected []string
  57. }{
  58. {"a,b,c", ",", " ", []string{"a", "b", "c"}},
  59. {" a , b , c ", ",", " ", []string{"a", "b", "c"}},
  60. {" a, b, c ", ",", " ", []string{"a", "b", "c"}},
  61. {" a, ", ",", " ", []string{"a"}},
  62. {" ", ",", " ", []string{}},
  63. }
  64. for _, tc := range testCases {
  65. assert.Equal(t, tc.expected, splitAndTrimEmpty(tc.s, tc.sep, tc.cutset), "%s", tc.s)
  66. }
  67. }
  68. func TestNodeDelayedStop(t *testing.T) {
  69. config := cfg.ResetTestRoot("node_delayed_node_test")
  70. now := tmtime.Now()
  71. // create & start node
  72. n, err := DefaultNewNode(config, log.TestingLogger())
  73. n.GenesisDoc().GenesisTime = now.Add(5 * time.Second)
  74. assert.NoError(t, err)
  75. n.Start()
  76. startTime := tmtime.Now()
  77. assert.Equal(t, true, startTime.After(n.GenesisDoc().GenesisTime))
  78. }