go-zero/zrpc/internal/clientinterceptors/breakerinterceptor_test.go

82 lines
1.8 KiB
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package clientinterceptors
import (
2020-08-23 22:33:20 +08:00
"context"
"errors"
2020-07-26 17:09:05 +08:00
"testing"
"github.com/stretchr/testify/assert"
2020-08-08 16:40:10 +08:00
"github.com/tal-tech/go-zero/core/breaker"
"github.com/tal-tech/go-zero/core/stat"
2020-09-18 11:41:52 +08:00
rcodes "github.com/tal-tech/go-zero/zrpc/internal/codes"
2020-08-23 22:33:20 +08:00
"google.golang.org/grpc"
2020-07-26 17:09:05 +08:00
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func init() {
stat.SetReporter(nil)
}
type mockError struct {
st *status.Status
}
func (m mockError) GRPCStatus() *status.Status {
return m.st
}
func (m mockError) Error() string {
return "mocked error"
}
func TestBreakerInterceptorNotFound(t *testing.T) {
err := mockError{st: status.New(codes.NotFound, "any")}
for i := 0; i < 1000; i++ {
assert.Equal(t, err, breaker.DoWithAcceptable("call", func() error {
return err
2020-08-06 20:55:38 +08:00
}, rcodes.Acceptable))
2020-07-26 17:09:05 +08:00
}
}
func TestBreakerInterceptorDeadlineExceeded(t *testing.T) {
err := mockError{st: status.New(codes.DeadlineExceeded, "any")}
errs := make(map[error]int)
for i := 0; i < 1000; i++ {
e := breaker.DoWithAcceptable("call", func() error {
return err
2020-08-06 20:55:38 +08:00
}, rcodes.Acceptable)
2020-07-26 17:09:05 +08:00
errs[e]++
}
assert.Equal(t, 2, len(errs))
assert.True(t, errs[err] > 0)
assert.True(t, errs[breaker.ErrServiceUnavailable] > 0)
}
2020-08-23 22:33:20 +08:00
func TestBreakerInterceptor(t *testing.T) {
tests := []struct {
name string
err error
}{
{
name: "nil",
err: nil,
},
{
name: "with error",
err: errors.New("mock"),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cc := new(grpc.ClientConn)
err := BreakerInterceptor(context.Background(), "/foo", nil, nil, cc,
func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,
opts ...grpc.CallOption) error {
return test.err
})
assert.Equal(t, test.err, err)
})
}
}