mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
chore: add tests (#3921)
This commit is contained in:
parent
6be37ad533
commit
25a807afb2
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user