github.com/cloudwego/kitex@v0.9.0/pkg/remote/trans/nphttp2/grpc/http_util_test.go (about)

     1  /*
     2   * Copyright 2022 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package grpc
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/cloudwego/kitex/internal/test"
    26  )
    27  
    28  func TestEncoding(t *testing.T) {
    29  	testBytes := []byte("testbytes")
    30  	encodingStr := encodeBinHeader(testBytes)
    31  	decodingBytes, err := decodeBinHeader(encodingStr)
    32  	test.Assert(t, err == nil, err)
    33  	test.Assert(t, string(decodingBytes) == string(testBytes))
    34  
    35  	testKey, testValue := "key-bin", "value"
    36  	encodeValue := encodeMetadataHeader(testKey, testValue)
    37  	decodeValue, err := decodeMetadataHeader(testKey, encodeValue)
    38  	test.Assert(t, err == nil, err)
    39  	test.Assert(t, decodeValue == testValue)
    40  
    41  	testEncoding := "%testcoding"
    42  	encodedTestStr := encodeGrpcMessage(testEncoding)
    43  	decodedTestStr := decodeGrpcMessage(encodedTestStr)
    44  	test.Assert(t, decodedTestStr == testEncoding)
    45  }
    46  
    47  func TestEncodeTimeout(t *testing.T) {
    48  	_, err := decodeTimeout("")
    49  	test.Assert(t, err != nil, err)
    50  
    51  	_, err = decodeTimeout("#####")
    52  	test.Assert(t, err != nil, err)
    53  
    54  	_, err = decodeTimeout("1234567890123")
    55  	test.Assert(t, err != nil, err)
    56  
    57  	to := encodeTimeout(-1)
    58  	test.Assert(t, to == "0n")
    59  	d, err := decodeTimeout(to)
    60  	test.Assert(t, err == nil, err)
    61  	test.Assert(t, d == 0)
    62  
    63  	to = encodeTimeout(time.Nanosecond)
    64  	test.Assert(t, to == "1n")
    65  	d, err = decodeTimeout(to)
    66  	test.Assert(t, err == nil, err)
    67  	test.Assert(t, d == time.Nanosecond)
    68  
    69  	to = encodeTimeout(time.Microsecond)
    70  	test.Assert(t, to == "1000n")
    71  	d, err = decodeTimeout(to)
    72  	test.Assert(t, err == nil, err)
    73  	test.Assert(t, d == time.Microsecond)
    74  
    75  	to = encodeTimeout(time.Millisecond)
    76  	test.Assert(t, to == "1000000n")
    77  	d, err = decodeTimeout(to)
    78  	test.Assert(t, err == nil, err)
    79  	test.Assert(t, d == time.Millisecond)
    80  
    81  	to = encodeTimeout(time.Second)
    82  	test.Assert(t, to == "1000000u")
    83  	d, err = decodeTimeout(to)
    84  	test.Assert(t, err == nil, err)
    85  	test.Assert(t, d == time.Second)
    86  
    87  	to = encodeTimeout(time.Minute)
    88  	test.Assert(t, to == "60000000u")
    89  	d, err = decodeTimeout(to)
    90  	test.Assert(t, err == nil, err)
    91  	test.Assert(t, d == time.Minute)
    92  
    93  	to = encodeTimeout(time.Hour)
    94  	test.Assert(t, to == "3600000m")
    95  	d, err = decodeTimeout(to)
    96  	test.Assert(t, err == nil, err)
    97  	test.Assert(t, d == time.Hour)
    98  
    99  	to = encodeTimeout(time.Minute * 1000000)
   100  	test.Assert(t, to == "60000000S")
   101  	d, err = decodeTimeout(to)
   102  	test.Assert(t, err == nil, err)
   103  	test.Assert(t, d == time.Minute*1000000)
   104  
   105  	to = encodeTimeout(time.Hour * 1000000)
   106  	test.Assert(t, to == "60000000M")
   107  	d, err = decodeTimeout(to)
   108  	test.Assert(t, err == nil, err)
   109  	test.Assert(t, d == time.Hour*1000000)
   110  
   111  	to = encodeTimeout(time.Hour * 2000000)
   112  	test.Assert(t, to == "2000000H")
   113  	d, err = decodeTimeout(to)
   114  	test.Assert(t, err == nil, err)
   115  	test.Assert(t, d == time.Hour*2000000)
   116  }
   117  
   118  func TestTimeUnit(t *testing.T) {
   119  	d, ok := timeoutUnitToDuration(timeoutUnit('H'))
   120  	test.Assert(t, ok)
   121  	test.Assert(t, d == time.Hour)
   122  
   123  	d, ok = timeoutUnitToDuration(timeoutUnit('M'))
   124  	test.Assert(t, ok)
   125  	test.Assert(t, d == time.Minute)
   126  
   127  	d, ok = timeoutUnitToDuration(timeoutUnit('S'))
   128  	test.Assert(t, ok)
   129  	test.Assert(t, d == time.Second)
   130  
   131  	d, ok = timeoutUnitToDuration(timeoutUnit('m'))
   132  	test.Assert(t, ok)
   133  	test.Assert(t, d == time.Millisecond)
   134  
   135  	d, ok = timeoutUnitToDuration(timeoutUnit('u'))
   136  	test.Assert(t, ok)
   137  	test.Assert(t, d == time.Microsecond)
   138  
   139  	d, ok = timeoutUnitToDuration(timeoutUnit('n'))
   140  	test.Assert(t, ok)
   141  	test.Assert(t, d == time.Nanosecond)
   142  
   143  	_, ok = timeoutUnitToDuration(timeoutUnit('x'))
   144  	test.Assert(t, !ok)
   145  }
   146  
   147  func TestBufferWriter(t *testing.T) {
   148  	bytes := []byte("hello")
   149  	br := newBufWriter(newMockNpConn(mockAddr0), len(bytes)*5)
   150  	for i := 0; i < 6; i++ {
   151  		_, err := br.Write(bytes)
   152  		test.Assert(t, err == nil, err)
   153  	}
   154  	err := br.Flush()
   155  	test.Assert(t, err == nil, err)
   156  }
   157  
   158  func TestBufferPool(t *testing.T) {
   159  	pool := newBufferPool()
   160  	buffer := pool.get()
   161  	pool.put(buffer)
   162  }
   163  
   164  func TestRecvBufferReader(t *testing.T) {
   165  	testBytes := []byte("hello")
   166  	buffer := new(bytes.Buffer)
   167  	buffer.Reset()
   168  	buffer.Write(testBytes)
   169  	recvBuffer := newRecvBuffer()
   170  	recvBuffer.put(recvMsg{buffer: buffer})
   171  	ctx := context.Background()
   172  	rbReader := &recvBufferReader{
   173  		ctx:     ctx,
   174  		ctxDone: ctx.Done(),
   175  		recv:    recvBuffer,
   176  		closeStream: func(err error) {
   177  		},
   178  		freeBuffer: func(buffer *bytes.Buffer) {
   179  			buffer.Reset()
   180  		},
   181  	}
   182  
   183  	n, err := rbReader.Read(testBytes)
   184  	test.Assert(t, err == nil, err)
   185  	test.Assert(t, n == len(testBytes))
   186  
   187  	buffer.Write(testBytes)
   188  	recvBuffer.put(recvMsg{buffer: buffer})
   189  	rbReader.closeStream = nil
   190  	n, err = rbReader.Read(testBytes)
   191  	test.Assert(t, err == nil, err)
   192  	test.Assert(t, n == len(testBytes))
   193  }
   194  
   195  func TestConnectionError(t *testing.T) {
   196  	connectionError := ConnectionError{
   197  		Desc: "err desc",
   198  		temp: true,
   199  		err:  nil,
   200  	}
   201  
   202  	errStr := connectionError.Error()
   203  	test.Assert(t, errStr == "connection error: desc = \"err desc\"")
   204  
   205  	temp := connectionError.Temporary()
   206  	test.Assert(t, temp)
   207  
   208  	ori := connectionError.Origin()
   209  	test.Assert(t, ori == connectionError)
   210  
   211  	connectionError.err = context.Canceled
   212  	ori = connectionError.Origin()
   213  	test.Assert(t, ori == context.Canceled)
   214  }