trpc.group/trpc-go/trpc-go@v1.0.3/naming/discovery/discovery_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 discovery 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 = ®istry.Node{ 25 ServiceName: "testservice", 26 Address: "testservice.ip.1:16721", 27 Network: "tcp", 28 } 29 30 type testDiscovery struct{} 31 32 // List 获取节点列表 33 func (d *testDiscovery) List(serviceName string, opt ...Option) ([]*registry.Node, error) { 34 return []*registry.Node{testNode}, nil 35 } 36 37 func TestDiscoveryRegister(t *testing.T) { 38 Register("test-discovery", &testDiscovery{}) 39 assert.NotNil(t, Get("test-discovery")) 40 unregisterForTesting("test-discovery") 41 } 42 43 func TestDiscoveryGet(t *testing.T) { 44 Register("test-discovery", &testDiscovery{}) 45 assert.NotNil(t, Get("test-discovery")) 46 unregisterForTesting("test-discovery") 47 assert.Nil(t, Get("not_exist")) 48 } 49 50 func TestDiscoveryList(t *testing.T) { 51 Register("test-discovery", &testDiscovery{}) 52 d := Get("test-discovery") 53 list, err := d.List("test-service", nil) 54 assert.Nil(t, err) 55 assert.Equal(t, list[0], testNode) 56 unregisterForTesting("test-discovery") 57 } 58 59 func TestSetDefaultDiscovery(t *testing.T) { 60 noop := &testDiscovery{} 61 SetDefaultDiscovery(noop) 62 assert.Equal(t, DefaultDiscovery, noop) 63 }