kill_random_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package test
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "os"
  6. "testing"
  7. "time"
  8. )
  9. // TestKillRandom kills random peers in the cluster and
  10. // restart them after all other peers agree on the same leader
  11. func TestKillRandom(t *testing.T) {
  12. procAttr := new(os.ProcAttr)
  13. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  14. clusterSize := 9
  15. argGroup, etcds, err := CreateCluster(clusterSize, procAttr, false)
  16. if err != nil {
  17. t.Fatal("cannot create cluster")
  18. }
  19. defer DestroyCluster(etcds)
  20. stop := make(chan bool)
  21. leaderChan := make(chan string, 1)
  22. all := make(chan bool, 1)
  23. time.Sleep(3 * time.Second)
  24. go Monitor(clusterSize, 4, leaderChan, all, stop)
  25. toKill := make(map[int]bool)
  26. for i := 0; i < 20; i++ {
  27. fmt.Printf("TestKillRandom Round[%d/20]\n", i)
  28. j := 0
  29. for {
  30. r := rand.Int31n(9)
  31. if _, ok := toKill[int(r)]; !ok {
  32. j++
  33. toKill[int(r)] = true
  34. }
  35. if j > 3 {
  36. break
  37. }
  38. }
  39. for num, _ := range toKill {
  40. err := etcds[num].Kill()
  41. if err != nil {
  42. panic(err)
  43. }
  44. etcds[num].Wait()
  45. }
  46. time.Sleep(1 * time.Second)
  47. <-leaderChan
  48. for num, _ := range toKill {
  49. etcds[num], err = os.StartProcess(EtcdBinPath, argGroup[num], procAttr)
  50. }
  51. toKill = make(map[int]bool)
  52. <-all
  53. }
  54. stop <- true
  55. }