-
Notifications
You must be signed in to change notification settings - Fork 18
/
ha_conn_factory_test.go
100 lines (91 loc) · 2.79 KB
/
ha_conn_factory_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
package pool
import (
"errors"
"testing"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("client pool", func() {})
func TestClientPool(t *testing.T) {
It("eject/rejoin host", func() {
cfg := &HAConfig{
Master: "addr:0",
Slaves: []string{"addr:0:100", "addr:0:200", "addr:0:300"},
AutoEjectHost: true,
ServerRetryTimeout: 100 * time.Millisecond,
ServerFailureLimit: 3,
}
_ = cfg.init()
p := newClientPool(cfg)
Expect(p.alives).To(Equal(p.slaves))
p.slaves[0].failureCount = p.serverFailureLimit
time.Sleep(120 * time.Millisecond)
// the slave 0 should be evicted
Expect(p.alives).To(Equal(p.slaves[1:]))
time.Sleep(120 * time.Millisecond)
// the slave 0 should be rejoined
Expect(p.alives[0].failureCount).To(Equal(p.serverFailureLimit - 1))
Expect(p.alives).To(Equal(p.slaves))
})
It("not eject/rejoin host", func() {
cfg := &HAConfig{
Master: "addr:0",
Slaves: []string{"addr:0:100", "addr:0:200", "addr:0:300"},
AutoEjectHost: false,
ServerRetryTimeout: 100 * time.Millisecond,
ServerFailureLimit: 3,
}
_ = cfg.init()
p := newClientPool(cfg)
Expect(p.alives).To(Equal(p.slaves))
p.slaves[0].failureCount = p.serverFailureLimit
time.Sleep(120 * time.Millisecond)
Expect(p.alives).To(Equal(p.slaves))
time.Sleep(120 * time.Millisecond)
Expect(p.alives[0].failureCount).To(Equal(p.serverFailureLimit))
Expect(p.alives).To(Equal(p.slaves))
})
It("get conn", func() {
cfg := &HAConfig{
Master: "addr:0",
Slaves: []string{"addr:0:100", "addr:0:200", "addr:0:300"},
AutoEjectHost: true,
ServerRetryTimeout: 100 * time.Millisecond,
ServerFailureLimit: 3,
PollType: PollByWeight,
}
_ = cfg.init()
p := newClientPool(cfg)
for i := range p.slaves {
p.slaves[i].failureCount = p.serverFailureLimit
}
time.Sleep(120 * time.Millisecond)
_, err := p.getConn()
Expect(err).To(Equal(errors.New("no alive slaves")))
for i := range p.slaves {
p.slaves[i].failureCount = p.serverFailureLimit - 1
}
_, err = p.getConn()
Expect(err).NotTo(Equal(HaveOccurred()))
})
It("min server num", func() {
cfg := &HAConfig{
Master: "addr:0",
Slaves: []string{"addr:0:100", "addr:0:200", "addr:0:300"},
AutoEjectHost: true,
ServerRetryTimeout: 100 * time.Millisecond,
ServerFailureLimit: 3,
PollType: PollByWeight,
MinServerNum: 2,
}
_ = cfg.init()
p := newClientPool(cfg)
time.Sleep(120 * time.Millisecond)
Expect(p.alives).To(Equal(p.slaves))
p.slaves[0].failureCount = p.serverFailureLimit
p.slaves[1].failureCount = p.serverFailureLimit
time.Sleep(120 * time.Millisecond)
Expect(len(p.alives)).To(Equal(p.minServerNum))
})
}