-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathhttp_test.go
224 lines (205 loc) · 5.36 KB
/
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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package katsubushi_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http/httptest"
"strconv"
"testing"
"time"
"github.com/kayac/go-katsubushi/v2"
)
var httpApp *katsubushi.App
var httpPort int
func init() {
var err error
httpApp, err = katsubushi.New(80)
if err != nil {
panic(err)
}
listener, err := net.Listen("tcp", "localhost:0")
if err != nil {
panic(err)
}
httpPort = listener.Addr().(*net.TCPAddr).Port
go httpApp.RunHTTPServer(context.Background(), &katsubushi.Config{HTTPListener: listener})
time.Sleep(3 * time.Second)
}
func TestHTTPSingle(t *testing.T) {
req := httptest.NewRequest("GET", "/id", nil)
w := httptest.NewRecorder()
httpApp.HTTPGetSingleID(w, req)
if w.Code != 200 {
t.Errorf("status code should be 200 but %d", w.Code)
}
b := new(bytes.Buffer)
if _, err := io.Copy(b, w.Body); err != nil {
t.Errorf("failed to read body: %v", err)
}
if id, err := strconv.ParseUint(b.String(), 10, 64); err != nil {
t.Errorf("body should be a number uint64: %v", err)
} else {
t.Logf("HTTP fetched single ID: %d", id)
}
}
func TestHTTPSingleJSON(t *testing.T) {
req := httptest.NewRequest("GET", "/id", nil)
req.Header.Set("Accept", "application/json")
w := httptest.NewRecorder()
httpApp.HTTPGetSingleID(w, req)
if w.Code != 200 {
t.Errorf("status code should be 200 but %d", w.Code)
}
v := struct {
ID string `json:"id"`
}{}
if err := json.NewDecoder(w.Body).Decode(&v); err != nil {
t.Errorf("failed to decode body: %v", err)
}
if id, err := strconv.ParseUint(v.ID, 10, 64); err != nil {
t.Errorf("body should be a number uint64: %v", err)
} else {
t.Logf("HTTP fetched single ID as JSON: %d", id)
}
}
func TestHTTPMulti(t *testing.T) {
req := httptest.NewRequest("GET", "/ids?n=10", nil)
w := httptest.NewRecorder()
httpApp.HTTPGetMultiID(w, req)
if w.Code != 200 {
t.Errorf("status code should be 200 but %d", w.Code)
}
b := new(bytes.Buffer)
if _, err := io.Copy(b, w.Body); err != nil {
t.Errorf("failed to read body: %v", err)
}
bs := bytes.Split(b.Bytes(), []byte("\n"))
if len(bs) != 10 {
t.Errorf("body should contain 10 lines but %d", len(bs))
}
for _, b := range bs {
if id, err := strconv.ParseUint(string(b), 10, 64); err != nil {
t.Errorf("body should be a number uint64: %v", err)
} else {
t.Logf("HTTP fetched ID: %d", id)
}
}
}
func TestHTTPMultiJSON(t *testing.T) {
req := httptest.NewRequest("GET", "/ids?n=10", nil)
req.Header.Set("Accept", "application/json")
w := httptest.NewRecorder()
httpApp.HTTPGetMultiID(w, req)
if w.Code != 200 {
t.Errorf("status code should be 200 but %d", w.Code)
}
v := struct {
IDs []string `json:"ids"`
}{}
if err := json.NewDecoder(w.Body).Decode(&v); err != nil {
t.Errorf("failed to decode body: %v", err)
}
if len(v.IDs) != 10 {
t.Errorf("body should contain 10 lines but %d", len(v.IDs))
}
for _, id := range v.IDs {
if i, err := strconv.ParseUint(id, 10, 64); err != nil {
t.Errorf("body should be a number uint64: %v", err)
} else {
t.Logf("HTTP fetched single ID as JSON: %d", i)
}
}
}
func testHTTPStats(t *testing.T) *katsubushi.MemdStats {
req := httptest.NewRequest("GET", "/stats", nil)
req.Header.Set("Accept", "application/json")
w := httptest.NewRecorder()
httpApp.HTTPGetStats(w, req)
if w.Code != 200 {
t.Errorf("status code should be 200 but %d", w.Code)
}
var s katsubushi.MemdStats
if err := json.NewDecoder(w.Body).Decode(&s); err != nil {
t.Errorf("failed to read body: %v", err)
}
t.Logf("%#v", s)
return &s
}
func TestHTTPStats(t *testing.T) {
TestHTTPSingle(t)
s1 := testHTTPStats(t)
TestHTTPSingle(t)
s2 := testHTTPStats(t)
if s2.CmdGet != s1.CmdGet+1 {
t.Errorf("cmd_get should be incremented by 1 but %d", s2.CmdGet-s1.CmdGet)
}
if s2.GetHits != s1.GetHits+1 {
t.Errorf("get_hits should be incremented by 1 but %d", s2.GetHits-s1.GetHits)
}
TestHTTPMulti(t)
s3 := testHTTPStats(t)
if s3.CmdGet != s2.CmdGet+1 {
t.Errorf("cmd_get should be incremented by 10 but %d", s3.CmdGet-s2.CmdGet)
}
if s3.GetHits != s2.GetHits+10 {
t.Errorf("get_hits should be incremented by 10 but %d", s3.GetHits-s2.GetHits)
}
}
func TestHTTPSingleCS(t *testing.T) {
u := fmt.Sprintf("http://localhost:%d", httpPort)
client, err := katsubushi.NewHTTPClient([]string{u}, "")
if err != nil {
t.Fatal(err)
}
for i := 0; i < 10; i++ {
id, err := client.Fetch(context.Background())
if err != nil {
t.Fatal(err)
}
if id == 0 {
t.Fatal("id should not be 0")
}
t.Logf("HTTP fetched single ID: %d", id)
}
}
func TestHTTPMultiCS(t *testing.T) {
u := fmt.Sprintf("http://localhost:%d", httpPort)
client, err := katsubushi.NewHTTPClient([]string{u}, "")
if err != nil {
t.Fatal(err)
}
for i := 0; i < 10; i++ {
ids, err := client.FetchMulti(context.Background(), 10)
if err != nil {
t.Fatal(err)
}
if len(ids) != 10 {
t.Fatalf("ids should contain 10 elements %v", ids)
}
for _, id := range ids {
if id == 0 {
t.Fatal("id should not be 0")
}
}
t.Logf("HTTP fetched IDs: %v", ids)
}
}
func BenchmarkHTTPClientFetch(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
u := fmt.Sprintf("http://localhost:%d", httpPort)
c, _ := katsubushi.NewHTTPClient([]string{u}, "")
for pb.Next() {
id, err := c.Fetch(context.Background())
if err != nil {
b.Fatal(err)
}
if id == 0 {
b.Error("could not fetch id > 0")
}
}
})
}