multi_node_kill_all_and_recovery_test.go 2.8 KB

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