multi_node_kill_all_and_recovery_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. }