Repository: zeromicro/go-zero
Pull request #3921 (2024-02-19 15:55:58)

chore: add tests

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/zeromicro/go-zero/pull/3921?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`6be37ad`)](https://app.codecov.io/gh/zeromicro/go-zero/commit/6be37ad533934bdb379123b8cc9cf2deae600708?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) 94.47% compared to head [(`60a9d65`)](https://app.codecov.io/gh/zeromicro/go-zero/pull/3921?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) 94.49%.

<details><summary>Additional details and impacted files</summary>



| [Files](https://app.codecov.io/gh/zeromicro/go-zero/pull/3921?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) | Coverage Δ | |
|---|---|---|
| [core/breaker/googlebreaker.go](https://app.codecov.io/gh/zeromicro/go-zero/pull/3921?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro#diff-Y29yZS9icmVha2VyL2dvb2dsZWJyZWFrZXIuZ28=) | `100.00% <100.00%> (ø)` | |

... and [1 file with indirect coverage changes](https://app.codecov.io/gh/zeromicro/go-zero/pull/3921/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro)

</details>

Patch:
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
 	}


Test patch:
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)
+}