multi_node_kill_all_and_recovery_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package test
  2. import (
  3. "os"
  4. "strconv"
  5. "testing"
  6. "time"
  7. "github.com/coreos/etcd/third_party/github.com/coreos/go-etcd/etcd"
  8. )
  9. // Create a five nodes
  10. // Kill all the nodes and restart
  11. func TestMultiNodeKillAllAndRecovery(t *testing.T) {
  12. procAttr := new(os.ProcAttr)
  13. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  14. stop := make(chan bool)
  15. leaderChan := make(chan string, 1)
  16. all := make(chan bool, 1)
  17. clusterSize := 5
  18. argGroup, etcds, err := CreateCluster(clusterSize, procAttr, false)
  19. defer DestroyCluster(etcds)
  20. if err != nil {
  21. t.Fatal("cannot create cluster")
  22. }
  23. c := etcd.NewClient(nil)
  24. go Monitor(clusterSize, clusterSize, leaderChan, all, stop)
  25. <-all
  26. <-leaderChan
  27. stop <- true
  28. c.SyncCluster()
  29. // send 10 commands
  30. for i := 0; i < 10; i++ {
  31. // Test Set
  32. _, err := c.Set("foo", "bar", 0)
  33. if err != nil {
  34. panic(err)
  35. }
  36. }
  37. time.Sleep(time.Second)
  38. // kill all
  39. DestroyCluster(etcds)
  40. time.Sleep(time.Second)
  41. stop = make(chan bool)
  42. leaderChan = make(chan string, 1)
  43. all = make(chan bool, 1)
  44. time.Sleep(time.Second)
  45. for i := 0; i < clusterSize; i++ {
  46. etcds[i], err = os.StartProcess(EtcdBinPath, argGroup[i], procAttr)
  47. }
  48. go Monitor(clusterSize, 1, leaderChan, all, stop)
  49. <-all
  50. <-leaderChan
  51. result, err := c.Set("foo", "bar", 0)
  52. if err != nil {
  53. t.Fatalf("Recovery error: %s", err)
  54. }
  55. if result.Node.ModifiedIndex != 16 {
  56. t.Fatalf("recovery failed! [%d/16]", result.Node.ModifiedIndex)
  57. }
  58. }
  59. // TestTLSMultiNodeKillAllAndRecovery create a five nodes
  60. // then kill all the nodes and restart
  61. func TestTLSMultiNodeKillAllAndRecovery(t *testing.T) {
  62. procAttr := new(os.ProcAttr)
  63. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  64. stop := make(chan bool)
  65. leaderChan := make(chan string, 1)
  66. all := make(chan bool, 1)
  67. clusterSize := 5
  68. argGroup, etcds, err := CreateCluster(clusterSize, procAttr, true)
  69. defer DestroyCluster(etcds)
  70. if err != nil {
  71. t.Fatal("cannot create cluster")
  72. }
  73. c := etcd.NewClient(nil)
  74. go Monitor(clusterSize, clusterSize, leaderChan, all, stop)
  75. <-all
  76. <-leaderChan
  77. stop <- true
  78. c.SyncCluster()
  79. // send 10 commands
  80. for i := 0; i < 10; i++ {
  81. // Test Set
  82. _, err := c.Set("foo", "bar", 0)
  83. if err != nil {
  84. panic(err)
  85. }
  86. }
  87. time.Sleep(time.Second)
  88. // kill all
  89. DestroyCluster(etcds)
  90. time.Sleep(time.Second)
  91. stop = make(chan bool)
  92. leaderChan = make(chan string, 1)
  93. all = make(chan bool, 1)
  94. time.Sleep(time.Second)
  95. for i := 0; i < clusterSize; i++ {
  96. etcds[i], err = os.StartProcess(EtcdBinPath, argGroup[i], procAttr)
  97. // See util.go for the reason to wait for server
  98. client := buildClient()
  99. err = WaitForServer("127.0.0.1:400"+strconv.Itoa(i+1), client, "http")
  100. if err != nil {
  101. t.Fatalf("node start error: %s", err)
  102. }
  103. }
  104. go Monitor(clusterSize, 1, leaderChan, all, stop)
  105. <-all
  106. <-leaderChan
  107. result, err := c.Set("foo", "bar", 0)
  108. if err != nil {
  109. t.Fatalf("Recovery error: %s", err)
  110. }
  111. if result.Node.ModifiedIndex != 16 {
  112. t.Fatalf("recovery failed! [%d/16]", result.Node.ModifiedIndex)
  113. }
  114. }