github.com/cloudwego/kitex@v0.9.0/pkg/transmeta/ttheader_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 transmeta
    18  
    19  import (
    20  	"context"
    21  	"strconv"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/cloudwego/kitex/internal/mocks"
    26  	"github.com/cloudwego/kitex/internal/test"
    27  	"github.com/cloudwego/kitex/pkg/remote"
    28  	"github.com/cloudwego/kitex/pkg/remote/transmeta"
    29  	"github.com/cloudwego/kitex/pkg/rpcinfo"
    30  	"github.com/cloudwego/kitex/pkg/serviceinfo"
    31  	"github.com/cloudwego/kitex/transport"
    32  )
    33  
    34  func TestIsTTHeader(t *testing.T) {
    35  	t.Run("with ttheader", func(t *testing.T) {
    36  		ri := rpcinfo.NewRPCInfo(nil, nil, rpcinfo.NewInvocation("", ""), nil, rpcinfo.NewRPCStats())
    37  		msg := remote.NewMessage(nil, mocks.ServiceInfo(), ri, remote.Call, remote.Server)
    38  		msg.SetProtocolInfo(remote.NewProtocolInfo(transport.TTHeader, serviceinfo.Thrift))
    39  		test.Assert(t, isTTHeader(msg))
    40  	})
    41  	t.Run("with ttheader framed", func(t *testing.T) {
    42  		ri := rpcinfo.NewRPCInfo(nil, nil, rpcinfo.NewInvocation("", ""), nil, rpcinfo.NewRPCStats())
    43  		msg := remote.NewMessage(nil, mocks.ServiceInfo(), ri, remote.Call, remote.Server)
    44  		msg.SetProtocolInfo(remote.NewProtocolInfo(transport.TTHeaderFramed, serviceinfo.Thrift))
    45  		test.Assert(t, isTTHeader(msg))
    46  	})
    47  }
    48  
    49  func TestTTHeaderClientWriteMetainfo(t *testing.T) {
    50  	ctx := context.Background()
    51  
    52  	cfg := rpcinfo.NewRPCConfig()
    53  	cfgMutable := rpcinfo.AsMutableRPCConfig(cfg)
    54  	cfgMutable.SetRPCTimeout(time.Millisecond * 100)
    55  	cfgMutable.LockConfig(rpcinfo.BitRPCTimeout)
    56  
    57  	fromInfo := rpcinfo.NewEndpointInfo("fromServiceName", "fromMethod", nil, nil)
    58  	toInfo := rpcinfo.NewEndpointInfo("toServiceName", "toMethod", nil, nil)
    59  	ri := rpcinfo.NewRPCInfo(fromInfo, toInfo, rpcinfo.NewInvocation("", ""), cfg, rpcinfo.NewRPCStats())
    60  	msg := remote.NewMessage(nil, mocks.ServiceInfo(), ri, remote.Call, remote.Client)
    61  
    62  	// pure paylod, no effect
    63  	msg.SetProtocolInfo(remote.NewProtocolInfo(transport.PurePayload, serviceinfo.Thrift))
    64  	ctx, err := ClientTTHeaderHandler.WriteMeta(ctx, msg)
    65  	kvs := msg.TransInfo().TransIntInfo()
    66  	test.Assert(t, err == nil)
    67  	test.Assert(t, len(kvs) == 0)
    68  	strKvs := msg.TransInfo().TransStrInfo()
    69  	test.Assert(t, len(strKvs) == 0)
    70  
    71  	// ttheader
    72  	msg.SetProtocolInfo(remote.NewProtocolInfo(transport.TTHeader, serviceinfo.Thrift))
    73  	_, err = ClientTTHeaderHandler.WriteMeta(ctx, msg)
    74  	test.Assert(t, err == nil)
    75  	kvs = msg.TransInfo().TransIntInfo()
    76  	test.Assert(t, len(kvs) > 0)
    77  	test.Assert(t, kvs[transmeta.FromService] == fromInfo.ServiceName())
    78  	test.Assert(t, kvs[transmeta.FromMethod] == fromInfo.Method())
    79  	test.Assert(t, kvs[transmeta.ToService] == toInfo.ServiceName())
    80  	test.Assert(t, kvs[transmeta.ToMethod] == toInfo.Method())
    81  	test.Assert(t, kvs[transmeta.MsgType] == strconv.Itoa(int(remote.Call)))
    82  	test.Assert(t, kvs[transmeta.TransportType] == unframedTransportType)
    83  	test.Assert(t, kvs[transmeta.RPCTimeout] == "100")
    84  	strKvs = msg.TransInfo().TransStrInfo()
    85  	test.Assert(t, len(strKvs) == 1)
    86  	test.Assert(t, strKvs[transmeta.HeaderIDLServiceName] == "")
    87  }
    88  
    89  func TestTTHeaderServerReadMetainfo(t *testing.T) {
    90  	ctx := context.Background()
    91  	ri := rpcinfo.NewRPCInfo(rpcinfo.EmptyEndpointInfo(), nil, rpcinfo.NewInvocation("", ""),
    92  		rpcinfo.NewRPCConfig(), rpcinfo.NewRPCStats())
    93  	msg := remote.NewMessage(nil, mocks.ServiceInfo(), ri, remote.Call, remote.Client)
    94  
    95  	hd := map[uint16]string{
    96  		transmeta.FromService: "fromService",
    97  		transmeta.FromMethod:  "fromMethod",
    98  		transmeta.RPCTimeout:  "100",
    99  	}
   100  	msg.TransInfo().PutTransIntInfo(hd)
   101  
   102  	msg.SetProtocolInfo(remote.NewProtocolInfo(transport.PurePayload, serviceinfo.Thrift))
   103  	_, err := ServerTTHeaderHandler.ReadMeta(ctx, msg)
   104  	test.Assert(t, err == nil)
   105  	fromEndPoint := msg.RPCInfo().From()
   106  	test.Assert(t, fromEndPoint.ServiceName() == "")
   107  	test.Assert(t, fromEndPoint.Method() == "")
   108  
   109  	msg.SetProtocolInfo(remote.NewProtocolInfo(transport.TTHeader, serviceinfo.Thrift))
   110  	_, err = ServerTTHeaderHandler.ReadMeta(ctx, msg)
   111  	test.Assert(t, err == nil)
   112  	test.Assert(t, msg.RPCInfo().From().ServiceName() == hd[transmeta.FromService])
   113  	test.Assert(t, msg.RPCInfo().From().Method() == hd[transmeta.FromMethod])
   114  	test.Assert(t, ri.Config().RPCTimeout() == 100*time.Millisecond)
   115  }
   116  
   117  func TestTTHeaderServerWriteMetainfo(t *testing.T) {
   118  	ctx := context.Background()
   119  	ri := rpcinfo.NewRPCInfo(nil, nil, rpcinfo.NewInvocation("", ""),
   120  		rpcinfo.NewRPCConfig(), rpcinfo.NewRPCStats())
   121  	msg := remote.NewMessage(nil, mocks.ServiceInfo(), ri, remote.Call, remote.Client)
   122  
   123  	msg.SetProtocolInfo(remote.NewProtocolInfo(transport.PurePayload, serviceinfo.Thrift))
   124  	_, err := ServerTTHeaderHandler.WriteMeta(ctx, msg)
   125  	test.Assert(t, err == nil)
   126  	kvs := msg.TransInfo().TransIntInfo()
   127  	test.Assert(t, len(kvs) == 0)
   128  
   129  	msg.SetProtocolInfo(remote.NewProtocolInfo(transport.TTHeader, serviceinfo.Thrift))
   130  	_, err = ServerTTHeaderHandler.WriteMeta(ctx, msg)
   131  	test.Assert(t, err == nil)
   132  	kvs = msg.TransInfo().TransIntInfo()
   133  	test.Assert(t, kvs[transmeta.MsgType] == strconv.Itoa(int(remote.Call)))
   134  }