forked from bsm/redis-balancer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
balancer_test.go
123 lines (103 loc) · 4.41 KB
/
balancer_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
package balancer
import (
"math/rand"
"testing"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Balancer", func() {
var subject *Balancer
It("should initialize with defaults", func() {
subject = New([]*Options{}, ModeFirstUp)
defer subject.Close()
Expect(subject.selector).To(HaveLen(1))
Expect(subject.selector[0].opt.Addr).To(Equal("127.0.0.1:6379"))
})
Describe("Next", func() {
BeforeEach(func() {
rand.Seed(100)
ms := int64(time.Millisecond)
subject.selector = pool{
&redisBackend{opt: mockOpts("host-1:6379"), up: 0, connections: 0, latency: ms},
&redisBackend{opt: mockOpts("host-2:6379"), up: 1, connections: 10, latency: 2 * ms},
&redisBackend{opt: mockOpts("host-3:6379"), up: 1, connections: 8, latency: 3 * ms},
&redisBackend{opt: mockOpts("host-4:6379"), up: 1, connections: 14, latency: ms},
}
})
It("should pick next backend (first-up)", func() {
subject.mode = ModeFirstUp
Expect(subject.pickNext().opt.Addr).To(Equal("host-2:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-2:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-2:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-2:6379"))
Expect(subject.selector[1].connections).To(Equal(int64(14)))
})
It("should pick next backend (least-conn)", func() {
subject.mode = ModeLeastConn
Expect(subject.pickNext().opt.Addr).To(Equal("host-3:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-3:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-2:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-3:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-2:6379"))
Expect(subject.selector[1].connections).To(Equal(int64(12)))
Expect(subject.selector[2].connections).To(Equal(int64(11)))
})
It("should pick next backend (min-latency)", func() {
subject.mode = ModeMinLatency
Expect(subject.pickNext().opt.Addr).To(Equal("host-4:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-4:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-4:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-4:6379"))
Expect(subject.selector[3].connections).To(Equal(int64(18)))
})
It("should pick next backend (randomly)", func() {
subject.mode = ModeRandom
Expect(subject.pickNext().opt.Addr).To(Equal("host-3:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-4:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-3:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-2:6379"))
Expect(subject.selector[3].connections).To(Equal(int64(15)))
})
It("should pick next backend (weighted-latency)", func() {
subject.mode = ModeWeightedLatency
Expect(subject.pickNext().opt.Addr).To(Equal("host-4:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-4:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-2:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-2:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-4:6379"))
Expect(subject.selector[1].connections).To(Equal(int64(12)))
Expect(subject.selector[3].connections).To(Equal(int64(17)))
})
It("should pick next backend (round-robin)", func() {
subject.mode = ModeRoundRobin
Expect(subject.pickNext().opt.Addr).To(Equal("host-3:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-4:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-2:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-3:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-4:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-2:6379"))
Expect(subject.selector[3].connections).To(Equal(int64(16)))
})
It("should fallback on random when everything down", func() {
subject.selector[1].up = 0
subject.selector[2].up = 0
subject.selector[3].up = 0
Expect(subject.pickNext().opt.Addr).To(Equal("host-4:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-1:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-1:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-1:6379"))
Expect(subject.pickNext().opt.Addr).To(Equal("host-3:6379"))
})
})
})
// --------------------------------------------------------------------
func TestSuite(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "redis-balancer")
}
func mockOpts(addr string) *Options {
return &Options{
Network: "tcp", Addr: addr,
}
}