trpc.group/trpc-go/trpc-go@v1.0.3/naming/selector/trpc_selector_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package selector
    15  
    16  import (
    17  	"testing"
    18  
    19  	"trpc.group/trpc-go/trpc-go/naming/registry"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  func TestTrpcSelectorSelect(t *testing.T) {
    25  	selector := &TrpcSelector{}
    26  	n, err := selector.Select("127.0.0.1:12345")
    27  	assert.Nil(t, err)
    28  	assert.Equal(t, n.Address, "127.0.0.1:12345")
    29  }
    30  
    31  func TestTrpcSelectorReport(t *testing.T) {
    32  	selector := &TrpcSelector{}
    33  	n, err := selector.Select("127.0.0.1:12345")
    34  
    35  	assert.Nil(t, err)
    36  	assert.Equal(t, n.Address, "127.0.0.1:12345")
    37  
    38  	assert.Nil(t, selector.Report(n, 0, nil))
    39  }
    40  
    41  func TestTrpcSelectorReportErr(t *testing.T) {
    42  	selector := &TrpcSelector{}
    43  	assert.Equal(t, selector.Report(nil, 0, nil), ErrReportNodeEmpty)
    44  	assert.Equal(t, selector.Report(&registry.Node{}, 0, nil), ErrReportMetaDataEmpty)
    45  	assert.Equal(t, selector.Report(&registry.Node{
    46  		Metadata: make(map[string]interface{}),
    47  	}, 0, nil), ErrReportNoCircuitBreaker)
    48  	assert.Equal(t, selector.Report(&registry.Node{
    49  		Metadata: map[string]interface{}{
    50  			"circuitbreaker": "circuitbreaker",
    51  		},
    52  	}, 0, nil), ErrReportInvalidCircuitBreaker)
    53  }
    54  
    55  func TestTrpcSelectorNil(t *testing.T) {
    56  	selector := &TrpcSelector{}
    57  
    58  	_, err := selector.Select("")
    59  	assert.NotNil(t, err)
    60  
    61  	_, err = selector.Select("service", WithDiscovery(nil))
    62  	assert.NotNil(t, err)
    63  
    64  	_, err = selector.Select("service", WithLoadBalancer(nil))
    65  	assert.NotNil(t, err)
    66  
    67  	_, err = selector.Select("service", WithCircuitBreaker(nil))
    68  	assert.NotNil(t, err)
    69  }