-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlb_http_test.go
142 lines (125 loc) · 2.76 KB
/
lb_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
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
// Copyright © 2021 Hedzr Yeh.
package lb
import (
"context"
"fmt"
"github.com/hedzr/lb/lbapi"
"github.com/hedzr/lb/wrr"
"io/ioutil"
"net/http"
"strings"
"sync"
"testing"
)
func TestWebServers(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", helloHandler1)
srv := &http.Server{
Addr: ":38080",
Handler: mux,
}
mux = http.NewServeMux()
mux.HandleFunc("/", helloHandler2)
srv1 := &http.Server{
Addr: ":38081",
Handler: mux,
}
var err error
var exitCh = make(chan struct{})
var wgStartup sync.WaitGroup
wgStartup.Add(4)
go func() {
fmt.Println("Server started at port 38080")
wgStartup.Done()
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
t.Errorf("listen: %+v\n", err)
}
}()
go func() {
fmt.Println("Server started at port 38081")
wgStartup.Done()
if err := srv1.ListenAndServe(); err != nil && err != http.ErrServerClosed {
t.Errorf("listen: %+v\n", err)
}
}()
go func() {
wgStartup.Done()
<-exitCh
if err = srv.Shutdown(context.Background()); err != nil {
t.Errorf("server Shutdown Failed: %+v", err)
}
if err == http.ErrServerClosed {
err = nil
}
}()
go func() {
wgStartup.Done()
<-exitCh
if err = srv1.Shutdown(context.Background()); err != nil {
t.Errorf("server Shutdown Failed: %+v", err)
}
if err == http.ErrServerClosed {
err = nil
}
}()
var wg sync.WaitGroup
wg.Add(1)
sum := make(map[lbapi.Peer]int)
res := make(map[string]int)
go func() {
defer wg.Done()
wgStartup.Wait()
t.Logf("requesting...")
lb := wrr.New(wrr.WithPeersAndWeights(
[]lbapi.Peer{
&exP{"http://localhost:38080", 2},
&exP{"http://localhost:38081", 3},
},
[]int{2, 3},
))
for i := 0; i < 300; i++ {
p, _ := lb.Next(lbapi.DummyFactor)
sum[p]++
content, err := clientGet(t, p.String())
if err == nil {
res[string(content)]++
} else {
t.Errorf("client Get failed: %v", err)
}
}
lb.Clear()
}()
wg.Wait()
for k, v := range res {
t.Logf("%v : %v", strings.TrimRight(k, "\r\n"), v)
}
}
type exP struct {
addr string
weight int
}
func (s *exP) String() string { return s.addr }
func (s *exP) Weight() int { return s.weight }
func helloHandler1(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, there 38080\n")
}
func helloHandler2(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, there 38081\n")
}
func clientGet(t *testing.T, url string) (content []byte, err error) {
var resp *http.Response
resp, err = http.Get(url)
if err != nil {
t.Log("Get failed:", err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Log("statuscode:", resp.StatusCode)
}
content, err = ioutil.ReadAll(resp.Body)
if err != nil {
t.Log("Read failed:", err)
}
return
}