-
Notifications
You must be signed in to change notification settings - Fork 104
/
compressor_test.go
97 lines (82 loc) · 2.29 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
package gocb
type maybeCompressedResponseValue struct {
compressed []byte
uncompressed []byte
}
func (c *maybeCompressedResponseValue) GetContentCompressed() []byte {
return c.compressed
}
func (c *maybeCompressedResponseValue) GetContentUncompressed() []byte {
return c.uncompressed
}
func (suite *UnitTestSuite) TestCompressor() {
suite.Run("Value should compress and decompress", func() {
var val []byte
for i := 0; i < 500; i++ {
val = append(val, byte(i))
}
compressor := &compressor{
CompressionEnabled: true,
CompressionMinRatio: 1.0,
CompressionMinSize: 5,
}
after, isCompressed := compressor.Compress(val)
suite.Assert().True(isCompressed)
suite.Assert().NotEqual(val, after)
decompressed, err := compressor.Decompress(&maybeCompressedResponseValue{compressed: after})
suite.Require().NoError(err)
suite.Assert().Equal(val, decompressed)
})
suite.Run("Value too small", func() {
var val []byte
for i := 0; i < 2; i++ {
val = append(val, byte(i))
}
compressor := &compressor{
CompressionEnabled: true,
CompressionMinRatio: 1.0,
CompressionMinSize: 5,
}
after, isCompressed := compressor.Compress(val)
suite.Assert().False(isCompressed)
suite.Assert().Equal(val, after)
})
suite.Run("Value above ratio", func() {
var val []byte
for i := 0; i < 50; i++ {
val = append(val, byte(i))
}
compressor := &compressor{
CompressionEnabled: true,
CompressionMinRatio: 0.05,
CompressionMinSize: 5,
}
after, isCompressed := compressor.Compress(val)
suite.Assert().False(isCompressed)
suite.Assert().Equal(val, after)
})
suite.Run("Compression disabled", func() {
var val []byte
for i := 0; i < 500; i++ {
val = append(val, byte(i))
}
compressor := &compressor{
CompressionEnabled: false,
CompressionMinRatio: 1.0,
CompressionMinSize: 5,
}
after, isCompressed := compressor.Compress(val)
suite.Assert().False(isCompressed)
suite.Assert().Equal(val, after)
})
suite.Run("Decompress uncompressed value", func() {
var val []byte
for i := 0; i < 500; i++ {
val = append(val, byte(i))
}
compressor := &compressor{}
decompressed, err := compressor.Decompress(&maybeCompressedResponseValue{uncompressed: val})
suite.Require().NoError(err)
suite.Assert().Equal(val, decompressed)
})
}