MCPcopy Index your code
hub / github.com/cortexproject/cortex / testCompress

Function testCompress

pkg/util/grpcencoding/encoding_test.go:40–85  ·  view source on GitHub ↗
(name string, t *testing.T)

Source from the content-addressed store, hash-verified

38}
39
40func testCompress(name string, t *testing.T) {
41 c := encoding.GetCompressor(name)
42 assert.Equal(t, name, c.Name())
43
44 tests := []struct {
45 test string
46 input string
47 }{
48 {"empty", ""},
49 {"short", "hello world"},
50 {"long", strings.Repeat("123456789", 2024)},
51 }
52 for _, test := range tests {
53 t.Run(test.test, func(t *testing.T) {
54 buf := &bytes.Buffer{}
55 // Compress
56 w, err := c.Compress(buf)
57 require.NoError(t, err)
58 n, err := w.Write([]byte(test.input))
59 require.NoError(t, err)
60 assert.Len(t, test.input, n)
61 err = w.Close()
62 require.NoError(t, err)
63 compressedBytes := buf.Bytes()
64 buf = bytes.NewBuffer(compressedBytes)
65
66 // Decompress
67 r, err := c.Decompress(buf)
68 require.NoError(t, err)
69 out, err := io.ReadAll(r)
70 require.NoError(t, err)
71 assert.Equal(t, test.input, string(out))
72
73 if sizer, ok := c.(interface {
74 DecompressedSize(compressedBytes []byte) int
75 }); ok {
76 buf = bytes.NewBuffer(compressedBytes)
77 r, err := c.Decompress(buf)
78 require.NoError(t, err)
79 out, err := io.ReadAll(r)
80 require.NoError(t, err)
81 assert.Equal(t, len(out), sizer.DecompressedSize(compressedBytes))
82 }
83 })
84 }
85}
86
87func BenchmarkCompress(b *testing.B) {
88 data := []byte(strings.Repeat("123456789", 1024))

Callers 1

TestCompressorsFunction · 0.85

Calls 10

BytesMethod · 0.95
DecompressedSizeMethod · 0.80
EqualMethod · 0.65
NameMethod · 0.65
RunMethod · 0.65
CloseMethod · 0.65
CompressMethod · 0.45
WriteMethod · 0.45
LenMethod · 0.45
DecompressMethod · 0.45

Tested by

no test coverage detected