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

     1  /*
     2   * Copyright 2021 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 trans
    18  
    19  import (
    20  	"context"
    21  	"net"
    22  	"testing"
    23  
    24  	"github.com/cloudwego/kitex/internal/mocks"
    25  	"github.com/cloudwego/kitex/internal/test"
    26  	"github.com/cloudwego/kitex/pkg/remote"
    27  	"github.com/cloudwego/kitex/pkg/remote/connpool"
    28  	"github.com/cloudwego/kitex/pkg/rpcinfo"
    29  )
    30  
    31  func TestDefaultCliTransHandler(t *testing.T) {
    32  	buf := remote.NewReaderWriterBuffer(1024)
    33  	ext := &MockExtension{
    34  		NewWriteByteBufferFunc: func(ctx context.Context, conn net.Conn, msg remote.Message) remote.ByteBuffer {
    35  			return buf
    36  		},
    37  		NewReadByteBufferFunc: func(ctx context.Context, conn net.Conn, msg remote.Message) remote.ByteBuffer {
    38  			return buf
    39  		},
    40  	}
    41  
    42  	tagEncode, tagDecode := 0, 0
    43  	opt := &remote.ClientOption{
    44  		Codec: &MockCodec{
    45  			EncodeFunc: func(ctx context.Context, msg remote.Message, out remote.ByteBuffer) error {
    46  				tagEncode++
    47  				test.Assert(t, out == buf)
    48  				return nil
    49  			},
    50  			DecodeFunc: func(ctx context.Context, msg remote.Message, in remote.ByteBuffer) error {
    51  				tagDecode++
    52  				test.Assert(t, in == buf)
    53  				return nil
    54  			},
    55  		},
    56  		ConnPool: connpool.NewShortPool("opt"),
    57  	}
    58  
    59  	handler, err := NewDefaultCliTransHandler(opt, ext)
    60  	test.Assert(t, err == nil)
    61  
    62  	ctx := context.Background()
    63  	conn := &mocks.Conn{}
    64  	msg := &MockMessage{
    65  		RPCInfoFunc: func() rpcinfo.RPCInfo {
    66  			return newMockRPCInfo()
    67  		},
    68  	}
    69  	ctx, err = handler.Write(ctx, conn, msg)
    70  	test.Assert(t, ctx != nil, ctx)
    71  	test.Assert(t, err == nil, err)
    72  	test.Assert(t, tagEncode == 1, tagEncode)
    73  	test.Assert(t, tagDecode == 0, tagDecode)
    74  
    75  	ctx, err = handler.Read(ctx, conn, msg)
    76  	test.Assert(t, ctx != nil, ctx)
    77  	test.Assert(t, err == nil, err)
    78  	test.Assert(t, tagEncode == 1, tagEncode)
    79  	test.Assert(t, tagDecode == 1, tagDecode)
    80  }