github.com/uber-go/tally/v4@v4.1.17/m3/customtransports/buffered_read_transport_test.go (about)

     1  // Copyright (c) 2021 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 customtransport
    22  
    23  import (
    24  	"bytes"
    25  	"testing"
    26  
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  // TestTBufferedReadTransport tests the TBufferedReadTransport
    31  func TestTBufferedReadTransport(t *testing.T) {
    32  	buffer := bytes.NewBuffer([]byte("testString"))
    33  	trans, err := NewTBufferedReadTransport(buffer)
    34  	require.NotNil(t, trans)
    35  	require.NoError(t, err)
    36  	require.Equal(t, uint64(10), trans.RemainingBytes())
    37  
    38  	firstRead := make([]byte, 4)
    39  	n, err := trans.Read(firstRead)
    40  	require.NoError(t, err)
    41  	require.Equal(t, 4, n)
    42  	require.Equal(t, []byte("test"), firstRead)
    43  	require.Equal(t, uint64(6), trans.RemainingBytes())
    44  
    45  	secondRead := make([]byte, 7)
    46  	n, err = trans.Read(secondRead)
    47  	require.Equal(t, 6, n)
    48  	require.NoError(t, err)
    49  	require.Equal(t, []byte("String"), secondRead[0:6])
    50  	require.Equal(t, uint64(0), trans.RemainingBytes())
    51  }
    52  
    53  // TestTBufferedReadTransportEmptyFunctions tests the empty functions in TBufferedReadTransport
    54  func TestTBufferedReadTransportEmptyFunctions(t *testing.T) {
    55  	byteArr := make([]byte, 1)
    56  	trans, err := NewTBufferedReadTransport(bytes.NewBuffer(byteArr))
    57  	require.NotNil(t, trans)
    58  	require.NoError(t, err)
    59  
    60  	err = trans.Open()
    61  	require.NoError(t, err)
    62  
    63  	err = trans.Close()
    64  	require.NoError(t, err)
    65  
    66  	err = trans.Flush()
    67  	require.NoError(t, err)
    68  
    69  	n, err := trans.Write(byteArr)
    70  	require.Equal(t, 1, n)
    71  	require.NoError(t, err)
    72  
    73  	isOpen := trans.IsOpen()
    74  	require.True(t, isOpen)
    75  }