-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
260 lines (202 loc) · 5.42 KB
/
handler_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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package handler
import (
"bytes"
"fmt"
"github.com/r3labs/diff/v2"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"sync"
"testing"
"time"
)
func TestSemaphore(t *testing.T) {
s := newSemaphore(20)
for i := 0; i < 20; i++ {
if !s.acquire() {
t.Fatal("failed to acquire semaphore")
}
}
if s.acquire() {
t.Fatal("semaphore has been acquired but should not have")
}
for i := 0; i < 5; i++ {
s.release()
}
for i := 0; i < 5; i++ {
if !s.acquire() {
t.Fatal("failed to acquire semaphore")
}
}
for i := 0; i < 20; i++ {
s.release()
}
}
// TestHandlerOneIncomingRequest checks case when requests limit is not exceeded.
func TestHandlerRequestLimitNotExceeded(t *testing.T) {
requestsLimit := 5
clientTimeout := time.Millisecond * 500
server := createServer(clientTimeout)
s := httptest.NewServer(NewHandler(WithClient(server.Client()), LimitRequests(requestsLimit)))
var wg sync.WaitGroup
ch := make(chan error)
for i := 0; i < requestsLimit; i++ {
wg.Add(1)
go func() {
defer wg.Done()
resp, err := s.Client().Post(
s.URL,
"text/plain",
getRequestBodyBuffer(
getUrl(server.URL, 100, time.Millisecond*100), // should be in time
getUrl(server.URL, 200, time.Millisecond*600), // should not be in time
getUrl(server.URL, 300, time.Millisecond*300), // should be in time
getUrl(server.URL, 400, time.Millisecond*50), // should be in time
getUrl(server.URL, 500, time.Millisecond*550), // should not be in time
),
)
if err != nil {
ch <- fmt.Errorf("failed to make request: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
ch <- fmt.Errorf("bad status code: %d", resp.StatusCode)
}
if err := checkResponse(resp, []int{100, 300, 400}); err != nil {
ch <- err
}
}()
}
go func() {
wg.Wait()
close(ch)
}()
for err := range ch {
t.Error(err)
}
}
// TestHandlerOneIncomingRequest checks case when requests limit is exceeded.
func TestHandlerRequestLimitExceeded(t *testing.T) {
requestsLimit := 5
clientTimeout := time.Millisecond * 500
server := createServer(clientTimeout)
s := httptest.NewServer(NewHandler(WithClient(server.Client()), LimitRequests(requestsLimit)))
var exceededRequests uint64
var wg sync.WaitGroup
errCh := make(chan error)
exceedCh := make(chan struct{})
for i := 0; i < requestsLimit*2; i++ {
wg.Add(1)
go func() {
defer wg.Done()
resp, err := s.Client().Post(
s.URL,
"text/plain",
getRequestBodyBuffer(
getUrl(server.URL, 100, time.Millisecond*100), // should be in time
getUrl(server.URL, 200, time.Millisecond*600), // should not be in time
getUrl(server.URL, 300, time.Millisecond*300), // should be in time
getUrl(server.URL, 400, time.Millisecond*50), // should be in time
getUrl(server.URL, 500, time.Millisecond*550), // should not be in time
),
)
if err != nil {
errCh <- fmt.Errorf("failed to make request: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusServiceUnavailable {
exceedCh <- struct{}{}
return
}
if resp.StatusCode != http.StatusOK {
errCh <- fmt.Errorf("bad status code: %d", resp.StatusCode)
}
if err := checkResponse(resp, []int{100, 300, 400}); err != nil {
errCh <- err
}
}()
}
go func() {
wg.Wait()
close(errCh)
close(exceedCh)
}()
go func() {
for range exceedCh {
exceededRequests++
}
}()
for err := range errCh {
t.Error(err)
}
if exceededRequests != 5 {
log.Fatalf("wrong number of exceeded requests, expected %d, got %d", 5, exceededRequests)
}
}
func TestHandler_ServeHTTP(t *testing.T) {
server := createServer(0)
http.Handle("/", NewHandler(WithClient(server.Client())))
go func() {
http.ListenAndServe("127.0.0.1:8082", nil)
}()
var wg sync.WaitGroup
for i := 0; i < 120; i++ {
wg.Add(1)
go func() {
defer wg.Done()
resp, err := http.Post(
"http://127.0.0.1:8082",
"text/plain",
bytes.NewBufferString(getUrl(server.URL, 100, time.Second)),
)
if err != nil {
fmt.Print(err)
return
}
defer resp.Body.Close()
c, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(c))
}()
}
wg.Wait()
}
func createServer(clientTimeout time.Duration) *httptest.Server {
s := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
length, _ := strconv.ParseUint(request.URL.Query().Get("length"), 10, 64)
timeout, _ := time.ParseDuration(request.URL.Query().Get("timeout"))
time.Sleep(timeout)
writer.Write(bytes.Repeat([]byte{' '}, int(length)))
}))
s.Client().Timeout = clientTimeout
return s
}
func getUrl(base string, responseLength int, responseTimeout time.Duration) string {
q := make(url.Values, 2)
q.Set("length", strconv.FormatInt(int64(responseLength), 10))
q.Set("timeout", responseTimeout.String())
u, _ := url.Parse(base)
u.RawQuery = q.Encode()
return u.String()
}
func getRequestBodyBuffer(urls ...string) io.Reader {
return bytes.NewBufferString(strings.Join(urls, "\n"))
}
func checkResponse(resp *http.Response, expectedData []int) error {
responseData := make([]int, 0)
for {
var v int
if _, err := fmt.Fscanf(resp.Body, "%d", &v); err != nil {
break
}
responseData = append(responseData, v)
}
if d, _ := diff.Diff(responseData, expectedData); len(d) != 0 {
return fmt.Errorf("expected data does not match response data: %s", d)
}
return nil
}