github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/compressor/gzipCompressor_test.go (about)

     1  package compressor_test
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/onflow/flow-go/network/compressor"
    11  )
    12  
    13  // TestRoundTrip evaluates that (1) reading what has been written by compressor yields in same result,
    14  // and (2) data is compressed when written.
    15  func TestRoundTrip(t *testing.T) {
    16  	text := "hello world, hello world!"
    17  	textBytes := []byte(text)
    18  	textBytesLen := len(textBytes)
    19  	buf := new(bytes.Buffer)
    20  
    21  	gzipComp := compressor.GzipStreamCompressor{}
    22  
    23  	w, err := gzipComp.NewWriter(buf)
    24  	require.NoError(t, err)
    25  
    26  	// testing write
    27  	//
    28  	n, err := w.Write(textBytes)
    29  	require.NoError(t, err)
    30  	// written bytes should match original data
    31  	require.Equal(t, n, textBytesLen)
    32  	// written data on buffer should be compressed in size.
    33  	require.Less(t, buf.Len(), textBytesLen)
    34  	require.NoError(t, w.Close())
    35  
    36  	// testing read
    37  	//
    38  	r, err := gzipComp.NewReader(buf)
    39  	require.NoError(t, err)
    40  
    41  	b := make([]byte, textBytesLen)
    42  	n, err = r.Read(b)
    43  	// we read the entire buffer on reader, so it should return an EOF at the end
    44  	require.ErrorIs(t, err, io.EOF)
    45  	// we should read same number of bytes as we've written
    46  	require.Equal(t, n, textBytesLen)
    47  	// we should read what we have written
    48  	require.Equal(t, b, textBytes)
    49  }