forked from zssky/zk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_test.go
168 lines (149 loc) · 3.48 KB
/
conn_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package zk
import (
"context"
"fmt"
"io/ioutil"
"sync"
"testing"
"time"
)
func TestRecurringReAuthHang(t *testing.T) {
zkC, err := StartTestCluster(t, 3, ioutil.Discard, ioutil.Discard)
if err != nil {
panic(err)
}
defer zkC.Stop()
conn, evtC, err := zkC.ConnectAll()
if err != nil {
panic(err)
}
defer conn.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
waitForSession(ctx, evtC)
// Add auth.
conn.AddAuth("digest", []byte("test:test"))
var reauthCloseOnce sync.Once
reauthSig := make(chan struct{}, 1)
conn.resendZkAuthFn = func(ctx context.Context, c *Conn) error {
// in current implimentation the reauth might be called more than once based on various conditions
reauthCloseOnce.Do(func() { close(reauthSig) })
return resendZkAuth(ctx, c)
}
conn.debugCloseRecvLoop = true
currentServer := conn.Server()
zkC.StopServer(currentServer)
// wait connect to new zookeeper.
ctx, cancel = context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
waitForSession(ctx, evtC)
select {
case _, ok := <-reauthSig:
if !ok {
return // we closed the channel as expected
}
t.Fatal("reauth testing channel should have been closed")
case <-ctx.Done():
t.Fatal(ctx.Err())
}
}
func TestDeadlockInClose(t *testing.T) {
c := &Conn{
shouldQuit: make(chan struct{}),
connectTimeout: 1 * time.Second,
sendChan: make(chan *request, sendChanSize),
logger: DefaultLogger,
}
for i := 0; i < sendChanSize; i++ {
c.sendChan <- &request{}
}
okChan := make(chan struct{})
go func() {
c.Close()
close(okChan)
}()
select {
case <-okChan:
case <-time.After(3 * time.Second):
t.Fatal("apparent deadlock!")
}
}
func TestNotifyWatches(t *testing.T) {
cases := []struct {
eType EventType
path string
watches map[watchPathType]bool
}{
{
EventNodeCreated, "/",
map[watchPathType]bool{
{"/", watchTypeExist}: true,
{"/", watchTypeChild}: false,
{"/", watchTypeData}: false,
},
},
{
EventNodeCreated, "/a",
map[watchPathType]bool{
{"/b", watchTypeExist}: false,
},
},
{
EventNodeDataChanged, "/",
map[watchPathType]bool{
{"/", watchTypeExist}: true,
{"/", watchTypeData}: true,
{"/", watchTypeChild}: false,
},
},
{
EventNodeChildrenChanged, "/",
map[watchPathType]bool{
{"/", watchTypeExist}: false,
{"/", watchTypeData}: false,
{"/", watchTypeChild}: true,
},
},
{
EventNodeDeleted, "/",
map[watchPathType]bool{
{"/", watchTypeExist}: true,
{"/", watchTypeData}: true,
{"/", watchTypeChild}: true,
},
},
}
conn := &Conn{watchers: make(map[watchPathType][]chan Event)}
for idx, c := range cases {
t.Run(fmt.Sprintf("#%d %s", idx, c.eType), func(t *testing.T) {
c := c
notifications := make([]struct {
path string
notify bool
ch <-chan Event
}, len(c.watches))
var idx int
for wpt, expectEvent := range c.watches {
ch := conn.addWatcher(wpt.path, wpt.wType)
notifications[idx].path = wpt.path
notifications[idx].notify = expectEvent
notifications[idx].ch = ch
idx++
}
ev := Event{Type: c.eType, Path: c.path}
conn.notifyWatches(ev)
for _, res := range notifications {
select {
case e := <-res.ch:
if !res.notify || e.Path != res.path {
t.Fatal("unexpeted notification received")
}
default:
if res.notify {
t.Fatal("expected notification not received")
}
}
}
})
}
}