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

Backport PR #2517 to release/v1.7 for Bugfix that caused an error when argument has 3 or more nil arguments #2520

Merged
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
14 changes: 11 additions & 3 deletions internal/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,17 @@
var e *joinError
switch x := errs[0].(type) {
case *joinError:
e = x
if x != nil && len(x.errs) != 0 {
e = x

Check warning on line 267 in internal/errors/errors.go

View check run for this annotation

Codecov / codecov/patch

internal/errors/errors.go#L266-L267

Added lines #L266 - L267 were not covered by tests
}
errs = errs[1:]
case interface{ Unwrap() []error }:
e = &joinError{errs: x.Unwrap()}
if x != nil && len(x.Unwrap()) != 0 {
e = &joinError{errs: x.Unwrap()}

Check warning on line 272 in internal/errors/errors.go

View check run for this annotation

Codecov / codecov/patch

internal/errors/errors.go#L271-L272

Added lines #L271 - L272 were not covered by tests
}
errs = errs[1:]
default:
}
if e == nil {
e = &joinError{
errs: make([]error, 0, l),
}
Expand All @@ -278,6 +283,9 @@
e.errs = append(e.errs, err)
}
}
if len(e.errs) == 0 {
return nil
}
return e
}

Expand Down
169 changes: 83 additions & 86 deletions internal/errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"math"
"reflect"
"testing"

"github.com/vdaas/vald/internal/test/goleak"
)

func TestErrTimeoutParseFailed(t *testing.T) {
Expand Down Expand Up @@ -1649,6 +1651,87 @@ func TestRemoveDuplicates(t *testing.T) {
}
}

func TestJoin(t *testing.T) {
type args struct {
errs []error
}
type want struct {
err error
}
type test struct {
name string
args args
want want
checkFunc func(want, error) error
beforeFunc func(*testing.T, args)
afterFunc func(*testing.T, args)
}
defaultCheckFunc := func(w want, err error) error {
if !Is(err, w.err) {
return Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err)
}
return nil
}
tests := []test{
{
name: "return nil when all errors are nil",
args: args{
errs: []error{
nil, nil, nil,
},
},
},
{
name: "returns an aggregated error when all errors are non-nil and different",
args: args{
errs: []error{
New("error1"), New("error2"), New("error3"),
},
},
want: want{
err: &joinError{
errs: []error{
New("error1"), New("error2"), New("error3"),
},
},
},
},
{
name: "returns an error when errors are mixed nil and non-nil",
args: args{
errs: []error{
nil, New("error1"), nil,
},
},
want: want{
err: New("error1"),
},
},
}
for _, tc := range tests {
test := tc
t.Run(test.name, func(tt *testing.T) {
tt.Parallel()
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
if test.beforeFunc != nil {
test.beforeFunc(tt, test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(tt, test.args)
}
checkFunc := test.checkFunc
if test.checkFunc == nil {
checkFunc = defaultCheckFunc
}

err := Join(test.args.errs...)
if err := checkFunc(test.want, err); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

// NOT IMPLEMENTED BELOW
//
// func TestUnwrap(t *testing.T) {
Expand Down Expand Up @@ -1737,92 +1820,6 @@ func TestRemoveDuplicates(t *testing.T) {
// }
// }
//
// func TestJoin(t *testing.T) {
// type args struct {
// errs []error
// }
// type want struct {
// err error
// }
// type test struct {
// name string
// args args
// want want
// checkFunc func(want, error) error
// beforeFunc func(*testing.T, args)
// afterFunc func(*testing.T, args)
// }
// defaultCheckFunc := func(w want, err error) error {
// if !Is(err, w.err) {
// return Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err)
// }
// return nil
// }
// tests := []test{
// // TODO test cases
// /*
// {
// name: "test_case_1",
// args: args {
// errs:nil,
// },
// want: want{},
// checkFunc: defaultCheckFunc,
// beforeFunc: func(t *testing.T, args args) {
// t.Helper()
// },
// afterFunc: func(t *testing.T, args args) {
// t.Helper()
// },
// },
// */
//
// // TODO test cases
// /*
// func() test {
// return test {
// name: "test_case_2",
// args: args {
// errs:nil,
// },
// want: want{},
// checkFunc: defaultCheckFunc,
// beforeFunc: func(t *testing.T, args args) {
// t.Helper()
// },
// afterFunc: func(t *testing.T, args args) {
// t.Helper()
// },
// }
// }(),
// */
// }
//
// for _, tc := range tests {
// test := tc
// t.Run(test.name, func(tt *testing.T) {
// tt.Parallel()
// defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
// if test.beforeFunc != nil {
// test.beforeFunc(tt, test.args)
// }
// if test.afterFunc != nil {
// defer test.afterFunc(tt, test.args)
// }
// checkFunc := test.checkFunc
// if test.checkFunc == nil {
// checkFunc = defaultCheckFunc
// }
//
// err := Join(test.args.errs...)
// if err := checkFunc(test.want, err); err != nil {
// tt.Errorf("error = %v", err)
// }
//
// })
// }
// }
//
// func Test_joinError_Error(t *testing.T) {
// type fields struct {
// errs []error
Expand Down
Loading