-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_balance_test.go
201 lines (168 loc) · 4.88 KB
/
load_balance_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package grpcloadbalancing
import (
"fmt"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
"google.golang.org/grpc"
)
type tempEndpoint struct {
endpoint *Endpoint
iteration int
}
var generator = func() (*grpc.ClientConn, error) {
return &grpc.ClientConn{}, nil
}
func TestGcd(t *testing.T) {
Convey("test gcd", t, func() {
Convey("b = 0", func() {
res := gcd(2, 0)
So(res, ShouldEqual, 2)
})
Convey("running well", func() {
res := gcd(9, 6)
So(res, ShouldEqual, 3)
})
Convey("calculate all gcd", func() {
end1, _ := NewEndpoint("localhost:8080", 2, 20, generator)
end2, _ := NewEndpoint("localhost:8888", 2, 20, generator)
end3, _ := NewEndpoint("localhost:9999", 7, 20, generator)
gcd := <-calculateEndpointsGCD([]*Endpoint{end1, end2, end3})
So(gcd, ShouldEqual, 1)
})
Convey("calculate all gcd 2", func() {
end1, _ := NewEndpoint("localhost:8080", 2, 20, generator)
end2, _ := NewEndpoint("localhost:8888", 2, 20, generator)
end3, _ := NewEndpoint("localhost:9999", 6, 20, generator)
gcd := <-calculateEndpointsGCD([]*Endpoint{end1, end2, end3})
So(gcd, ShouldEqual, 2)
})
})
}
func TestMaxWeight(t *testing.T) {
Convey("test max weight", t, func() {
Convey("test max weight ordered", func() {
end1, _ := NewEndpoint("localhost:8080", 2, 20, generator)
end2, _ := NewEndpoint("localhost:8888", 2, 20, generator)
end3, _ := NewEndpoint("localhost:9999", 6, 20, generator)
max := <-getMaxWeight([]*Endpoint{end1, end2, end3})
So(max, ShouldEqual, 6)
})
Convey("test max weight unordered", func() {
end1, _ := NewEndpoint("localhost:8080", 2, 20, generator)
end2, _ := NewEndpoint("localhost:8888", 9, 20, generator)
end3, _ := NewEndpoint("localhost:9999", 6, 20, generator)
max := <-getMaxWeight([]*Endpoint{end1, end2, end3})
So(max, ShouldEqual, 9)
})
})
}
func TestAllLoadBalance(t *testing.T) {
Convey("Test All load balance", t, func() {
// creating struct
end1, _ := NewEndpoint("localhost:8080", 1, 20, generator)
end2, _ := NewEndpoint("localhost:8888", 1, 20, generator)
end3, _ := NewEndpoint("localhost:9999", 3, 20, generator)
res := NewLoadBalance([]*Endpoint{end1, end2, end3})
So(res, ShouldNotBeNil)
Convey("test creating load balance", func() {
So(res.cw, ShouldEqual, 0)
So(len(res.endpoints), ShouldEqual, 3)
So(res.lastSelected, ShouldEqual, -1)
So(res.gcd, ShouldEqual, 1)
})
Convey("test get then reset", func() {
// 9999, 9999, 8080, 8888, 9999
// first get
e, err := res.Get()
So(err, ShouldBeNil)
So(e.url, ShouldEqual, "localhost:9999")
// second get
e, err = res.Get()
So(err, ShouldBeNil)
So(e.url, ShouldEqual, "localhost:9999")
// third get
e, err = res.Get()
So(err, ShouldBeNil)
So(e.url, ShouldEqual, "localhost:8080")
// fourth get
e, err = res.Get()
So(err, ShouldBeNil)
So(e.url, ShouldEqual, "localhost:8888")
// fifth get
e, err = res.Get()
So(err, ShouldBeNil)
So(e.url, ShouldEqual, "localhost:9999")
// validate element parameter
So(res.gcd, ShouldEqual, 1)
So(res.maxWeight, ShouldEqual, 3)
So(res.cw, ShouldEqual, 1)
So(res.lastSelected, ShouldEqual, 2)
// reset it
res.reset()
So(res.gcd, ShouldEqual, 1)
So(res.cw, ShouldEqual, 0)
So(res.lastSelected, ShouldEqual, -1)
So(res.maxWeight, ShouldEqual, 3)
})
Convey("test get with multiple goroutine", func() {
chanEndpoints := make(chan *tempEndpoint, 3)
chanError := make(chan error)
defer func() {
close(chanEndpoints)
close(chanError)
}()
for i := 0; i < 3; i++ {
go func(iteration int) {
e, err := res.Get()
if err != nil {
chanError <- err
return
}
tmp := &tempEndpoint{
endpoint: e,
iteration: iteration,
}
chanEndpoints <- tmp
}(i)
time.Sleep(1 * time.Millisecond)
}
// store temp result
tmpResults := []string{"localhost:9999", "localhost:9999", "localhost:8080"}
var succeed, fail int
for i := 0; i < 3; i++ {
fmt.Println("Waiting ")
select {
case e := <-chanEndpoints:
So(e.endpoint.url, ShouldEqual, tmpResults[e.iteration])
succeed++
case <-chanError:
fail++
}
}
So(succeed, ShouldEqual, 3)
So(fail, ShouldEqual, 0)
// validate parameter
So(res.gcd, ShouldEqual, 1)
So(res.maxWeight, ShouldEqual, 3)
So(res.cw, ShouldEqual, 1)
So(res.lastSelected, ShouldEqual, 0)
// reset it
res.reset()
So(res.gcd, ShouldEqual, 1)
So(res.cw, ShouldEqual, 0)
So(res.lastSelected, ShouldEqual, -1)
So(res.maxWeight, ShouldEqual, 3)
})
Convey("test get then add", func() {
e, err := res.Get()
So(err, ShouldBeNil)
So(e.url, ShouldEqual, "localhost:9999")
// validate parameter
So(res.gcd, ShouldEqual, 1)
So(res.maxWeight, ShouldEqual, 3)
So(res.cw, ShouldEqual, 3)
So(res.lastSelected, ShouldEqual, 2)
})
})
}