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

Fix deepsource: VET-V0008 Lock erroneously passed by value (pkg/manager) #1861

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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: 16 additions & 4 deletions pkg/manager/index/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func TestNewConfig(t *testing.T) {
args args
want want
checkFunc func(want, *Data, error) error
beforeFunc func(args)
afterFunc func(args)
beforeFunc func(*testing.T, args)
afterFunc func(*testing.T, args)
}
defaultCheckFunc := func(w want, gotCfg *Data, err error) error {
if !errors.Is(err, w.err) {
Expand All @@ -60,6 +60,12 @@ func TestNewConfig(t *testing.T) {
},
want: want{},
checkFunc: defaultCheckFunc,
beforeFunc: func(t *testing.T, args args) {
t.Helper()
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
},
},
*/

Expand All @@ -73,6 +79,12 @@ func TestNewConfig(t *testing.T) {
},
want: want{},
checkFunc: defaultCheckFunc,
beforeFunc: func(t *testing.T, args args) {
t.Helper()
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
},
}
}(),
*/
Expand All @@ -84,10 +96,10 @@ func TestNewConfig(t *testing.T) {
tt.Parallel()
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
if test.beforeFunc != nil {
test.beforeFunc(test.args)
test.beforeFunc(tt, test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
defer test.afterFunc(tt, test.args)
}
checkFunc := test.checkFunc
if test.checkFunc == nil {
Expand Down
48 changes: 38 additions & 10 deletions pkg/manager/index/handler/grpc/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func TestNew(t *testing.T) {
args args
want want
checkFunc func(want, index.IndexServer) error
beforeFunc func(args)
afterFunc func(args)
beforeFunc func(*testing.T, args)
afterFunc func(*testing.T, args)
}
defaultCheckFunc := func(w want, got index.IndexServer) error {
if !reflect.DeepEqual(got, w.want) {
Expand All @@ -60,6 +60,12 @@ func TestNew(t *testing.T) {
},
want: want{},
checkFunc: defaultCheckFunc,
beforeFunc: func(t *testing.T, args args) {
t.Helper()
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
},
},
*/

Expand All @@ -73,6 +79,12 @@ func TestNew(t *testing.T) {
},
want: want{},
checkFunc: defaultCheckFunc,
beforeFunc: func(t *testing.T, args args) {
t.Helper()
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
},
}
}(),
*/
Expand All @@ -84,10 +96,10 @@ func TestNew(t *testing.T) {
tt.Parallel()
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
if test.beforeFunc != nil {
test.beforeFunc(test.args)
test.beforeFunc(tt, test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
defer test.afterFunc(tt, test.args)
}
checkFunc := test.checkFunc
if test.checkFunc == nil {
Expand All @@ -108,7 +120,8 @@ func Test_server_IndexInfo(t *testing.T) {
in1 *payload.Empty
}
type fields struct {
indexer service.Indexer
indexer service.Indexer
UnimplementedIndexServer index.UnimplementedIndexServer
}
type want struct {
wantRes *payload.Info_Index_Count
Expand All @@ -120,8 +133,8 @@ func Test_server_IndexInfo(t *testing.T) {
fields fields
want want
checkFunc func(want, *payload.Info_Index_Count, error) error
beforeFunc func(args)
afterFunc func(args)
beforeFunc func(*testing.T, args)
afterFunc func(*testing.T, args)
}
defaultCheckFunc := func(w want, gotRes *payload.Info_Index_Count, err error) error {
if !errors.Is(err, w.err) {
Expand All @@ -143,9 +156,16 @@ func Test_server_IndexInfo(t *testing.T) {
},
fields: fields {
indexer: nil,
UnimplementedIndexServer: nil,
},
want: want{},
checkFunc: defaultCheckFunc,
beforeFunc: func(t *testing.T, args args) {
t.Helper()
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
},
},
*/

Expand All @@ -160,9 +180,16 @@ func Test_server_IndexInfo(t *testing.T) {
},
fields: fields {
indexer: nil,
UnimplementedIndexServer: nil,
},
want: want{},
checkFunc: defaultCheckFunc,
beforeFunc: func(t *testing.T, args args) {
t.Helper()
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
},
}
}(),
*/
Expand All @@ -174,17 +201,18 @@ func Test_server_IndexInfo(t *testing.T) {
tt.Parallel()
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
if test.beforeFunc != nil {
test.beforeFunc(test.args)
test.beforeFunc(tt, test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
defer test.afterFunc(tt, test.args)
}
checkFunc := test.checkFunc
if test.checkFunc == nil {
checkFunc = defaultCheckFunc
}
s := &server{
indexer: test.fields.indexer,
indexer: test.fields.indexer,
UnimplementedIndexServer: test.fields.UnimplementedIndexServer,
}

gotRes, err := s.IndexInfo(test.args.ctx, test.args.in1)
Expand Down
107 changes: 39 additions & 68 deletions pkg/manager/index/handler/grpc/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,35 @@
package grpc

import (
"reflect"
"testing"

"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/test/goleak"
"github.com/vdaas/vald/pkg/manager/index/service"
)

func TestWithIndexer(t *testing.T) {
// Change interface type to the type of object you are testing
type T = interface{}
type args struct {
i service.Indexer
}
type want struct {
obj *T
// Uncomment this line if the option returns an error, otherwise delete it
// err error
want Option
}
type test struct {
name string
args args
want want
// Use the first line if the option returns an error. otherwise use the second line
// checkFunc func(want, *T, error) error
// checkFunc func(want, *T) error
beforeFunc func(args)
afterFunc func(args)
name string
args args
want want
checkFunc func(want, Option) error
beforeFunc func(*testing.T, args)
afterFunc func(*testing.T, args)
}
defaultCheckFunc := func(w want, got Option) error {
if !reflect.DeepEqual(got, w.want) {
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want)
}
return nil
}

// Uncomment this block if the option returns an error, otherwise delete it
/*
defaultCheckFunc := func(w want, obj *T, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err)
}
if !reflect.DeepEqual(obj, w.obj) {
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", obj, w.obj)
}
return nil
}
*/

// Uncomment this block if the option do not returns an error, otherwise delete it
/*
defaultCheckFunc := func(w want, obj *T) error {
if !reflect.DeepEqual(obj, w.obj) {
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", obj, w.obj)
}
return nil
}
*/

tests := []test{
// TODO test cases
/*
Expand All @@ -77,8 +55,13 @@ func TestWithIndexer(t *testing.T) {
args: args {
i: nil,
},
want: want {
obj: new(T),
want: want{},
checkFunc: defaultCheckFunc,
beforeFunc: func(t *testing.T, args args) {
t.Helper()
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
},
},
*/
Expand All @@ -91,8 +74,13 @@ func TestWithIndexer(t *testing.T) {
args: args {
i: nil,
},
want: want {
obj: new(T),
want: want{},
checkFunc: defaultCheckFunc,
beforeFunc: func(t *testing.T, args args) {
t.Helper()
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
},
}
}(),
Expand All @@ -105,37 +93,20 @@ func TestWithIndexer(t *testing.T) {
tt.Parallel()
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
if test.beforeFunc != nil {
test.beforeFunc(test.args)
test.beforeFunc(tt, test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
defer test.afterFunc(tt, test.args)
}
checkFunc := test.checkFunc
if test.checkFunc == nil {
checkFunc = defaultCheckFunc
}

// Uncomment this block if the option returns an error, otherwise delete it
/*
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

got := WithIndexer(test.args.i)
obj := new(T)
if err := checkFunc(test.want, obj, got(obj)); err != nil {
tt.Errorf("error = %v", err)
}
*/

// Uncomment this block if the option do not return an error, otherwise delete it
/*
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}
got := WithIndexer(test.args.i)
obj := new(T)
got(obj)
if err := checkFunc(test.want, obj); err != nil {
tt.Errorf("error = %v", err)
}
*/
got := WithIndexer(test.args.i)
if err := checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}
Loading