forked from valyala/fasthttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streaming_test.go
191 lines (161 loc) · 4.08 KB
/
streaming_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
package fasthttp
import (
"bufio"
"bytes"
"io/ioutil"
"sync"
"testing"
"time"
"github.com/valyala/fasthttp/fasthttputil"
)
func TestStreamingPipeline(t *testing.T) {
t.Parallel()
reqS := `POST /one HTTP/1.1
Host: example.com
Content-Length: 10
aaaaaaaaaa
POST /two HTTP/1.1
Host: example.com
Content-Length: 10
aaaaaaaaaa`
ln := fasthttputil.NewInmemoryListener()
s := &Server{
StreamRequestBody: true,
Handler: func(ctx *RequestCtx) {
body := ""
expected := "aaaaaaaaaa"
if string(ctx.Path()) == "/one" {
body = string(ctx.PostBody())
} else {
all, err := ioutil.ReadAll(ctx.RequestBodyStream())
if err != nil {
t.Error(err)
}
body = string(all)
}
if body != expected {
t.Errorf("expected %q got %q", expected, body)
}
},
}
ch := make(chan struct{})
go func() {
if err := s.Serve(ln); err != nil {
t.Errorf("unexpected error: %s", err)
}
close(ch)
}()
conn, err := ln.Dial()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if _, err = conn.Write([]byte(reqS)); err != nil {
t.Fatalf("unexpected error: %s", err)
}
var resp Response
br := bufio.NewReader(conn)
respCh := make(chan struct{})
go func() {
if err := resp.Read(br); err != nil {
t.Errorf("error when reading response: %s", err)
}
if resp.StatusCode() != StatusOK {
t.Errorf("unexpected status code %d. Expecting %d", resp.StatusCode(), StatusOK)
}
if err := resp.Read(br); err != nil {
t.Errorf("error when reading response: %s", err)
}
if resp.StatusCode() != StatusOK {
t.Errorf("unexpected status code %d. Expecting %d", resp.StatusCode(), StatusOK)
}
close(respCh)
}()
select {
case <-respCh:
case <-time.After(time.Second):
t.Fatal("timeout")
}
if err := ln.Close(); err != nil {
t.Fatalf("error when closing listener: %s", err)
}
select {
case <-ch:
case <-time.After(time.Second):
t.Fatal("timeout when waiting for the server to stop")
}
}
func getChunkedTestEnv(t testing.TB) (*fasthttputil.InmemoryListener, []byte) {
body := createFixedBody(128 * 1024)
chunkedBody := createChunkedBody(body)
testHandler := func(ctx *RequestCtx) {
bodyBytes, err := ioutil.ReadAll(ctx.RequestBodyStream())
if err != nil {
t.Logf("ioutil read returned err=%s", err)
t.Error("unexpected error while reading request body stream")
}
if !bytes.Equal(body, bodyBytes) {
t.Errorf("unexpected request body, expected %q, got %q", body, bodyBytes)
}
}
s := &Server{
Handler: testHandler,
StreamRequestBody: true,
MaxRequestBodySize: 1, // easier to test with small limit
}
ln := fasthttputil.NewInmemoryListener()
go func() {
err := s.Serve(ln)
if err != nil {
t.Errorf("could not serve listener: %s", err)
}
}()
req := Request{}
req.SetHost("localhost")
req.Header.SetMethod("POST")
req.Header.Set("transfer-encoding", "chunked")
req.Header.SetContentLength(-1)
formattedRequest := req.Header.Header()
formattedRequest = append(formattedRequest, chunkedBody...)
return ln, formattedRequest
}
func TestRequestStream(t *testing.T) {
ln, formattedRequest := getChunkedTestEnv(t)
c, err := ln.Dial()
if err != nil {
t.Errorf("unexpected error while dialing: %s", err)
}
if _, err = c.Write(formattedRequest); err != nil {
t.Errorf("unexpected error while writing request: %s", err)
}
br := bufio.NewReader(c)
var respH ResponseHeader
if err = respH.Read(br); err != nil {
t.Errorf("unexpected error: %s", err)
}
}
func BenchmarkRequestStreamE2E(b *testing.B) {
ln, formattedRequest := getChunkedTestEnv(b)
wg := &sync.WaitGroup{}
wg.Add(4)
for i := 0; i < 4; i++ {
go func(wg *sync.WaitGroup) {
for i := 0; i < b.N/4; i++ {
c, err := ln.Dial()
if err != nil {
b.Errorf("unexpected error while dialing: %s", err)
}
if _, err = c.Write(formattedRequest); err != nil {
b.Errorf("unexpected error while writing request: %s", err)
}
br := bufio.NewReaderSize(c, 128)
var respH ResponseHeader
if err = respH.Read(br); err != nil {
b.Errorf("unexpected error: %s", err)
}
c.Close()
}
wg.Done()
}(wg)
}
wg.Wait()
}