trpc.group/trpc-go/trpc-go@v1.0.3/naming/circuitbreaker/circuitbreaker_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 circuitbreaker
    15  
    16  import (
    17  	"testing"
    18  	"time"
    19  
    20  	"trpc.group/trpc-go/trpc-go/naming/registry"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  type testCircuitBreaker struct{}
    26  
    27  // Available determines whether the circuit breaker is available.
    28  func (cb *testCircuitBreaker) Available(node *registry.Node) bool {
    29  	return true
    30  }
    31  
    32  // Report reports the result.
    33  func (cb *testCircuitBreaker) Report(node *registry.Node, cost time.Duration, err error) error {
    34  	return nil
    35  }
    36  
    37  func TestCircuitBreakerRegister(t *testing.T) {
    38  	Register("cb", &testCircuitBreaker{})
    39  	assert.NotNil(t, Get("cb"))
    40  	unregisterForTesting("cb")
    41  }
    42  
    43  func TestCircuitBreakerGet(t *testing.T) {
    44  	Register("cb", &testCircuitBreaker{})
    45  	assert.NotNil(t, Get("cb"))
    46  	unregisterForTesting("cb")
    47  	assert.Nil(t, Get("not_exist"))
    48  }
    49  
    50  func TestNoopCircuitBreaker(t *testing.T) {
    51  	noop := &NoopCircuitBreaker{}
    52  	assert.True(t, noop.Available(nil))
    53  	assert.Nil(t, noop.Report(nil, 0, nil))
    54  }
    55  
    56  func TestSetDefaultCircuitBreaker(t *testing.T) {
    57  	noop := &NoopCircuitBreaker{}
    58  	SetDefaultCircuitBreaker(noop)
    59  	assert.Equal(t, DefaultCircuitBreaker, noop)
    60  }