go.uber.org/yarpc@v1.72.1/transport/tchannel/internal/reader_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 internal
    22  
    23  import (
    24  	"bytes"
    25  	"io"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/uber/tchannel-go/testutils/testreader"
    30  )
    31  
    32  // Also lifted from tchannel/typed
    33  
    34  func nString(n int) []byte {
    35  	buf := make([]byte, n)
    36  	reader := testreader.Looper([]byte{'a', 'b', 'c', 'd', 'e'})
    37  	io.ReadFull(reader, buf)
    38  	return buf
    39  }
    40  
    41  func TestReader(t *testing.T) {
    42  	s1 := nString(10)
    43  	s2 := nString(800)
    44  
    45  	var buf []byte
    46  	buf = append(buf, 0, 1)       // uint16, 1
    47  	buf = append(buf, 0xff, 0xff) // uint16, 65535
    48  	buf = append(buf, 0, 10)      // uint16, 10
    49  	buf = append(buf, s1...)      // string, 10 bytes
    50  	buf = append(buf, 3, 32)      // uint16, 800
    51  	buf = append(buf, s2...)      // string, 800 bytes
    52  	buf = append(buf, 0, 10)      // uint16, 10
    53  
    54  	reader := NewReader(bytes.NewReader(buf))
    55  
    56  	assert.Equal(t, uint16(1), reader.ReadUint16())
    57  	assert.Equal(t, uint16(65535), reader.ReadUint16())
    58  	assert.Equal(t, string(s1), reader.ReadLen16String())
    59  	assert.Equal(t, string(s2), reader.ReadLen16String())
    60  	assert.Equal(t, uint16(10), reader.ReadUint16())
    61  }
    62  
    63  func TestReaderErr(t *testing.T) {
    64  	tests := []struct {
    65  		chunks     [][]byte
    66  		validation func(reader *Reader)
    67  	}{
    68  		{
    69  			chunks: [][]byte{
    70  				{0, 1},
    71  				nil,
    72  				{2, 3},
    73  			},
    74  			validation: func(reader *Reader) {
    75  				assert.Equal(t, uint16(1), reader.ReadUint16(), "Read unexpected value")
    76  				assert.Equal(t, uint16(0), reader.ReadUint16(), "Expected default value")
    77  			},
    78  		},
    79  		{
    80  			chunks: [][]byte{
    81  				{0, 4},
    82  				[]byte("test"),
    83  				nil,
    84  				{'A', 'b'},
    85  			},
    86  			validation: func(reader *Reader) {
    87  				assert.Equal(t, "test", reader.ReadLen16String(), "Read unexpected value")
    88  				assert.Equal(t, "", reader.ReadString(2), "Expected default value")
    89  			},
    90  		},
    91  	}
    92  
    93  	for _, tt := range tests {
    94  		writer, chunkReader := testreader.ChunkReader()
    95  		reader := NewReader(chunkReader)
    96  		defer reader.Release()
    97  
    98  		for _, chunk := range tt.chunks {
    99  			writer <- chunk
   100  		}
   101  		close(writer)
   102  
   103  		tt.validation(reader)
   104  		// Once there's an error, all further calls should fail.
   105  		assert.Equal(t, testreader.ErrUser, reader.Err(), "Unexpected error")
   106  		assert.Equal(t, uint16(0), reader.ReadUint16(), "Expected default value")
   107  		assert.Equal(t, "", reader.ReadString(1), "Expected default value")
   108  		assert.Equal(t, "", reader.ReadLen16String(), "Expected default value")
   109  		assert.Equal(t, testreader.ErrUser, reader.Err(), "Unexpected error")
   110  	}
   111  }