Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pubsub/pstest)!: Add status code to error injection. #2926

Merged
merged 1 commit into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions pubsub/pstest/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -1171,20 +1171,22 @@ func (s *GServer) runReactor(req interface{}, funcName string, defaultObj interf
return false, nil, nil
}

// ErrorInjectionReactor is a reactor to inject an error message
type ErrorInjectionReactor struct {
errMsg string
// errorInjectionReactor is a reactor to inject an error message with status code.
type errorInjectionReactor struct {
code codes.Code
msg string
}

// React simply returns an error with defined error message.
func (e *ErrorInjectionReactor) React(_ interface{}) (handled bool, ret interface{}, err error) {
return true, nil, fmt.Errorf(e.errMsg)
// React simply returns an error with defined error message and status code.
func (e *errorInjectionReactor) React(_ interface{}) (handled bool, ret interface{}, err error) {
return true, nil, status.Errorf(e.code, e.msg)
}

// WithErrorInjection creates a ServerReactorOption that injects error for a certain function.
func WithErrorInjection(funcName string, errMsg string) ServerReactorOption {
// WithErrorInjection creates a ServerReactorOption that injects error with defined status code and
// message for a certain function.
func WithErrorInjection(funcName string, code codes.Code, msg string) ServerReactorOption {
return ServerReactorOption{
FuncName: funcName,
Reactor: &ErrorInjectionReactor{errMsg: errMsg},
Reactor: &errorInjectionReactor{code: code, msg: msg},
}
}
13 changes: 11 additions & 2 deletions pubsub/pstest/fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -951,15 +951,19 @@ func TestErrorInjection(t *testing.T) {
testcases := []struct {
funcName string
param interface{}
code codes.Code
}{
{
funcName: "CreateTopic",
code: codes.Internal,
},
{
funcName: "GetTopic",
code: codes.Aborted,
},
{
funcName: "UpdateTopic",
code: codes.DeadlineExceeded,
},
{
funcName: "ListTopics",
Expand Down Expand Up @@ -1010,8 +1014,13 @@ func TestErrorInjection(t *testing.T) {
for _, tc := range testcases {
ctx := context.TODO()
errMsg := "error-injection-" + tc.funcName
// set error code to unknown unless specified
ec := codes.Unknown
if tc.code != codes.OK {
ec = tc.code
}
opts := []ServerReactorOption{
WithErrorInjection(tc.funcName, errMsg),
WithErrorInjection(tc.funcName, ec, errMsg),
}
_, _, server, cleanup := newFake(ctx, t, opts...)
defer cleanup()
Expand All @@ -1033,7 +1042,7 @@ func TestErrorInjection(t *testing.T) {
ret := reflect.ValueOf(&server.GServer).MethodByName(tc.funcName).Call([]reflect.Value{reflect.ValueOf(ctx), req})

got := ret[1].Interface().(error)
if got == nil || !strings.Contains(got.Error(), errMsg) {
if got == nil || status.Code(got) != ec || !strings.Contains(got.Error(), errMsg) {
t.Errorf("Got error does not contain the right key %v", got)
}
}
Expand Down