trpc.group/trpc-go/trpc-go@v1.0.3/naming/registry/registry_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 registry
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/stretchr/testify/assert"
    20  )
    21  
    22  type testRegistry struct{}
    23  
    24  // Register 注册
    25  func (r *testRegistry) Register(service string, opt ...Option) error {
    26  	return nil
    27  }
    28  
    29  // Deregister 反注册
    30  func (r *testRegistry) Deregister(service string) error {
    31  	return nil
    32  }
    33  
    34  func TestRegistryRegister(t *testing.T) {
    35  	Register("test-registry", &testRegistry{})
    36  	assert.NotNil(t, Get("test-registry"))
    37  	unregisterForTesting("test-registry")
    38  }
    39  
    40  func TestRegistryGet(t *testing.T) {
    41  	Register("test-registry", &testRegistry{})
    42  	r := Get("test-registry")
    43  	assert.Nil(t, r.Register("service1", nil))
    44  	assert.Nil(t, r.Deregister("service1"))
    45  	unregisterForTesting("test-registry")
    46  }
    47  
    48  func TestNoopRegister(t *testing.T) {
    49  	noop := &NoopRegistry{}
    50  	assert.Equal(t, noop.Register("test", nil), ErrNotImplement)
    51  	assert.Equal(t, noop.Deregister("test"), ErrNotImplement)
    52  }
    53  
    54  func TestSetDefaultRegistry(t *testing.T) {
    55  	noop := &NoopRegistry{}
    56  	SetDefaultRegistry(noop)
    57  	assert.Equal(t, DefaultRegistry, noop)
    58  }
    59  
    60  func unregisterForTesting(name string) {
    61  	lock.Lock()
    62  	delete(registries, name)
    63  	lock.Unlock()
    64  }