@@ -2,6 +2,7 @@ package gateway
2
2
3
3
import (
4
4
"io"
5
+ "math/rand"
5
6
"net"
6
7
"net/http"
7
8
"net/http/httptest"
@@ -11,12 +12,17 @@ import (
11
12
)
12
13
13
14
type mockDNSResolver struct {
14
- IPs []net.IP
15
- Err error
15
+ IPs []net.IP
16
+ Err error
17
+ Rand * rand.Rand
16
18
}
17
19
18
20
func (m mockDNSResolver ) LookupIP (hostname string ) ([]net.IP , error ) {
19
- return m .IPs , m .Err
21
+ shuffledIPs := make ([]net.IP , len (m .IPs ))
22
+ copy (shuffledIPs , m .IPs )
23
+ rand .Shuffle (len (shuffledIPs ), func (i , j int ) { shuffledIPs [i ], shuffledIPs [j ] = shuffledIPs [j ], shuffledIPs [i ] })
24
+
25
+ return shuffledIPs , m .Err
20
26
}
21
27
22
28
type customRoundTripper struct {}
@@ -31,6 +37,7 @@ func (rt customRoundTripper) RoundTrip(req *http.Request) (*http.Response, error
31
37
}
32
38
33
39
func TestDistribution (t * testing.T ) {
40
+ r := rand .New (rand .NewSource (time .Now ().UnixNano ()))
34
41
hostname := "example.com"
35
42
testCases := []struct {
36
43
name string
@@ -109,8 +116,9 @@ func TestDistribution(t *testing.T) {
109
116
for _ , tc := range testCases {
110
117
t .Run (tc .name , func (t * testing.T ) {
111
118
mockResolver := mockDNSResolver {
112
- IPs : tc .IPs ,
113
- Err : nil ,
119
+ IPs : tc .IPs ,
120
+ Err : nil ,
121
+ Rand : r ,
114
122
}
115
123
116
124
lb , err := newRoundRobinLoadBalancer (hostname , mockResolver .LookupIP )
0 commit comments