trpc.group/trpc-go/trpc-go@v1.0.3/codec/framer_builder_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package codec
    15  
    16  import (
    17  	"bufio"
    18  	"strings"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  func TestReaderSize(t *testing.T) {
    25  	assert.Equal(t, DefaultReaderSize, GetReaderSize())
    26  	defer SetReaderSize(DefaultReaderSize)
    27  
    28  	bufSize := 128 * 1024
    29  	SetReaderSize(bufSize)
    30  	assert.Equal(t, bufSize, GetReaderSize())
    31  	SetReaderSize(0)
    32  	assert.Equal(t, 0, GetReaderSize())
    33  }
    34  
    35  func TestNewReaderSize(t *testing.T) {
    36  	orig := strings.NewReader("test")
    37  	newer := NewReaderSize(orig, -1)
    38  	assert.Equal(t, orig, newer)
    39  
    40  	newer = NewReaderSize(orig, 0)
    41  	assert.Equal(t, orig, newer)
    42  
    43  	newer = NewReaderSize(orig, 32*1024)
    44  	assert.NotEqual(t, orig, newer)
    45  	bufioReader, ok := newer.(*bufio.Reader)
    46  	assert.Equal(t, true, ok)
    47  	assert.Equal(t, 32*1024, bufioReader.Size())
    48  }
    49  
    50  func TestNewReader(t *testing.T) {
    51  	orig := strings.NewReader("test")
    52  	newer := NewReader(orig)
    53  	assert.NotEqual(t, orig, newer)
    54  	bufioReader, ok := newer.(*bufio.Reader)
    55  	assert.Equal(t, true, ok)
    56  	assert.Equal(t, DefaultReaderSize, bufioReader.Size())
    57  }
    58  
    59  type fakeSafeFramer struct {
    60  	safe bool
    61  }
    62  
    63  func (f *fakeSafeFramer) ReadFrame() ([]byte, error) {
    64  	return nil, nil
    65  }
    66  
    67  func (f *fakeSafeFramer) IsSafe() bool {
    68  	return f.safe
    69  }
    70  
    71  func TestIsSafeFramer(t *testing.T) {
    72  	safeFrame := fakeSafeFramer{safe: true}
    73  	assert.Equal(t, true, IsSafeFramer(&safeFrame))
    74  
    75  	noSafeFrame := fakeSafeFramer{}
    76  	assert.Equal(t, false, IsSafeFramer(&noSafeFrame))
    77  
    78  	assert.Equal(t, false, IsSafeFramer(10))
    79  }