github.com/sym3tri/etcd@v0.2.1-0.20140422215517-a563d82f95d6/tests/functional/multi_node_kill_all_and_recovery_test.go (about)

     1  package test
     2  
     3  import (
     4  	"os"
     5  	"strconv"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/coreos/etcd/third_party/github.com/coreos/go-etcd/etcd"
    10  )
    11  
    12  // Create a five nodes
    13  // Kill all the nodes and restart
    14  func TestMultiNodeKillAllAndRecovery(t *testing.T) {
    15  	procAttr := new(os.ProcAttr)
    16  	procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
    17  
    18  	stop := make(chan bool)
    19  	leaderChan := make(chan string, 1)
    20  	all := make(chan bool, 1)
    21  
    22  	clusterSize := 5
    23  	argGroup, etcds, err := CreateCluster(clusterSize, procAttr, false)
    24  	defer DestroyCluster(etcds)
    25  
    26  	if err != nil {
    27  		t.Fatal("cannot create cluster")
    28  	}
    29  
    30  	c := etcd.NewClient(nil)
    31  
    32  	go Monitor(clusterSize, clusterSize, leaderChan, all, stop)
    33  	<-all
    34  	<-leaderChan
    35  	stop <- true
    36  
    37  	c.SyncCluster()
    38  
    39  	// send 10 commands
    40  	for i := 0; i < 10; i++ {
    41  		// Test Set
    42  		_, err := c.Set("foo", "bar", 0)
    43  		if err != nil {
    44  			panic(err)
    45  		}
    46  	}
    47  
    48  	time.Sleep(time.Second)
    49  
    50  	// kill all
    51  	DestroyCluster(etcds)
    52  
    53  	time.Sleep(time.Second)
    54  
    55  	stop = make(chan bool)
    56  	leaderChan = make(chan string, 1)
    57  	all = make(chan bool, 1)
    58  
    59  	time.Sleep(time.Second)
    60  
    61  	for i := 0; i < clusterSize; i++ {
    62  		etcds[i], err = os.StartProcess(EtcdBinPath, argGroup[i], procAttr)
    63  	}
    64  
    65  	go Monitor(clusterSize, 1, leaderChan, all, stop)
    66  
    67  	<-all
    68  	<-leaderChan
    69  
    70  	result, err := c.Set("foo", "bar", 0)
    71  
    72  	if err != nil {
    73  		t.Fatalf("Recovery error: %s", err)
    74  	}
    75  
    76  	if result.Node.ModifiedIndex != 16 {
    77  		t.Fatalf("recovery failed! [%d/16]", result.Node.ModifiedIndex)
    78  	}
    79  }
    80  
    81  // TestTLSMultiNodeKillAllAndRecovery create a five nodes
    82  // then kill all the nodes and restart
    83  func TestTLSMultiNodeKillAllAndRecovery(t *testing.T) {
    84  	procAttr := new(os.ProcAttr)
    85  	procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
    86  
    87  	stop := make(chan bool)
    88  	leaderChan := make(chan string, 1)
    89  	all := make(chan bool, 1)
    90  
    91  	clusterSize := 5
    92  	argGroup, etcds, err := CreateCluster(clusterSize, procAttr, true)
    93  	defer DestroyCluster(etcds)
    94  
    95  	if err != nil {
    96  		t.Fatal("cannot create cluster")
    97  	}
    98  
    99  	c := etcd.NewClient(nil)
   100  
   101  	go Monitor(clusterSize, clusterSize, leaderChan, all, stop)
   102  	<-all
   103  	<-leaderChan
   104  	stop <- true
   105  
   106  	c.SyncCluster()
   107  
   108  	// send 10 commands
   109  	for i := 0; i < 10; i++ {
   110  		// Test Set
   111  		_, err := c.Set("foo", "bar", 0)
   112  		if err != nil {
   113  			panic(err)
   114  		}
   115  	}
   116  
   117  	time.Sleep(time.Second)
   118  
   119  	// kill all
   120  	DestroyCluster(etcds)
   121  
   122  	time.Sleep(time.Second)
   123  
   124  	stop = make(chan bool)
   125  	leaderChan = make(chan string, 1)
   126  	all = make(chan bool, 1)
   127  
   128  	time.Sleep(time.Second)
   129  
   130  	for i := 0; i < clusterSize; i++ {
   131  		etcds[i], err = os.StartProcess(EtcdBinPath, argGroup[i], procAttr)
   132  		// See util.go for the reason to wait for server
   133  		client := buildClient()
   134  		err = WaitForServer("127.0.0.1:400"+strconv.Itoa(i+1), client, "http")
   135  		if err != nil {
   136  			t.Fatalf("node start error: %s", err)
   137  		}
   138  	}
   139  
   140  	go Monitor(clusterSize, 1, leaderChan, all, stop)
   141  
   142  	<-all
   143  	<-leaderChan
   144  
   145  	result, err := c.Set("foo", "bar", 0)
   146  
   147  	if err != nil {
   148  		t.Fatalf("Recovery error: %s", err)
   149  	}
   150  
   151  	if result.Node.ModifiedIndex != 16 {
   152  		t.Fatalf("recovery failed! [%d/16]", result.Node.ModifiedIndex)
   153  	}
   154  }