From 25a807afb26e17bee54f7c11c8c36c0c144f83db Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Tue, 20 Feb 2024 10:11:43 +0800 Subject: [PATCH] chore: add tests (#3921) --- core/breaker/googlebreaker.go | 4 ++-- .../serverinterceptors/breakerinterceptor_test.go | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/core/breaker/googlebreaker.go b/core/breaker/googlebreaker.go index a15dfba8..f58566b0 100644 --- a/core/breaker/googlebreaker.go +++ b/core/breaker/googlebreaker.go @@ -1,7 +1,6 @@ package breaker import ( - "math" "time" "github.com/zeromicro/go-zero/core/collection" @@ -38,7 +37,8 @@ func (b *googleBreaker) accept() error { accepts, total := b.history() weightedAccepts := b.k * float64(accepts) // https://landing.google.com/sre/sre-book/chapters/handling-overload/#eq2101 - dropRatio := math.Max(0, (float64(total-protection)-weightedAccepts)/float64(total+1)) + // for better performance, no need to care about negative ratio + dropRatio := (float64(total-protection) - weightedAccepts) / float64(total+1) if dropRatio <= 0 { return nil } diff --git a/zrpc/internal/serverinterceptors/breakerinterceptor_test.go b/zrpc/internal/serverinterceptors/breakerinterceptor_test.go index 26538c27..58662de2 100644 --- a/zrpc/internal/serverinterceptors/breakerinterceptor_test.go +++ b/zrpc/internal/serverinterceptors/breakerinterceptor_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/zeromicro/go-zero/core/breaker" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -27,3 +28,12 @@ func TestUnaryBreakerInterceptor(t *testing.T) { }) assert.NotNil(t, err) } + +func TestUnaryBreakerInterceptor_Unavailable(t *testing.T) { + _, err := UnaryBreakerInterceptor(context.Background(), nil, &grpc.UnaryServerInfo{ + FullMethod: "any", + }, func(_ context.Context, _ any) (any, error) { + return nil, breaker.ErrServiceUnavailable + }) + assert.NotNil(t, err) +}