-
Notifications
You must be signed in to change notification settings - Fork 0
/
compressor_test.go
337 lines (289 loc) · 7.84 KB
/
compressor_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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package gompress
import (
"archive/zip"
"bytes"
"errors"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"testing"
)
const (
testFolder = "./testdata"
enwik8URL = "https://data.deepai.org/enwik8.zip"
)
var (
testFile = filepath.Join(testFolder, "enwik8")
m1 = 1024 * 1024
defaultBenchmarks = []struct {
name string
size int
}{
{"4M", m1 * 4},
{"16M", m1 * 16},
{"64M", m1 * 64},
}
defaultTest = make([]byte, 1024*1024)
// used to test invalid compression levels
faultyCompressionLevel = &CompressionLevel{
level: 5,
minLevel: 1,
maxLevel: 100,
}
errMock = errors.New("mock error")
)
type mockCompressor struct{}
func (m *mockCompressor) Compress(r io.Reader, w io.Writer) error {
return nil
}
func (m *mockCompressor) Decompress(r io.Reader, w io.Writer) error {
return nil
}
type errMockCompressor struct{}
func (e *errMockCompressor) Compress(r io.Reader, w io.Writer) error {
return errMock
}
func (e *errMockCompressor) Decompress(r io.Reader, w io.Writer) error {
return errMock
}
type mockFs struct {
osFS
errCreate error
errOpen error
errStat error
}
type mockedFileInfo struct {
os.FileInfo
}
func (m mockFs) Create(filename string) (*os.File, error) {
if m.errCreate != nil {
return nil, m.errCreate
}
return &os.File{}, nil
}
func (m mockFs) Open(filename string) (*os.File, error) {
if m.errOpen != nil {
return nil, m.errOpen
}
return &os.File{}, nil
}
func (m mockFs) Stat(filename string) (os.FileInfo, error) {
if m.errStat != nil {
return nil, m.errStat
}
return &mockedFileInfo{}, nil
}
func (m *mockFs) reset() {
m.errCreate = nil
m.errOpen = nil
m.errStat = os.ErrNotExist // default for isExists
}
func init() {
// test folder
if _, err := os.Stat(testFolder); os.IsNotExist(err) {
if err := os.Mkdir(testFolder, 0777); err != nil {
panic(err)
}
}
// download test data if needed
if _, err := os.Stat(testFile); os.IsNotExist(err) {
out, err := os.Create(testFile + ".zip")
resp, err := http.Get(enwik8URL)
if err != nil {
panic(err)
}
_, err = io.Copy(out, resp.Body)
if err != nil {
panic(err)
}
reader, err := zip.OpenReader(testFile + ".zip")
if err != nil {
panic(err)
}
for _, file := range reader.File {
if file.Name == "enwik8" {
fileReader, err := file.Open()
if err != nil {
panic(err)
}
defer fileReader.Close()
out, err := os.Create(testFile)
if _, err := io.Copy(out, fileReader); err != nil {
panic(err)
}
}
}
} else if err != nil {
panic(err)
}
}
func TestNewFileCompressor(t *testing.T) {
fC := NewFileCompressor(&mockCompressor{}, "mock", "mock")
if fC == nil {
t.Error("NewFileCompressor returned nil pointer")
}
}
func TestFileCompressor(t *testing.T) {
fC := NewFileCompressor(&mockCompressor{}, "mock", "mock")
oldFs := fs
mfs := &mockFs{}
fs = mfs
defer func() {
fs = oldFs
}()
mfs.errStat = os.ErrNotExist
// test successful CompressFile/DecompressFile, set errStat to handle isExists
t.Run("CompressFile", func(t *testing.T) {
if err := fC.CompressFile(testFile, testFile+".mock"); err != nil {
t.Error(err)
}
})
t.Run("DecompressFile", func(t *testing.T) {
if err := fC.DecompressFile(testFile+".mock", testFile+".tmp"); err != nil {
t.Error(err)
}
})
// test existing destination output, set errStat to os.ErrExist
mfs.errStat = os.ErrExist
t.Run("ExistingFileDest", func(t *testing.T) {
if err := fC.CompressFile(testFile, testFile); err != os.ErrExist {
t.Errorf("expecting os.ErrExist, got %v", err)
}
if err := fC.DecompressFile(testFile, testFile); err != os.ErrExist {
t.Errorf("expecting os.ErrExist, got %v", err)
}
})
// test non-existing file input, set errOpen to os.ErrNotExist
mfs.reset()
mfs.errOpen = os.ErrNotExist
t.Run("NonExistingFileInput", func(t *testing.T) {
if err := fC.CompressFile(testFolder+"/invalid", testFolder+"/invalid.mock"); err != os.ErrNotExist {
t.Errorf("expecting os.ErrNotExist, got %v", err)
}
if err := fC.DecompressFile(testFolder+"/invalid", testFolder+"/invalid"); err != os.ErrNotExist {
t.Errorf("expecting os.ErrNotExist, got %v", err)
}
})
// test error creating output file
mfs.reset()
mfs.errCreate = errMock
t.Run("ErrorCreatingFileOutput", func(t *testing.T) {
if err := fC.CompressFile(testFile, testFile+".mock"); err != errMock {
t.Errorf("expecting errMock, got %v", err)
}
if err := fC.DecompressFile(testFile+".mock", testFile); err != errMock {
t.Errorf("expecting errMock, got %v", err)
}
})
// test FileCompressor initialized without CompressorDecompressor
t.Run("NoCompressorDecompressor", func(t *testing.T) {
fC := &FileCompressor{}
if err := fC.CompressFile(testFile, testFile+".mock"); err != errNoCompressorDecompressor {
t.Errorf("expecting errNoCompressorDecompressor, got %v", err)
}
if err := fC.DecompressFile(testFile+".mock", testFile); err != errNoCompressorDecompressor {
t.Errorf("expecting errNoCompressorDecompressor, got %v", err)
}
})
// test CompressorDecompressor returning error
mfs.reset()
t.Run("ErrCompressorDecompressor", func(t *testing.T) {
fC := NewFileCompressor(&errMockCompressor{}, "err-mock", "err-mock")
if err := fC.CompressFile(testFile, testFile+".mock"); err == nil {
t.Errorf("expecting errMock compressing file, got %v", err)
}
if err := fC.DecompressFile(testFile+".mock", testFile+".tmp"); err == nil {
t.Errorf("expecting errMock decompressing file, got %v", err)
}
})
}
func testCompressDecompress(t *testing.T, cD CompressorDecompressor) {
f, err := os.Open(testFile)
if err != nil {
panic(err)
}
var in bytes.Buffer
var out bytes.Buffer
var buffer = make([]byte, 1024*512)
f.Read(buffer)
in.Write(buffer)
t.Run("Compress", func(t *testing.T) {
if err := cD.Compress(&in, &out); err != nil {
t.Error(err)
}
})
t.Run("Decompress", func(t *testing.T) {
var compare bytes.Buffer
if err := cD.Decompress(&out, &compare); err != nil {
t.Error(err)
}
if bytes.Compare(compare.Bytes(), buffer) != 0 {
t.Error("decompression output was not the same as compression input")
}
})
t.Run("DecompressInvalidData", func(t *testing.T) {
var in bytes.Buffer
var out bytes.Buffer
var buffer = make([]byte, 1024*512)
f.Seek(0, 0)
f.Read(buffer)
in.Write(buffer)
if err := cD.Decompress(&in, &out); err == nil {
// some compressors don't return error for decompress, and simply copy data
if len(in.Bytes()) == 0 || len(out.Bytes()) == 0 && bytes.Compare(in.Bytes(), out.Bytes()) != 0 {
t.Error("expecting an error, or for data to copy, neither case happened")
}
}
})
l, leveller := cD.(Leveller)
if leveller {
t.Run("Level", func(t *testing.T) {
if err := l.SetLevel(l.MinLevel() - 1); err == nil {
t.Error("expecting an error whilst setting invalid compression level, got none")
}
if err := l.SetLevel(l.MaxLevel() + 1); err == nil {
t.Error("expecting an error whilst setting invalid compression level, got none")
}
})
}
}
func benchmarkCompressDecompress(b *testing.B, cD CompressorDecompressor) {
data, err := os.Open(testFile)
if err != nil {
panic(err)
}
for _, bench := range defaultBenchmarks {
var w bytes.Buffer
var r bytes.Buffer
b.Run(bench.name, func(b *testing.B) {
var buffer = make([]byte, bench.size)
data.Seek(0, 0)
data.Read(buffer)
r.Write(buffer)
if !testing.Short() {
l, isLevelled := cD.(Leveller)
if isLevelled {
for n := l.MinLevel(); n <= l.MaxLevel(); n++ {
b.Run("CompressLevel-"+strconv.Itoa(n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
if err := cD.Compress(&r, &w); err != nil {
b.Error(err)
}
}
})
}
}
} else {
var buffer = make([]byte, 1024*1024)
r.Write(buffer)
for i := 0; i < b.N; i++ {
if err := cD.Compress(&r, &w); err != nil {
b.Error(err)
}
}
}
})
}
}