github.com/cloudwego/kitex@v0.9.0/pkg/rpcinfo/endpointInfo_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  	"testing"
    21  
    22  	"github.com/cloudwego/kitex/internal/test"
    23  	"github.com/cloudwego/kitex/pkg/rpcinfo"
    24  	"github.com/cloudwego/kitex/pkg/utils"
    25  )
    26  
    27  func TestEndpointInfo(t *testing.T) {
    28  	na := utils.NewNetAddr("nnn", "aaa")
    29  	ts := map[string]string{"a": "b", "1": "2"}
    30  	ei := rpcinfo.NewEndpointInfo("s", "m", na, ts)
    31  
    32  	test.Assert(t, ei.ServiceName() == "s")
    33  	test.Assert(t, ei.Method() == "m")
    34  	test.Assert(t, ei.Address().Network() == "nnn")
    35  	test.Assert(t, ei.Address().String() == "aaa")
    36  	test.Assert(t, ei.DefaultTag("a", "-") == "b")
    37  	test.Assert(t, ei.DefaultTag("1", "-") == "2")
    38  	test.Assert(t, ei.DefaultTag("x", "-") == "-")
    39  
    40  	tag, ok := ei.Tag("a")
    41  	test.Assert(t, ok && tag == "b")
    42  	tag, ok = ei.Tag("1")
    43  	test.Assert(t, ok && tag == "2")
    44  	tag, ok = ei.Tag("x")
    45  	test.Assert(t, !ok && tag == "")
    46  }
    47  
    48  func TestFromBasicInfo(t *testing.T) {
    49  	ebi := &rpcinfo.EndpointBasicInfo{
    50  		ServiceName: "svc",
    51  		Method:      "mtd",
    52  		Tags: map[string]string{
    53  			"a": "b", "1": "2",
    54  		},
    55  	}
    56  
    57  	ei := rpcinfo.FromBasicInfo(ebi)
    58  	test.Assert(t, ei != nil)
    59  	test.Assert(t, ei.ServiceName() == "svc")
    60  	test.Assert(t, ei.Method() == "mtd")
    61  	test.Assert(t, ei.Address() == nil)
    62  	test.Assert(t, ei.DefaultTag("a", "-") == "b")
    63  	test.Assert(t, ei.DefaultTag("1", "-") == "2")
    64  	test.Assert(t, ei.DefaultTag("x", "-") == "-")
    65  }
    66  
    67  func TestEmptyEndpointInfo(t *testing.T) {
    68  	ei := rpcinfo.EmptyEndpointInfo()
    69  	test.Assert(t, ei != nil)
    70  	test.Assert(t, ei.ServiceName() == "")
    71  	test.Assert(t, ei.Method() == "")
    72  	test.Assert(t, ei.Address() == nil)
    73  	test.Assert(t, ei.DefaultTag("!", "?") == "?")
    74  
    75  	ei2 := rpcinfo.EmptyEndpointInfo()
    76  	test.Assert(t, ei != ei2)
    77  }