trpc.group/trpc-go/trpc-go@v1.0.3/naming/loadbalance/loadbalance_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 loadbalance
    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  var testNode *registry.Node = &registry.Node{
    25  	ServiceName: "testservice",
    26  	Address:     "loadbalance.ip.1:16721",
    27  	Network:     "tcp",
    28  }
    29  
    30  type testLoadbalance struct{}
    31  
    32  // Select acquires a node.
    33  func (tlb *testLoadbalance) Select(serviceName string, list []*registry.Node, opt ...Option) (*registry.Node, error) {
    34  	return testNode, nil
    35  }
    36  
    37  func TestLoadbalanceRegister(t *testing.T) {
    38  	Register("tlb", &testLoadbalance{})
    39  	assert.NotNil(t, Get("tlb"))
    40  }
    41  
    42  func TestLoadbalanceGet(t *testing.T) {
    43  	Register("tlb", &testLoadbalance{})
    44  	assert.NotNil(t, Get("tlb"))
    45  	assert.Nil(t, Get("not_exist"))
    46  }
    47  
    48  func TestLoadbalanceSelect(t *testing.T) {
    49  	Register("tlb", &testLoadbalance{})
    50  	lb := Get("tlb")
    51  	n, err := lb.Select("test-service", nil, nil)
    52  	assert.Nil(t, err)
    53  	assert.Equal(t, n, testNode)
    54  }
    55  
    56  func TestSetDefaultLoadBalancer(t *testing.T) {
    57  	noop := &testLoadbalance{}
    58  	SetDefaultLoadBalancer(noop)
    59  	assert.Equal(t, DefaultLoadBalancer, noop)
    60  }