go.uber.org/yarpc@v1.72.1/compressor/snappy/snappy_test.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package yarpcsnappy_test
    22  
    23  import (
    24  	"bytes"
    25  	"io/ioutil"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  	"go.uber.org/yarpc/compressor/snappy"
    31  )
    32  
    33  // This should be compressible:
    34  var quote = "Now is the time for all good men to come to the aid of their country"
    35  var input = []byte(quote + quote + quote)
    36  
    37  func TestSnappy(t *testing.T) {
    38  	buf := bytes.NewBuffer(nil)
    39  	writer, err := yarpcsnappy.New().Compress(buf)
    40  	require.NoError(t, err)
    41  
    42  	_, err = writer.Write(input)
    43  	require.NoError(t, err)
    44  	require.NoError(t, writer.Close())
    45  
    46  	str, err := yarpcsnappy.New().Decompress(buf)
    47  	require.NoError(t, err)
    48  
    49  	output, err := ioutil.ReadAll(str)
    50  	require.NoError(t, err)
    51  	require.NoError(t, str.Close())
    52  
    53  	assert.Equal(t, input, output)
    54  }
    55  
    56  func TestCompressionPooling(t *testing.T) {
    57  	compressor := yarpcsnappy.New()
    58  	for i := 0; i < 128; i++ {
    59  		buf := bytes.NewBuffer(nil)
    60  		writer, err := compressor.Compress(buf)
    61  		require.NoError(t, err)
    62  
    63  		_, err = writer.Write(input)
    64  		require.NoError(t, err)
    65  		require.NoError(t, writer.Close())
    66  
    67  		str, err := compressor.Decompress(buf)
    68  		require.NoError(t, err)
    69  
    70  		output, err := ioutil.ReadAll(str)
    71  		require.NoError(t, err)
    72  		require.NoError(t, str.Close())
    73  
    74  		assert.Equal(t, input, output)
    75  	}
    76  }
    77  
    78  func TestSnappyName(t *testing.T) {
    79  	assert.Equal(t, "snappy", yarpcsnappy.New().Name())
    80  }
    81  
    82  func TestSnappyCompression(t *testing.T) {
    83  	compressor := yarpcsnappy.New()
    84  
    85  	buf := bytes.NewBuffer(nil)
    86  	writer, err := compressor.Compress(buf)
    87  	require.NoError(t, err)
    88  
    89  	_, err = writer.Write(input)
    90  	require.NoError(t, err)
    91  	require.NoError(t, writer.Close())
    92  
    93  	// Sanity check
    94  	assert.True(t, buf.Len() < len(input), "one would think the compressed data would be smaller")
    95  	t.Logf("decompress: %d\n", len(input))
    96  	t.Logf("compressed: %d\n", buf.Len())
    97  }