-
Notifications
You must be signed in to change notification settings - Fork 0
/
balancer_roundrobin_http_test.go
90 lines (71 loc) · 1.73 KB
/
balancer_roundrobin_http_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
package stargate
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
)
type httptestLister struct {
routes map[string][]*RouteOptions
}
func (h httptestLister) List(s string) ([]*RouteOptions, error) {
return h.routes[s], nil
}
func (h httptestLister) ListAll() (map[string][]*RouteOptions, error) {
return h.routes, nil
}
func newLister(servers []*httptest.Server, protocol string) ServiceLister {
sv := make([]*RouteOptions, 0)
for _, s := range servers {
sv = append(sv, makeRouteOption(s, protocol))
}
return httptestLister{
routes: map[string][]*RouteOptions{
"/": sv,
},
}
}
func TestRoundRobinHTTP(t *testing.T) {
maxServers := 3
backends := make([]*httptest.Server, maxServers)
for i := 1; i <= maxServers; i++ {
n := fmt.Sprintf("server_%d", i)
backends[i-1] = httptest.NewServer(namedHandler(n))
t.Logf("Named server %s ready", n)
}
defer func() {
for _, s := range backends {
s.Close()
}
}()
ls := newLister(backends, "http")
sg, err := NewRouter(ls)
if err != nil {
t.Errorf("Cannot create stargate proxy : %v", err)
}
server := httptest.NewServer(sg)
defer server.Close()
t.Logf("Stargate ready at %s", server.Listener.Addr().String())
client := &http.Client{}
for i, j := 1, 1; i < 10; i++ {
get, err := client.Get(makeRouteOption(server, "http").Address)
if err != nil {
t.Error(err)
}
b, err := io.ReadAll(get.Body)
if err != nil {
t.Fatalf("cannot read response from server %v", err)
}
resp := string(b)
expected := fmt.Sprintf("server_%d", j)
if resp != expected {
t.Errorf("RoundRobin failure - got '%s' expected '%s'", resp, expected)
}
j++
if j > maxServers {
j = 1
}
t.Logf("> iter %d passed with response %s", i, resp)
}
}