chore: add tests (#3921)

master
Kevin Wan 9 months ago committed by GitHub
parent 6be37ad533
commit 25a807afb2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,7 +1,6 @@
package breaker package breaker
import ( import (
"math"
"time" "time"
"github.com/zeromicro/go-zero/core/collection" "github.com/zeromicro/go-zero/core/collection"
@ -38,7 +37,8 @@ func (b *googleBreaker) accept() error {
accepts, total := b.history() accepts, total := b.history()
weightedAccepts := b.k * float64(accepts) weightedAccepts := b.k * float64(accepts)
// https://landing.google.com/sre/sre-book/chapters/handling-overload/#eq2101 // 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 { if dropRatio <= 0 {
return nil return nil
} }

@ -5,6 +5,7 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/breaker"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
@ -27,3 +28,12 @@ func TestUnaryBreakerInterceptor(t *testing.T) {
}) })
assert.NotNil(t, err) 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)
}

Loading…
Cancel
Save