forked from bitbd83/fasthttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress_test.go
232 lines (201 loc) · 5.34 KB
/
compress_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
package fasthttp
import (
"bytes"
"fmt"
"io/ioutil"
"testing"
"time"
)
var compressTestcases = func() []string {
a := []string{
"",
"foobar",
"выфаодлодл одлфываыв sd2 k34",
}
bigS := createFixedBody(1e4)
a = append(a, string(bigS))
return a
}()
func TestGzipBytesSerial(t *testing.T) {
t.Parallel()
if err := testGzipBytes(); err != nil {
t.Fatal(err)
}
}
func TestGzipBytesConcurrent(t *testing.T) {
t.Parallel()
if err := testConcurrent(10, testGzipBytes); err != nil {
t.Fatal(err)
}
}
func TestDeflateBytesSerial(t *testing.T) {
t.Parallel()
if err := testDeflateBytes(); err != nil {
t.Fatal(err)
}
}
func TestDeflateBytesConcurrent(t *testing.T) {
t.Parallel()
if err := testConcurrent(10, testDeflateBytes); err != nil {
t.Fatal(err)
}
}
func testGzipBytes() error {
for _, s := range compressTestcases {
if err := testGzipBytesSingleCase(s); err != nil {
return err
}
}
return nil
}
func testDeflateBytes() error {
for _, s := range compressTestcases {
if err := testDeflateBytesSingleCase(s); err != nil {
return err
}
}
return nil
}
func testGzipBytesSingleCase(s string) error {
prefix := []byte("foobar")
gzippedS := AppendGzipBytes(prefix, []byte(s))
if !bytes.Equal(gzippedS[:len(prefix)], prefix) {
return fmt.Errorf("unexpected prefix when compressing %q: %q. Expecting %q", s, gzippedS[:len(prefix)], prefix)
}
gunzippedS, err := AppendGunzipBytes(prefix, gzippedS[len(prefix):])
if err != nil {
return fmt.Errorf("unexpected error when uncompressing %q: %s", s, err)
}
if !bytes.Equal(gunzippedS[:len(prefix)], prefix) {
return fmt.Errorf("unexpected prefix when uncompressing %q: %q. Expecting %q", s, gunzippedS[:len(prefix)], prefix)
}
gunzippedS = gunzippedS[len(prefix):]
if string(gunzippedS) != s {
return fmt.Errorf("unexpected uncompressed string %q. Expecting %q", gunzippedS, s)
}
return nil
}
func testDeflateBytesSingleCase(s string) error {
prefix := []byte("foobar")
deflatedS := AppendDeflateBytes(prefix, []byte(s))
if !bytes.Equal(deflatedS[:len(prefix)], prefix) {
return fmt.Errorf("unexpected prefix when compressing %q: %q. Expecting %q", s, deflatedS[:len(prefix)], prefix)
}
inflatedS, err := AppendInflateBytes(prefix, deflatedS[len(prefix):])
if err != nil {
return fmt.Errorf("unexpected error when uncompressing %q: %s", s, err)
}
if !bytes.Equal(inflatedS[:len(prefix)], prefix) {
return fmt.Errorf("unexpected prefix when uncompressing %q: %q. Expecting %q", s, inflatedS[:len(prefix)], prefix)
}
inflatedS = inflatedS[len(prefix):]
if string(inflatedS) != s {
return fmt.Errorf("unexpected uncompressed string %q. Expecting %q", inflatedS, s)
}
return nil
}
func TestGzipCompressSerial(t *testing.T) {
t.Parallel()
if err := testGzipCompress(); err != nil {
t.Fatal(err)
}
}
func TestGzipCompressConcurrent(t *testing.T) {
t.Parallel()
if err := testConcurrent(10, testGzipCompress); err != nil {
t.Fatal(err)
}
}
func TestFlateCompressSerial(t *testing.T) {
t.Parallel()
if err := testFlateCompress(); err != nil {
t.Fatal(err)
}
}
func TestFlateCompressConcurrent(t *testing.T) {
t.Parallel()
if err := testConcurrent(10, testFlateCompress); err != nil {
t.Fatal(err)
}
}
func testGzipCompress() error {
for _, s := range compressTestcases {
if err := testGzipCompressSingleCase(s); err != nil {
return err
}
}
return nil
}
func testFlateCompress() error {
for _, s := range compressTestcases {
if err := testFlateCompressSingleCase(s); err != nil {
return err
}
}
return nil
}
func testGzipCompressSingleCase(s string) error {
var buf bytes.Buffer
zw := acquireStacklessGzipWriter(&buf, CompressDefaultCompression)
if _, err := zw.Write([]byte(s)); err != nil {
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
}
releaseStacklessGzipWriter(zw, CompressDefaultCompression)
zr, err := acquireGzipReader(&buf)
if err != nil {
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
}
body, err := ioutil.ReadAll(zr)
if err != nil {
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
}
if string(body) != s {
return fmt.Errorf("unexpected string after decompression: %q. Expecting %q", body, s)
}
releaseGzipReader(zr)
return nil
}
func testFlateCompressSingleCase(s string) error {
var buf bytes.Buffer
zw := acquireStacklessDeflateWriter(&buf, CompressDefaultCompression)
if _, err := zw.Write([]byte(s)); err != nil {
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
}
releaseStacklessDeflateWriter(zw, CompressDefaultCompression)
zr, err := acquireFlateReader(&buf)
if err != nil {
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
}
body, err := ioutil.ReadAll(zr)
if err != nil {
return fmt.Errorf("unexpected error: %s. s=%q", err, s)
}
if string(body) != s {
return fmt.Errorf("unexpected string after decompression: %q. Expecting %q", body, s)
}
releaseFlateReader(zr)
return nil
}
func testConcurrent(concurrency int, f func() error) error {
ch := make(chan error, concurrency)
for i := 0; i < concurrency; i++ {
go func(idx int) {
err := f()
if err != nil {
ch <- fmt.Errorf("error in goroutine %d: %s", idx, err)
}
ch <- nil
}(i)
}
for i := 0; i < concurrency; i++ {
select {
case err := <-ch:
if err != nil {
return err
}
case <-time.After(time.Second):
return fmt.Errorf("timeout")
}
}
return nil
}