github.com/uber-go/tally/v4@v4.1.17/m3/customtransports/m3_calc_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  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  	"github.com/uber-go/tally/v4/thirdparty/github.com/apache/thrift/lib/go/thrift"
    28  )
    29  
    30  // Make sure that TCalcTransport implements TRichTransport.
    31  // We use TRichTransport instead of TTransport to avoid unnecessary allocations
    32  // when writing string fields. See tests in
    33  var _ thrift.TRichTransport = (*TCalcTransport)(nil)
    34  
    35  func TestTCalcTransport(t *testing.T) {
    36  	trans := &TCalcTransport{}
    37  	require.Nil(t, trans.Open())
    38  	require.True(t, trans.IsOpen())
    39  	require.EqualValues(t, 0, trans.GetCount())
    40  
    41  	testString1 := "test"
    42  	testString2 := "string"
    43  	n, err := trans.Write([]byte(testString1))
    44  	require.Equal(t, len(testString1), n)
    45  	require.NoError(t, err)
    46  	require.EqualValues(t, len(testString1), trans.GetCount())
    47  	n, err = trans.Write([]byte(testString2))
    48  	require.EqualValues(t, len(testString2), n)
    49  	require.NoError(t, err)
    50  	require.EqualValues(t, len(testString1)+len(testString2), trans.GetCount())
    51  
    52  	trans.ResetCount()
    53  	n, err = trans.WriteString(testString1)
    54  	require.EqualValues(t, len(testString1), n)
    55  	require.NoError(t, err)
    56  	require.EqualValues(t, len(testString1), trans.GetCount())
    57  
    58  	err = trans.WriteByte('a')
    59  	require.NoError(t, err)
    60  	require.EqualValues(t, len(testString1)+1, trans.GetCount())
    61  
    62  	n, err = trans.Read([]byte(testString1))
    63  	require.NoError(t, err)
    64  	require.EqualValues(t, 0, n)
    65  
    66  	b, err := trans.ReadByte()
    67  	require.NoError(t, err)
    68  	require.Equal(t, byte(0), b)
    69  
    70  	require.Equal(t, ^uint64(0), trans.RemainingBytes())
    71  
    72  	trans.ResetCount()
    73  	require.EqualValues(t, 0, trans.GetCount())
    74  
    75  	err = trans.Flush()
    76  	require.NoError(t, err)
    77  	require.Nil(t, trans.Close())
    78  }