go.uber.org/yarpc@v1.72.1/compressor/gzip/gzip_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 yarpcgzip_test
    22  
    23  import (
    24  	"bytes"
    25  	"compress/gzip"
    26  	"io/ioutil"
    27  	"strconv"
    28  	"testing"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  	"github.com/stretchr/testify/require"
    32  	"go.uber.org/yarpc/compressor/gzip"
    33  )
    34  
    35  // This should be compressible:
    36  var quote = "Now is the time for all good men to come to the aid of their country"
    37  var input = []byte(quote + quote + quote)
    38  
    39  func TestGzip(t *testing.T) {
    40  	buf := bytes.NewBuffer(nil)
    41  	writer, err := yarpcgzip.New().Compress(buf)
    42  	require.NoError(t, err)
    43  
    44  	_, err = writer.Write(input)
    45  	require.NoError(t, err)
    46  	require.NoError(t, writer.Close())
    47  
    48  	str, err := yarpcgzip.New().Decompress(buf)
    49  	require.NoError(t, err)
    50  
    51  	output, err := ioutil.ReadAll(str)
    52  	require.NoError(t, err)
    53  	require.NoError(t, str.Close())
    54  
    55  	assert.Equal(t, input, output)
    56  }
    57  
    58  func TestCompressionPooling(t *testing.T) {
    59  	compressor := yarpcgzip.New()
    60  	for i := 0; i < 128; i++ {
    61  		buf := bytes.NewBuffer(nil)
    62  		writer, err := compressor.Compress(buf)
    63  		require.NoError(t, err)
    64  
    65  		_, err = writer.Write(input)
    66  		require.NoError(t, err)
    67  		require.NoError(t, writer.Close())
    68  
    69  		str, err := compressor.Decompress(buf)
    70  		require.NoError(t, err)
    71  
    72  		output, err := ioutil.ReadAll(str)
    73  		require.NoError(t, err)
    74  		require.NoError(t, str.Close())
    75  
    76  		assert.Equal(t, input, output)
    77  	}
    78  }
    79  
    80  func TestEveryCompressionLevel(t *testing.T) {
    81  	levels := []int{
    82  		gzip.NoCompression,
    83  		gzip.BestSpeed,
    84  		gzip.BestCompression,
    85  		gzip.DefaultCompression,
    86  		gzip.HuffmanOnly,
    87  	}
    88  
    89  	for _, level := range levels {
    90  		t.Run(strconv.Itoa(level), func(t *testing.T) {
    91  			compressor := yarpcgzip.New(yarpcgzip.Level(level))
    92  
    93  			buf := bytes.NewBuffer(nil)
    94  			writer, err := compressor.Compress(buf)
    95  			require.NoError(t, err)
    96  
    97  			_, err = writer.Write(input)
    98  			require.NoError(t, err)
    99  			require.NoError(t, writer.Close())
   100  
   101  			str, err := compressor.Decompress(buf)
   102  			require.NoError(t, err)
   103  
   104  			output, err := ioutil.ReadAll(str)
   105  			require.NoError(t, err)
   106  			require.NoError(t, str.Close())
   107  
   108  			assert.Equal(t, input, output)
   109  		})
   110  	}
   111  }
   112  
   113  func TestGzipName(t *testing.T) {
   114  	assert.Equal(t, "gzip", yarpcgzip.New().Name())
   115  }
   116  
   117  func TestDecompressedSize(t *testing.T) {
   118  	compressor := yarpcgzip.New(yarpcgzip.Level(gzip.BestCompression))
   119  
   120  	buf := bytes.NewBuffer(nil)
   121  	writer, err := compressor.Compress(buf)
   122  	require.NoError(t, err)
   123  
   124  	_, err = writer.Write(input)
   125  	require.NoError(t, err)
   126  	require.NoError(t, writer.Close())
   127  
   128  	assert.Equal(t, len(input), compressor.DecompressedSize(buf.Bytes()))
   129  
   130  	// Sanity check
   131  	assert.True(t, buf.Len() < len(input), "one would think the compressed data would be smaller")
   132  	t.Logf("decompress: %d\n", len(input))
   133  	t.Logf("compressed: %d\n", buf.Len())
   134  }
   135  
   136  func TestDecompressedSizeError(t *testing.T) {
   137  	compressor := yarpcgzip.New(yarpcgzip.Level(gzip.BestCompression))
   138  	assert.Equal(t, -1, compressor.DecompressedSize([]byte{}))
   139  }