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