forked from go-zookeeper/zk
-
Notifications
You must be signed in to change notification settings - Fork 6
/
lock_test.go
125 lines (108 loc) · 2.88 KB
/
lock_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package zk
import (
"testing"
"time"
)
func TestLock(t *testing.T) {
WithTestCluster(t, 1, nil, logWriter{t: t, p: "[ZKERR] "}, func(t *testing.T, tc *TestCluster) {
WithConnectAll(t, tc, func(t *testing.T, c *Conn, ech <-chan Event) {
acls := WorldACL(PermAll)
l := NewLock(c, "/test", acls)
if err := l.Lock(); err != nil {
t.Fatal(err)
}
if err := l.Unlock(); err != nil {
t.Fatal(err)
}
val := make(chan int, 3)
if err := l.Lock(); err != nil {
t.Fatal(err)
}
l2 := NewLock(c, "/test", acls)
l2Errs := make(chan error, 2)
go func() {
defer close(l2Errs)
if err := l2.Lock(); err != nil {
l2Errs <- err
return
}
val <- 2
if err := l2.Unlock(); err != nil {
l2Errs <- err
return
}
val <- 3
}()
time.Sleep(time.Millisecond * 100)
val <- 1
if err := l.Unlock(); err != nil {
t.Fatal(err)
}
if x := <-val; x != 1 {
t.Fatalf("Expected 1 instead of %d", x)
}
if x := <-val; x != 2 {
t.Fatalf("Expected 2 instead of %d", x)
}
if x := <-val; x != 3 {
t.Fatalf("Expected 3 instead of %d", x)
}
if e, ok := <-l2Errs; ok {
t.Fatal(e)
}
})
})
}
// This tests creating a lock with a path that's more than 1 node deep (e.g. "/test-multi-level/lock"),
// when a part of that path already exists (i.e. "/test-multi-level" node already exists).
func TestMultiLevelLock(t *testing.T) {
WithTestCluster(t, 1, nil, logWriter{t: t, p: "[ZKERR] "}, func(t *testing.T, tc *TestCluster) {
WithConnectAll(t, tc, func(t *testing.T, c *Conn, ech <-chan Event) {
acls := WorldACL(PermAll)
path := "/test-multi-level"
if p, err := c.Create(path, []byte{1, 2, 3, 4}, 0, WorldACL(PermAll)); err != nil {
t.Fatalf("Create returned error: %+v", err)
} else if p != path {
t.Fatalf("Create returned different path '%s' != '%s'", p, path)
}
l := NewLock(c, "/test-multi-level/lock", acls)
// Clean up what we've created for this test
defer c.Delete("/test-multi-level", -1) // nolint: errcheck
defer c.Delete("/test-multi-level/lock", -1) // nolint: errcheck
if err := l.Lock(); err != nil {
t.Fatal(err)
}
if err := l.Unlock(); err != nil {
t.Fatal(err)
}
})
})
}
func TestParseSeq(t *testing.T) {
const (
goLock = "_c_38553bd6d1d57f710ae70ddcc3d24715-lock-0000000000"
negativeLock = "_c_38553bd6d1d57f710ae70ddcc3d24715-lock--2147483648"
pyLock = "da5719988c244fc793f49ec3aa29b566__lock__0000000003"
)
seq, err := parseSeq(goLock)
if err != nil {
t.Fatal(err)
}
if seq != 0 {
t.Fatalf("Expected 0 instead of %d", seq)
}
seq, err = parseSeq(negativeLock)
if err != nil {
t.Fatal(err)
}
if seq != -2147483648 {
t.Fatalf("Expected -2147483648 instead of %d", seq)
}
seq, err = parseSeq(pyLock)
if err != nil {
t.Fatal(err)
}
if seq != 3 {
t.Fatalf("Expected 3 instead of %d", seq)
}
}