github.com/cloudwego/kitex@v0.9.0/pkg/remote/trans/nphttp2/client_conn_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 nphttp2
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/cloudwego/kitex/internal/test"
    23  	"github.com/cloudwego/kitex/pkg/rpcinfo"
    24  )
    25  
    26  func TestClientConn(t *testing.T) {
    27  	// init
    28  	connPool := newMockConnPool()
    29  	ctx := newMockCtxWithRPCInfo()
    30  	conn, err := connPool.Get(ctx, "tcp", mockAddr0, newMockConnOption())
    31  	test.Assert(t, err == nil, err)
    32  	defer conn.Close()
    33  	clientConn := conn.(*clientConn)
    34  
    35  	// test LocalAddr()
    36  	la := clientConn.LocalAddr()
    37  	test.Assert(t, la == nil)
    38  
    39  	// test RemoteAddr()
    40  	ra := clientConn.RemoteAddr()
    41  	test.Assert(t, ra.String() == mockAddr0)
    42  
    43  	// test Read()
    44  	testStr := "1234567890"
    45  	mockStreamRecv(clientConn.s, testStr)
    46  	testByte := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    47  	n, err := clientConn.Read(testByte)
    48  	test.Assert(t, err == nil, err)
    49  	test.Assert(t, n == len(testStr))
    50  	test.Assert(t, string(testByte) == testStr)
    51  
    52  	// test write()
    53  	testByte = []byte(testStr)
    54  	n, err = clientConn.Write(testByte)
    55  	test.Assert(t, err == nil, err)
    56  	test.Assert(t, n == len(testByte))
    57  
    58  	// test write() short write error
    59  	n, err = clientConn.Write([]byte(""))
    60  	test.Assert(t, err != nil, err)
    61  	test.Assert(t, n == 0)
    62  }
    63  
    64  func TestClientConnStreamDesc(t *testing.T) {
    65  	connPool := newMockConnPool()
    66  
    67  	// streaming
    68  	ctx := newMockCtxWithRPCInfo()
    69  	ri := rpcinfo.GetRPCInfo(ctx)
    70  	test.Assert(t, ri != nil)
    71  	rpcinfo.AsMutableRPCConfig(ri.Config()).SetInteractionMode(rpcinfo.Streaming)
    72  	conn, err := connPool.Get(ctx, "tcp", mockAddr0, newMockConnOption())
    73  	test.Assert(t, err == nil, err)
    74  	defer conn.Close()
    75  	cn := conn.(*clientConn)
    76  	test.Assert(t, cn != nil)
    77  	test.Assert(t, cn.desc.isStreaming == true)
    78  
    79  	// pingpong
    80  	rpcinfo.AsMutableRPCConfig(ri.Config()).SetInteractionMode(rpcinfo.PingPong)
    81  	conn, err = connPool.Get(ctx, "tcp", mockAddr0, newMockConnOption())
    82  	test.Assert(t, err == nil, err)
    83  	defer conn.Close()
    84  	cn = conn.(*clientConn)
    85  	test.Assert(t, cn != nil)
    86  	test.Assert(t, cn.desc.isStreaming == false)
    87  }
    88  
    89  func Test_fullMethodName(t *testing.T) {
    90  	t.Run("empty pkg", func(t *testing.T) {
    91  		got := fullMethodName("", "svc", "method")
    92  		test.Assert(t, got == "/svc/method")
    93  	})
    94  	t.Run("with pkg", func(t *testing.T) {
    95  		got := fullMethodName("pkg", "svc", "method")
    96  		test.Assert(t, got == "/pkg.svc/method")
    97  	})
    98  }