github.com/cloudwego/kitex@v0.9.0/pkg/rpcinfo/convert_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 rpcinfo_test
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/cloudwego/kitex/internal/test"
    26  	"github.com/cloudwego/kitex/pkg/kerrors"
    27  	"github.com/cloudwego/kitex/pkg/rpcinfo"
    28  	"github.com/cloudwego/kitex/pkg/stats"
    29  	"github.com/cloudwego/kitex/pkg/utils"
    30  	"github.com/cloudwego/kitex/transport"
    31  )
    32  
    33  func TestAsTaggable(t *testing.T) {
    34  	test.Assert(t, rpcinfo.AsTaggable(nil) == nil)
    35  	test.Assert(t, rpcinfo.AsTaggable(0) == nil)
    36  	test.Assert(t, rpcinfo.AsTaggable(0.1) == nil)
    37  	test.Assert(t, rpcinfo.AsTaggable(rpcinfo.NewEndpointInfo("s", "m", nil, nil)) != nil)
    38  	test.Assert(t, rpcinfo.AsTaggable(rpcinfo.NewMutableEndpointInfo("s", "m", nil, nil)) != nil)
    39  }
    40  
    41  func TestAsMutableEndpointInfo(t *testing.T) {
    42  	test.Assert(t, rpcinfo.AsMutableEndpointInfo(rpcinfo.EndpointInfo(nil)) == nil)
    43  
    44  	na := utils.NewNetAddr("nnn", "aaa")
    45  	ts := map[string]string{"a": "b", "1": "2"}
    46  	ei := rpcinfo.NewEndpointInfo("s", "m", na, ts)
    47  
    48  	mi := rpcinfo.AsMutableEndpointInfo(ei)
    49  	test.Assert(t, mi != nil)
    50  
    51  	mi.SetServiceName("ss")
    52  	mi.SetMethod("mm")
    53  	mi.SetAddress(utils.NewNetAddr("n2", "a2"))
    54  	mi.SetTag("a", "aa")
    55  	mi.SetTag("x", "y")
    56  
    57  	test.Assert(t, ei.ServiceName() == "ss")
    58  	test.Assert(t, ei.Method() == "mm")
    59  	test.Assert(t, ei.Address().Network() == "n2")
    60  	test.Assert(t, ei.Address().String() == "a2")
    61  	test.Assert(t, ei.DefaultTag("a", "-") == "aa")
    62  	test.Assert(t, ei.DefaultTag("1", "-") == "2")
    63  	test.Assert(t, ei.DefaultTag("x", "-") == "y")
    64  
    65  	tag, ok := ei.Tag("a")
    66  	test.Assert(t, ok && tag == "aa")
    67  	tag, ok = ei.Tag("1")
    68  	test.Assert(t, ok && tag == "2")
    69  	tag, ok = ei.Tag("x")
    70  	test.Assert(t, ok && tag == "y")
    71  
    72  	ei = rpcinfo.NewEndpointInfo("s2", "m2", nil, nil)
    73  	mi = rpcinfo.AsMutableEndpointInfo(ei)
    74  	test.Assert(t, ei.Address() == nil)
    75  	test.Assert(t, ei.DefaultTag("x", "-") == "-")
    76  	mi.SetTag("x", "y")
    77  	test.Assert(t, ei.DefaultTag("x", "-") == "y")
    78  }
    79  
    80  func TestAsMutableRPCStats(t *testing.T) {
    81  	test.Assert(t, rpcinfo.AsMutableRPCStats(rpcinfo.RPCStats(nil)) == nil)
    82  	test.Assert(t, rpcinfo.AsMutableRPCStats(new(MockRPCStats)) == nil)
    83  
    84  	st := rpcinfo.NewRPCStats()
    85  	ms := rpcinfo.AsMutableRPCStats(st)
    86  	test.Assert(t, ms != nil)
    87  	test.Assert(t, ms.ImmutableView() != nil)
    88  
    89  	ms.SetSendSize(123)
    90  	ms.SetRecvSize(456)
    91  	test.Assert(t, st.SendSize() == 123 && st.RecvSize() == 456)
    92  
    93  	ms.SetError(errors.New("hello"))
    94  	test.Assert(t, st.Error() != nil && st.Error().Error() == "hello")
    95  
    96  	ms.SetPanicked("don't panic")
    97  	ok, what := st.Panicked()
    98  	test.Assert(t, ok && what.(string) == "don't panic")
    99  
   100  	ms.SetLevel(stats.LevelDetailed)
   101  	st.Record(context.Background(), stats.RPCStart, stats.StatusInfo, "informatic")
   102  	e := st.GetEvent(stats.RPCStart)
   103  	test.Assert(t, e != nil && !e.IsNil())
   104  	test.Assert(t, e.Event() == stats.RPCStart && e.Status() == stats.StatusInfo && e.Info() == "informatic")
   105  
   106  	ms.Reset()
   107  	test.Assert(t, st.Level() == stats.LevelDisabled)
   108  	test.Assert(t, st.SendSize() == 0 && st.RecvSize() == 0)
   109  	test.Assert(t, st.Error() == nil)
   110  	ok, what = st.Panicked()
   111  	test.Assert(t, !ok && what == nil)
   112  	test.Assert(t, st.GetEvent(stats.RPCStart) == nil)
   113  }
   114  
   115  func TestAsMutableRPCConfig(t *testing.T) {
   116  	test.Assert(t, rpcinfo.AsMutableRPCConfig(rpcinfo.RPCConfig(nil)) == nil)
   117  	test.Assert(t, rpcinfo.AsMutableRPCConfig(new(MockRPCConfig)) == nil)
   118  
   119  	c1 := rpcinfo.NewRPCConfig()
   120  	mc := rpcinfo.AsMutableRPCConfig(c1)
   121  	test.Assert(t, mc != nil)
   122  	test.Assert(t, !mc.IsRPCTimeoutLocked())
   123  	test.Assert(t, !mc.IsConnectTimeoutLocked())
   124  	test.Assert(t, !mc.IsReadWriteTimeoutLocked())
   125  
   126  	test.Assert(t, mc.SetRPCTimeout(time.Hour) == nil)
   127  	test.Assert(t, mc.SetConnectTimeout(time.Hour*2) == nil)
   128  	test.Assert(t, mc.SetReadWriteTimeout(time.Hour*3) == nil)
   129  	test.Assert(t, mc.SetIOBufferSize(9999) == nil)
   130  	test.Assert(t, mc.SetTransportProtocol(transport.HTTP) == nil)
   131  	test.Assert(t, mc.SetInteractionMode(rpcinfo.PingPong) == nil)
   132  
   133  	test.Assert(t, c1.RPCTimeout() == time.Hour)
   134  	test.Assert(t, c1.ConnectTimeout() == time.Hour*2)
   135  	test.Assert(t, c1.ReadWriteTimeout() == time.Hour*3)
   136  	test.Assert(t, c1.IOBufferSize() == 9999)
   137  	test.Assert(t, c1.TransportProtocol() == transport.HTTP)
   138  	test.Assert(t, c1.InteractionMode() == rpcinfo.PingPong)
   139  
   140  	mc.LockConfig(rpcinfo.BitRPCTimeout)
   141  	test.Assert(t, mc.IsRPCTimeoutLocked())
   142  	err := mc.SetRPCTimeout(time.Hour * 4)
   143  	test.Assert(t, errors.Is(err, kerrors.ErrNotSupported))
   144  
   145  	mc.LockConfig(rpcinfo.BitConnectTimeout)
   146  	test.Assert(t, mc.IsConnectTimeoutLocked())
   147  	err = mc.SetConnectTimeout(time.Hour * 4)
   148  	test.Assert(t, errors.Is(err, kerrors.ErrNotSupported))
   149  
   150  	mc.LockConfig(rpcinfo.BitReadWriteTimeout)
   151  	test.Assert(t, mc.IsReadWriteTimeoutLocked())
   152  	err = mc.SetReadWriteTimeout(time.Hour * 4)
   153  	test.Assert(t, errors.Is(err, kerrors.ErrNotSupported))
   154  
   155  	mc.LockConfig(rpcinfo.BitIOBufferSize)
   156  	err = mc.SetIOBufferSize(8888)
   157  	test.Assert(t, errors.Is(err, kerrors.ErrNotSupported))
   158  
   159  	mc2 := mc.Clone()
   160  	test.Assert(t, mc2 != nil)
   161  
   162  	test.Assert(t, mc.ImmutableView() != nil)
   163  }