func main() { good := 0 bad := 0 // open https // etcd.SetScheme(etcd.HTTPS) // add client cert // success, err :=etcd.SetCertAndKey("key/me_cert.pem", "key/me_key.pem") // if !success { // fmt.Println(err) // } etcd.SyncCluster() c := make(chan bool, 10) // set up a lock etcd.Set("lock", "unlock", 0) for i := 0; i < 10; i++ { go t(i, c) } for i := 0; i < 10; i++ { if <-c { good++ } else { bad++ } } fmt.Println("good: ", good, "bad: ", bad) }
// Sending set commands func set(t *testing.T, stop chan bool) { stopSet := false i := 0 for { key := fmt.Sprintf("%s_%v", "foo", i) result, err := etcd.Set(key, "bar", 0) if err != nil || result.Key != "/"+key || result.Value != "bar" { if err != nil { t.Fatal(err) } t.Fatalf("Set failed with %s %s %v", result.Key, result.Value) } select { case <-stop: stopSet = true default: } if stopSet { break } i++ } stop <- true }
func t(num int, c chan bool) { for i := 0; i < 10000; i++ { str := fmt.Sprintf("foo_%d", num*i) etcd.Set(str, "10", 0) } c <- true }
func unlock() { for { _, err := etcd.Set("lock", "unlock", 0) if err == nil { return } fmt.Println(err) } }
// Create a three nodes and try to set value func TestSimpleMultiNode(t *testing.T) { procAttr := new(os.ProcAttr) procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr} clusterSize := 3 _, etcds, err := createCluster(clusterSize, procAttr) if err != nil { t.Fatal("cannot create cluster") } defer destroyCluster(etcds) time.Sleep(time.Second) etcd.SyncCluster() // Test Set result, err := etcd.Set("foo", "bar", 100) if err != nil || result.Key != "/foo" || result.Value != "bar" || result.TTL != 99 { if err != nil { t.Fatal(err) } t.Fatalf("Set 1 failed with %s %s %v", result.Key, result.Value, result.TTL) } time.Sleep(time.Second) result, err = etcd.Set("foo", "bar", 100) if err != nil || result.Key != "/foo" || result.Value != "bar" || result.PrevValue != "bar" || result.TTL != 99 { if err != nil { t.Fatal(err) } t.Fatalf("Set 2 failed with %s %s %v", result.Key, result.Value, result.TTL) } }
func TestEtcdMasterOther(t *testing.T) { path := "foo" etcd := tools.NewFakeEtcdClient(t) etcd.Set(path, "baz", 0) master := NewEtcdMasterElector(etcd) w := master.Elect(path, "bar") result := <-w.ResultChan() if result.Type != watch.Modified || result.Object.(Master) != "baz" { t.Errorf("unexpected event: %#v", result) } w.Stop() }
// This test creates a single node and then set a value to it. // Then this test kills the node and restart it and tries to get the value again. func TestSingleNodeRecovery(t *testing.T) { procAttr := new(os.ProcAttr) procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr} args := []string{"etcd", "-d=/tmp/node1"} process, err := os.StartProcess("etcd", append(args, "-i"), procAttr) if err != nil { t.Fatal("start process failed:" + err.Error()) return } time.Sleep(time.Second) etcd.SyncCluster() // Test Set result, err := etcd.Set("foo", "bar", 100) if err != nil || result.Key != "/foo" || result.Value != "bar" || result.TTL != 99 { if err != nil { t.Fatal(err) } t.Fatalf("Set 1 failed with %s %s %v", result.Key, result.Value, result.TTL) } time.Sleep(time.Second) process.Kill() process, err = os.StartProcess("etcd", args, procAttr) defer process.Kill() if err != nil { t.Fatal("start process failed:" + err.Error()) return } time.Sleep(time.Second) results, err := etcd.Get("foo") if err != nil { t.Fatal("get fail: " + err.Error()) return } result = results[0] if err != nil || result.Key != "/foo" || result.Value != "bar" || result.TTL > 99 { if err != nil { t.Fatal(err) } t.Fatalf("Recovery Get failed with %s %s %v", result.Key, result.Value, result.TTL) } }
// Sending set commands func set(stop chan bool) { stopSet := false i := 0 for { key := fmt.Sprintf("%s_%v", "foo", i) result, err := etcd.Set(key, "bar", 0) if err != nil || result.Key != "/"+key || result.Value != "bar" { select { case <-stop: stopSet = true default: fmt.Println("Set failed!") return } } select { case <-stop: stopSet = true default: } if stopSet { break } i++ } fmt.Println("set stop") stop <- true }