github.com/jpetazzo/etcd@v0.2.1-0.20140113055439-97f1363afac5/tests/functional/multi_node_kill_all_and_recovery_test.go (about)

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