forked from inf-rno/zk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_test.go
82 lines (69 loc) · 1.7 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
package zk
import (
"context"
"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!")
}
}