forked from ngaut/go-zookeeper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcluster_test.go
94 lines (86 loc) · 1.85 KB
/
cluster_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package zk
import (
"testing"
"time"
)
func TestBasicCluster(t *testing.T) {
ts, err := StartTestCluster(3)
if err != nil {
t.Fatal(err)
}
defer ts.Stop()
zk1, err := ts.Connect(0)
if err != nil {
t.Fatalf("Connect returned error: %+v", err)
}
defer zk1.Close()
zk2, err := ts.Connect(1)
if err != nil {
t.Fatalf("Connect returned error: %+v", err)
}
defer zk2.Close()
if _, err := zk1.Create("/gozk-test", []byte("foo-cluster"), 0, WorldACL(PermAll)); err != nil {
t.Fatalf("Create failed on node 1: %+v", err)
}
if by, _, err := zk2.Get("/gozk-test"); err != nil {
t.Fatalf("Get failed on node 2: %+v", err)
} else if string(by) != "foo-cluster" {
t.Fatal("Wrong data for node 2")
}
}
func TestClientClusterFailover(t *testing.T) {
ts, err := StartTestCluster(3)
if err != nil {
t.Fatal(err)
}
defer ts.Stop()
zk, err := ts.ConnectAll()
if err != nil {
t.Fatalf("Connect returned error: %+v", err)
}
defer zk.Close()
if _, err := zk.Create("/gozk-test", []byte("foo-cluster"), 0, WorldACL(PermAll)); err != nil {
t.Fatalf("Create failed on node 1: %+v", err)
}
ts.Servers[0].Srv.Stop()
if by, _, err := zk.Get("/gozk-test"); err != nil {
t.Fatalf("Get failed on node 2: %+v", err)
} else if string(by) != "foo-cluster" {
t.Fatal("Wrong data for node 2")
}
}
func TestWaitForClose(t *testing.T) {
ts, err := StartTestCluster(1)
if err != nil {
t.Fatal(err)
}
defer ts.Stop()
zk, err := ts.Connect(0)
if err != nil {
t.Fatalf("Connect returned error: %+v", err)
}
timeout := time.After(30*time.Second)
CONNECTED:
for{
select{
case ev := <-zk.eventChan :
if ev.State == StateConnected {
break CONNECTED;
}
case <-timeout:
zk.Close()
t.Fatal("Timeout")
}
}
zk.Close()
for{
select{
case _,ok := <-zk.eventChan :
if !ok {
return
}
case <-timeout:
t.Fatal("Timeout")
}
}
}