diff --git a/cmd/filter/ingress/tensorflow/main_test.go b/cmd/filter/ingress/tensorflow/main_test.go new file mode 100644 index 0000000000..07f984b280 --- /dev/null +++ b/cmd/filter/ingress/tensorflow/main_test.go @@ -0,0 +1,82 @@ +// +// Copyright (C) 2019-2021 vdaas.org vald team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +// Package main provides program main +package main + +import ( + "testing" + + "go.uber.org/goleak" +) + +func Test_main(t *testing.T) { + type want struct { + } + type test struct { + name string + want want + checkFunc func(want) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + main() + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} diff --git a/go.mod b/go.mod index c4c64857dd..2b3c36d449 100755 --- a/go.mod +++ b/go.mod @@ -80,7 +80,7 @@ require ( golang.org/x/net v0.0.0-20210525063256-abc453219eb5 golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c golang.org/x/sync v0.0.0-20210220032951-036812b2e83c - golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea + golang.org/x/sys v0.0.0-20210531080801-fdfd190a6549 gonum.org/v1/hdf5 v0.0.0-20200504100616-496fefe91614 gonum.org/v1/plot v0.9.0 google.golang.org/api v0.47.0 diff --git a/go.sum b/go.sum index b8b0d5d8d3..2b98678474 100644 --- a/go.sum +++ b/go.sum @@ -861,8 +861,8 @@ golang.org/x/sys v0.0.0-20210503080704-8803ae5d1324/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210503173754-0981d6026fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea h1:+WiDlPBBaO+h9vPNZi8uJ3k4BkKQB7Iow3aqwHVA5hI= -golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210531080801-fdfd190a6549 h1:OL5GcZ2XPkte3dpfuFQ9o884vrE3BZQhajdntNMruv4= +golang.org/x/sys v0.0.0-20210531080801-fdfd190a6549/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/hack/helm/schema/crd/main_test.go b/hack/helm/schema/crd/main_test.go new file mode 100644 index 0000000000..8e8bff666e --- /dev/null +++ b/hack/helm/schema/crd/main_test.go @@ -0,0 +1,462 @@ +// +// Copyright (C) 2019-2021 vdaas.org vald team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package main + +import ( + "reflect" + "testing" + + "github.com/vdaas/vald/internal/errors" + "go.uber.org/goleak" +) + +func Test_main(t *testing.T) { + type want struct { + } + type test struct { + name string + want want + checkFunc func(want) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + main() + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func Test_genSchema(t *testing.T) { + type args struct { + path string + nindent int + } + type want struct { + err error + } + type test struct { + name string + args args + want want + checkFunc func(want, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, err error) error { + if !errors.Is(err, w.err) { + return errors.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 { + path: "", + nindent: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + path: "", + nindent: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + err := genSchema(test.args.path, test.args.nindent) + if err := test.checkFunc(test.want, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_objectProperties(t *testing.T) { + type args struct { + ls []VSchema + } + type want struct { + want map[string]*Schema + err error + } + type test struct { + name string + args args + want want + checkFunc func(want, map[string]*Schema, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got map[string]*Schema, 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(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ls: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ls: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got, err := objectProperties(test.args.ls) + if err := test.checkFunc(test.want, got, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_genNode(t *testing.T) { + type args struct { + ls []VSchema + } + type want struct { + want *Schema + err error + } + type test struct { + name string + args args + want want + checkFunc func(want, *Schema, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got *Schema, 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(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ls: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ls: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got, err := genNode(test.args.ls) + if err := test.checkFunc(test.want, got, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_newSpec(t *testing.T) { + type args struct { + schemas map[string]*Schema + } + type want struct { + want *Spec + } + type test struct { + name string + args args + want want + checkFunc func(want, *Spec) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got *Spec) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + schemas: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + schemas: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := newSpec(test.args.schemas) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_indent(t *testing.T) { + type args struct { + text string + nindent int + } + type want struct { + want string + } + type test struct { + name string + args args + want want + checkFunc func(want, string) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got string) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + text: "", + nindent: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + text: "", + nindent: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := indent(test.args.text, test.args.nindent) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} diff --git a/internal/client/v1/client/agent/core/client_test.go b/internal/client/v1/client/agent/core/client_test.go index d046e2d99b..c5ace54b99 100644 --- a/internal/client/v1/client/agent/core/client_test.go +++ b/internal/client/v1/client/agent/core/client_test.go @@ -22,6 +22,7 @@ import ( "reflect" "testing" + agent "github.com/vdaas/vald/apis/grpc/v1/agent/core" "github.com/vdaas/vald/internal/client/v1/client" "github.com/vdaas/vald/internal/client/v1/client/vald" "github.com/vdaas/vald/internal/errors" @@ -521,3 +522,488 @@ func Test_agentClient_IndexInfo(t *testing.T) { }) } } + +func TestNewAgentClient(t *testing.T) { + type args struct { + cc *grpc.ClientConn + } + type want struct { + want interface { + vald.Client + client.ObjectReader + client.Indexer + } + } + type test struct { + name string + args args + want want + checkFunc func(want, interface { + vald.Client + client.ObjectReader + client.Indexer + }) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got interface { + vald.Client + client.ObjectReader + client.Indexer + }) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + cc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + cc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := NewAgentClient(test.args.cc) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleAgentClient_CreateIndex(t *testing.T) { + type args struct { + ctx context.Context + req *client.ControlCreateIndexRequest + opts []grpc.CallOption + } + type fields struct { + Client vald.Client + ac agent.AgentClient + } + type want struct { + want *client.Empty + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, *client.Empty, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got *client.Empty, 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(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + req: nil, + opts: nil, + }, + fields: fields { + Client: nil, + ac: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + req: nil, + opts: nil, + }, + fields: fields { + Client: nil, + ac: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleAgentClient{ + Client: test.fields.Client, + ac: test.fields.ac, + } + + got, err := c.CreateIndex(test.args.ctx, test.args.req, test.args.opts...) + if err := test.checkFunc(test.want, got, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleAgentClient_SaveIndex(t *testing.T) { + type args struct { + ctx context.Context + req *client.Empty + opts []grpc.CallOption + } + type fields struct { + Client vald.Client + ac agent.AgentClient + } + type want struct { + want *client.Empty + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, *client.Empty, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got *client.Empty, 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(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + req: nil, + opts: nil, + }, + fields: fields { + Client: nil, + ac: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + req: nil, + opts: nil, + }, + fields: fields { + Client: nil, + ac: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleAgentClient{ + Client: test.fields.Client, + ac: test.fields.ac, + } + + got, err := c.SaveIndex(test.args.ctx, test.args.req, test.args.opts...) + if err := test.checkFunc(test.want, got, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleAgentClient_CreateAndSaveIndex(t *testing.T) { + type args struct { + ctx context.Context + req *client.ControlCreateIndexRequest + opts []grpc.CallOption + } + type fields struct { + Client vald.Client + ac agent.AgentClient + } + type want struct { + want *client.Empty + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, *client.Empty, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got *client.Empty, 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(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + req: nil, + opts: nil, + }, + fields: fields { + Client: nil, + ac: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + req: nil, + opts: nil, + }, + fields: fields { + Client: nil, + ac: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleAgentClient{ + Client: test.fields.Client, + ac: test.fields.ac, + } + + got, err := c.CreateAndSaveIndex(test.args.ctx, test.args.req, test.args.opts...) + if err := test.checkFunc(test.want, got, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleAgentClient_IndexInfo(t *testing.T) { + type args struct { + ctx context.Context + req *client.Empty + opts []grpc.CallOption + } + type fields struct { + Client vald.Client + ac agent.AgentClient + } + type want struct { + wantRes *client.InfoIndexCount + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, *client.InfoIndexCount, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes *client.InfoIndexCount, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + req: nil, + opts: nil, + }, + fields: fields { + Client: nil, + ac: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + req: nil, + opts: nil, + }, + fields: fields { + Client: nil, + ac: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleAgentClient{ + Client: test.fields.Client, + ac: test.fields.ac, + } + + gotRes, err := c.IndexInfo(test.args.ctx, test.args.req, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} diff --git a/internal/client/v1/client/vald/vald_test.go b/internal/client/v1/client/vald/vald_test.go index 2b5ea17fcd..809803c49b 100644 --- a/internal/client/v1/client/vald/vald_test.go +++ b/internal/client/v1/client/vald/vald_test.go @@ -2448,3 +2448,2323 @@ func Test_client_StreamGetObject(t *testing.T) { }) } } + +func TestNewValdClient(t *testing.T) { + type args struct { + cc *grpc.ClientConn + } + type want struct { + want Client + } + type test struct { + name string + args args + want want + checkFunc func(want, Client) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got Client) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + cc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + cc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := NewValdClient(test.args.cc) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_Start(t *testing.T) { + type args struct { + ctx context.Context + } + type fields struct { + vc vald.Client + } + type want struct { + want <-chan error + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, <-chan error, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got <-chan error, 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(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + got, err := c.Start(test.args.ctx) + if err := test.checkFunc(test.want, got, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_Stop(t *testing.T) { + type args struct { + ctx context.Context + } + type fields struct { + vc vald.Client + } + type want struct { + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, err error) error { + if !errors.Is(err, w.err) { + return errors.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 { + ctx: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + err := c.Stop(test.args.ctx) + if err := test.checkFunc(test.want, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_GRPCClient(t *testing.T) { + type fields struct { + vc vald.Client + } + type want struct { + want grpc.Client + } + type test struct { + name string + fields fields + want want + checkFunc func(want, grpc.Client) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, got grpc.Client) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + got := c.GRPCClient() + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_Exists(t *testing.T) { + type args struct { + ctx context.Context + in *payload.Object_ID + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantOid *payload.Object_ID + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, *payload.Object_ID, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotOid *payload.Object_ID, 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(gotOid, w.wantOid) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotOid, w.wantOid) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotOid, err := c.Exists(test.args.ctx, test.args.in, test.args.opts...) + if err := test.checkFunc(test.want, gotOid, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_Search(t *testing.T) { + type args struct { + ctx context.Context + in *payload.Search_Request + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes *payload.Search_Response + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, *payload.Search_Response, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes *payload.Search_Response, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.Search(test.args.ctx, test.args.in, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_SearchByID(t *testing.T) { + type args struct { + ctx context.Context + in *payload.Search_IDRequest + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes *payload.Search_Response + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, *payload.Search_Response, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes *payload.Search_Response, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.SearchByID(test.args.ctx, test.args.in, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_StreamSearch(t *testing.T) { + type args struct { + ctx context.Context + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes vald.Search_StreamSearchClient + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, vald.Search_StreamSearchClient, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes vald.Search_StreamSearchClient, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.StreamSearch(test.args.ctx, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_StreamSearchByID(t *testing.T) { + type args struct { + ctx context.Context + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes vald.Search_StreamSearchByIDClient + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, vald.Search_StreamSearchByIDClient, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes vald.Search_StreamSearchByIDClient, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.StreamSearchByID(test.args.ctx, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_MultiSearch(t *testing.T) { + type args struct { + ctx context.Context + in *payload.Search_MultiRequest + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes *payload.Search_Responses + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, *payload.Search_Responses, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes *payload.Search_Responses, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.MultiSearch(test.args.ctx, test.args.in, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_MultiSearchByID(t *testing.T) { + type args struct { + ctx context.Context + in *payload.Search_MultiIDRequest + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes *payload.Search_Responses + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, *payload.Search_Responses, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes *payload.Search_Responses, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.MultiSearchByID(test.args.ctx, test.args.in, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_Insert(t *testing.T) { + type args struct { + ctx context.Context + in *payload.Insert_Request + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes *payload.Object_Location + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, *payload.Object_Location, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes *payload.Object_Location, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.Insert(test.args.ctx, test.args.in, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_StreamInsert(t *testing.T) { + type args struct { + ctx context.Context + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes vald.Insert_StreamInsertClient + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, vald.Insert_StreamInsertClient, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes vald.Insert_StreamInsertClient, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.StreamInsert(test.args.ctx, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_MultiInsert(t *testing.T) { + type args struct { + ctx context.Context + in *payload.Insert_MultiRequest + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes *payload.Object_Locations + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, *payload.Object_Locations, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes *payload.Object_Locations, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.MultiInsert(test.args.ctx, test.args.in, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_Update(t *testing.T) { + type args struct { + ctx context.Context + in *payload.Update_Request + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes *payload.Object_Location + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, *payload.Object_Location, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes *payload.Object_Location, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.Update(test.args.ctx, test.args.in, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_StreamUpdate(t *testing.T) { + type args struct { + ctx context.Context + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes vald.Update_StreamUpdateClient + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, vald.Update_StreamUpdateClient, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes vald.Update_StreamUpdateClient, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.StreamUpdate(test.args.ctx, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_MultiUpdate(t *testing.T) { + type args struct { + ctx context.Context + in *payload.Update_MultiRequest + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes *payload.Object_Locations + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, *payload.Object_Locations, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes *payload.Object_Locations, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.MultiUpdate(test.args.ctx, test.args.in, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_Upsert(t *testing.T) { + type args struct { + ctx context.Context + in *payload.Upsert_Request + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes *payload.Object_Location + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, *payload.Object_Location, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes *payload.Object_Location, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.Upsert(test.args.ctx, test.args.in, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_StreamUpsert(t *testing.T) { + type args struct { + ctx context.Context + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes vald.Upsert_StreamUpsertClient + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, vald.Upsert_StreamUpsertClient, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes vald.Upsert_StreamUpsertClient, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.StreamUpsert(test.args.ctx, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_MultiUpsert(t *testing.T) { + type args struct { + ctx context.Context + in *payload.Upsert_MultiRequest + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes *payload.Object_Locations + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, *payload.Object_Locations, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes *payload.Object_Locations, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.MultiUpsert(test.args.ctx, test.args.in, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_Remove(t *testing.T) { + type args struct { + ctx context.Context + in *payload.Remove_Request + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes *payload.Object_Location + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, *payload.Object_Location, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes *payload.Object_Location, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.Remove(test.args.ctx, test.args.in, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_StreamRemove(t *testing.T) { + type args struct { + ctx context.Context + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes vald.Remove_StreamRemoveClient + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, vald.Remove_StreamRemoveClient, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes vald.Remove_StreamRemoveClient, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.StreamRemove(test.args.ctx, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_MultiRemove(t *testing.T) { + type args struct { + ctx context.Context + in *payload.Remove_MultiRequest + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes *payload.Object_Locations + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, *payload.Object_Locations, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes *payload.Object_Locations, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.MultiRemove(test.args.ctx, test.args.in, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_GetObject(t *testing.T) { + type args struct { + ctx context.Context + in *payload.Object_VectorRequest + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes *payload.Object_Vector + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, *payload.Object_Vector, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes *payload.Object_Vector, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + in: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.GetObject(test.args.ctx, test.args.in, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_singleClient_StreamGetObject(t *testing.T) { + type args struct { + ctx context.Context + opts []grpc.CallOption + } + type fields struct { + vc vald.Client + } + type want struct { + wantRes vald.Object_StreamGetObjectClient + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, vald.Object_StreamGetObjectClient, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes vald.Object_StreamGetObjectClient, 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(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + opts: nil, + }, + fields: fields { + vc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &singleClient{ + vc: test.fields.vc, + } + + gotRes, err := c.StreamGetObject(test.args.ctx, test.args.opts...) + if err := test.checkFunc(test.want, gotRes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} diff --git a/internal/compress/mock_test.go b/internal/compress/mock_test.go new file mode 100644 index 0000000000..45f7a67005 --- /dev/null +++ b/internal/compress/mock_test.go @@ -0,0 +1,781 @@ +// +// Copyright (C) 2019-2021 vdaas.org vald team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package compress + +import ( + "io" + "reflect" + "testing" + + "github.com/vdaas/vald/internal/errors" + "go.uber.org/goleak" +) + +func TestMockCompressor_CompressVector(t *testing.T) { + type args struct { + vector []float32 + } + type fields struct { + CompressVectorFunc func(vector []float32) (bytes []byte, err error) + DecompressVectorFunc func(bytes []byte) (vector []float32, err error) + ReaderFunc func(src io.ReadCloser) (io.ReadCloser, error) + WriterFunc func(dst io.WriteCloser) (io.WriteCloser, error) + } + type want struct { + wantBytes []byte + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, []byte, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotBytes []byte, 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(gotBytes, w.wantBytes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotBytes, w.wantBytes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + vector: nil, + }, + fields: fields { + CompressVectorFunc: nil, + DecompressVectorFunc: nil, + ReaderFunc: nil, + WriterFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + vector: nil, + }, + fields: fields { + CompressVectorFunc: nil, + DecompressVectorFunc: nil, + ReaderFunc: nil, + WriterFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &MockCompressor{ + CompressVectorFunc: test.fields.CompressVectorFunc, + DecompressVectorFunc: test.fields.DecompressVectorFunc, + ReaderFunc: test.fields.ReaderFunc, + WriterFunc: test.fields.WriterFunc, + } + + gotBytes, err := m.CompressVector(test.args.vector) + if err := test.checkFunc(test.want, gotBytes, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func TestMockCompressor_DecompressVector(t *testing.T) { + type args struct { + bytes []byte + } + type fields struct { + CompressVectorFunc func(vector []float32) (bytes []byte, err error) + DecompressVectorFunc func(bytes []byte) (vector []float32, err error) + ReaderFunc func(src io.ReadCloser) (io.ReadCloser, error) + WriterFunc func(dst io.WriteCloser) (io.WriteCloser, error) + } + type want struct { + wantVector []float32 + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, []float32, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotVector []float32, 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(gotVector, w.wantVector) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotVector, w.wantVector) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + bytes: nil, + }, + fields: fields { + CompressVectorFunc: nil, + DecompressVectorFunc: nil, + ReaderFunc: nil, + WriterFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + bytes: nil, + }, + fields: fields { + CompressVectorFunc: nil, + DecompressVectorFunc: nil, + ReaderFunc: nil, + WriterFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &MockCompressor{ + CompressVectorFunc: test.fields.CompressVectorFunc, + DecompressVectorFunc: test.fields.DecompressVectorFunc, + ReaderFunc: test.fields.ReaderFunc, + WriterFunc: test.fields.WriterFunc, + } + + gotVector, err := m.DecompressVector(test.args.bytes) + if err := test.checkFunc(test.want, gotVector, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func TestMockCompressor_Reader(t *testing.T) { + type args struct { + src io.ReadCloser + } + type fields struct { + CompressVectorFunc func(vector []float32) (bytes []byte, err error) + DecompressVectorFunc func(bytes []byte) (vector []float32, err error) + ReaderFunc func(src io.ReadCloser) (io.ReadCloser, error) + WriterFunc func(dst io.WriteCloser) (io.WriteCloser, error) + } + type want struct { + want io.ReadCloser + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, io.ReadCloser, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got io.ReadCloser, 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(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + src: nil, + }, + fields: fields { + CompressVectorFunc: nil, + DecompressVectorFunc: nil, + ReaderFunc: nil, + WriterFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + src: nil, + }, + fields: fields { + CompressVectorFunc: nil, + DecompressVectorFunc: nil, + ReaderFunc: nil, + WriterFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &MockCompressor{ + CompressVectorFunc: test.fields.CompressVectorFunc, + DecompressVectorFunc: test.fields.DecompressVectorFunc, + ReaderFunc: test.fields.ReaderFunc, + WriterFunc: test.fields.WriterFunc, + } + + got, err := m.Reader(test.args.src) + if err := test.checkFunc(test.want, got, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func TestMockCompressor_Writer(t *testing.T) { + type args struct { + dst io.WriteCloser + } + type fields struct { + CompressVectorFunc func(vector []float32) (bytes []byte, err error) + DecompressVectorFunc func(bytes []byte) (vector []float32, err error) + ReaderFunc func(src io.ReadCloser) (io.ReadCloser, error) + WriterFunc func(dst io.WriteCloser) (io.WriteCloser, error) + } + type want struct { + want io.WriteCloser + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, io.WriteCloser, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got io.WriteCloser, 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(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + dst: nil, + }, + fields: fields { + CompressVectorFunc: nil, + DecompressVectorFunc: nil, + ReaderFunc: nil, + WriterFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + dst: nil, + }, + fields: fields { + CompressVectorFunc: nil, + DecompressVectorFunc: nil, + ReaderFunc: nil, + WriterFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &MockCompressor{ + CompressVectorFunc: test.fields.CompressVectorFunc, + DecompressVectorFunc: test.fields.DecompressVectorFunc, + ReaderFunc: test.fields.ReaderFunc, + WriterFunc: test.fields.WriterFunc, + } + + got, err := m.Writer(test.args.dst) + if err := test.checkFunc(test.want, got, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func TestMockReadCloser_Read(t *testing.T) { + type args struct { + p []byte + } + type fields struct { + ReadFunc func(p []byte) (n int, err error) + CloseFunc func() error + } + type want struct { + want int + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, int, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got int, 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(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + p: nil, + }, + fields: fields { + ReadFunc: nil, + CloseFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + p: nil, + }, + fields: fields { + ReadFunc: nil, + CloseFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &MockReadCloser{ + ReadFunc: test.fields.ReadFunc, + CloseFunc: test.fields.CloseFunc, + } + + got, err := m.Read(test.args.p) + if err := test.checkFunc(test.want, got, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func TestMockReadCloser_Close(t *testing.T) { + type fields struct { + ReadFunc func(p []byte) (n int, err error) + CloseFunc func() error + } + type want struct { + err error + } + type test struct { + name string + fields fields + want want + checkFunc func(want, error) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, err error) error { + if !errors.Is(err, w.err) { + return errors.Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + ReadFunc: nil, + CloseFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + ReadFunc: nil, + CloseFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &MockReadCloser{ + ReadFunc: test.fields.ReadFunc, + CloseFunc: test.fields.CloseFunc, + } + + err := m.Close() + if err := test.checkFunc(test.want, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func TestMockWriteCloser_Write(t *testing.T) { + type args struct { + p []byte + } + type fields struct { + WriteFunc func(p []byte) (n int, err error) + CloseFunc func() error + } + type want struct { + want int + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, int, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got int, 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(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + p: nil, + }, + fields: fields { + WriteFunc: nil, + CloseFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + p: nil, + }, + fields: fields { + WriteFunc: nil, + CloseFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &MockWriteCloser{ + WriteFunc: test.fields.WriteFunc, + CloseFunc: test.fields.CloseFunc, + } + + got, err := m.Write(test.args.p) + if err := test.checkFunc(test.want, got, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func TestMockWriteCloser_Close(t *testing.T) { + type fields struct { + WriteFunc func(p []byte) (n int, err error) + CloseFunc func() error + } + type want struct { + err error + } + type test struct { + name string + fields fields + want want + checkFunc func(want, error) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, err error) error { + if !errors.Is(err, w.err) { + return errors.Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + WriteFunc: nil, + CloseFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + WriteFunc: nil, + CloseFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &MockWriteCloser{ + WriteFunc: test.fields.WriteFunc, + CloseFunc: test.fields.CloseFunc, + } + + err := m.Close() + if err := test.checkFunc(test.want, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} diff --git a/internal/db/storage/blob/cloudstorage/cloudstorage_test.go b/internal/db/storage/blob/cloudstorage/cloudstorage_test.go new file mode 100644 index 0000000000..51756fe99d --- /dev/null +++ b/internal/db/storage/blob/cloudstorage/cloudstorage_test.go @@ -0,0 +1,519 @@ +// +// Copyright (C) 2019-2021 vdaas.org vald team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package cloudstorage + +import ( + "context" + "io" + "net/url" + "reflect" + "testing" + + iblob "github.com/vdaas/vald/internal/db/storage/blob" + "github.com/vdaas/vald/internal/errors" + "go.uber.org/goleak" + "gocloud.dev/blob" + "gocloud.dev/blob/gcsblob" +) + +func TestNew(t *testing.T) { + type args struct { + opts []Option + } + type want struct { + want iblob.Bucket + err error + } + type test struct { + name string + args args + want want + checkFunc func(want, iblob.Bucket, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got iblob.Bucket, 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(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + opts: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + opts: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got, err := New(test.args.opts...) + if err := test.checkFunc(test.want, got, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_client_Open(t *testing.T) { + type args struct { + ctx context.Context + } + type fields struct { + url *url.URL + urlOpener *gcsblob.URLOpener + bucket *blob.Bucket + readerOpts *blob.ReaderOptions + writerOpts *blob.WriterOptions + } + type want struct { + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, err error) error { + if !errors.Is(err, w.err) { + return errors.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 { + ctx: nil, + }, + fields: fields { + url: nil, + urlOpener: nil, + bucket: nil, + readerOpts: nil, + writerOpts: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + }, + fields: fields { + url: nil, + urlOpener: nil, + bucket: nil, + readerOpts: nil, + writerOpts: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &client{ + url: test.fields.url, + urlOpener: test.fields.urlOpener, + bucket: test.fields.bucket, + readerOpts: test.fields.readerOpts, + writerOpts: test.fields.writerOpts, + } + + err := c.Open(test.args.ctx) + if err := test.checkFunc(test.want, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_client_Close(t *testing.T) { + type fields struct { + url *url.URL + urlOpener *gcsblob.URLOpener + bucket *blob.Bucket + readerOpts *blob.ReaderOptions + writerOpts *blob.WriterOptions + } + type want struct { + err error + } + type test struct { + name string + fields fields + want want + checkFunc func(want, error) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, err error) error { + if !errors.Is(err, w.err) { + return errors.Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + url: nil, + urlOpener: nil, + bucket: nil, + readerOpts: nil, + writerOpts: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + url: nil, + urlOpener: nil, + bucket: nil, + readerOpts: nil, + writerOpts: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &client{ + url: test.fields.url, + urlOpener: test.fields.urlOpener, + bucket: test.fields.bucket, + readerOpts: test.fields.readerOpts, + writerOpts: test.fields.writerOpts, + } + + err := c.Close() + if err := test.checkFunc(test.want, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_client_Reader(t *testing.T) { + type args struct { + ctx context.Context + key string + } + type fields struct { + url *url.URL + urlOpener *gcsblob.URLOpener + bucket *blob.Bucket + readerOpts *blob.ReaderOptions + writerOpts *blob.WriterOptions + } + type want struct { + want io.ReadCloser + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, io.ReadCloser, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got io.ReadCloser, 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(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + key: "", + }, + fields: fields { + url: nil, + urlOpener: nil, + bucket: nil, + readerOpts: nil, + writerOpts: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + key: "", + }, + fields: fields { + url: nil, + urlOpener: nil, + bucket: nil, + readerOpts: nil, + writerOpts: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &client{ + url: test.fields.url, + urlOpener: test.fields.urlOpener, + bucket: test.fields.bucket, + readerOpts: test.fields.readerOpts, + writerOpts: test.fields.writerOpts, + } + + got, err := c.Reader(test.args.ctx, test.args.key) + if err := test.checkFunc(test.want, got, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_client_Writer(t *testing.T) { + type args struct { + ctx context.Context + key string + } + type fields struct { + url *url.URL + urlOpener *gcsblob.URLOpener + bucket *blob.Bucket + readerOpts *blob.ReaderOptions + writerOpts *blob.WriterOptions + } + type want struct { + want io.WriteCloser + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, io.WriteCloser, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got io.WriteCloser, 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(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + key: "", + }, + fields: fields { + url: nil, + urlOpener: nil, + bucket: nil, + readerOpts: nil, + writerOpts: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + key: "", + }, + fields: fields { + url: nil, + urlOpener: nil, + bucket: nil, + readerOpts: nil, + writerOpts: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &client{ + url: test.fields.url, + urlOpener: test.fields.urlOpener, + bucket: test.fields.bucket, + readerOpts: test.fields.readerOpts, + writerOpts: test.fields.writerOpts, + } + + got, err := c.Writer(test.args.ctx, test.args.key) + if err := test.checkFunc(test.want, got, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} diff --git a/internal/db/storage/blob/cloudstorage/option_test.go b/internal/db/storage/blob/cloudstorage/option_test.go new file mode 100644 index 0000000000..fdab99de53 --- /dev/null +++ b/internal/db/storage/blob/cloudstorage/option_test.go @@ -0,0 +1,1415 @@ +// +// Copyright (C) 2019-2021 vdaas.org vald team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package cloudstorage + +import ( + "testing" + + "go.uber.org/goleak" + "gocloud.dev/blob/gcsblob" +) + +func TestWithURL(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + str string + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + str: "", + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + str: "", + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithURL(test.args.str) + obj := new(T) + if err := test.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 := WithURL(test.args.str) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithURLOpener(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + uo *gcsblob.URLOpener + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + uo: nil, + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + uo: nil, + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithURLOpener(test.args.uo) + obj := new(T) + if err := test.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 := WithURLOpener(test.args.uo) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithBeforeRead(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + fn func(asFunc func(interface{}) bool) error + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + fn: nil, + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + fn: nil, + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithBeforeRead(test.args.fn) + obj := new(T) + if err := test.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 := WithBeforeRead(test.args.fn) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithWriteBufferSize(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + size int + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + size: 0, + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + size: 0, + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithWriteBufferSize(test.args.size) + obj := new(T) + if err := test.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 := WithWriteBufferSize(test.args.size) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithWriteCacheControl(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + str string + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + str: "", + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + str: "", + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithWriteCacheControl(test.args.str) + obj := new(T) + if err := test.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 := WithWriteCacheControl(test.args.str) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithWriteContentDisposition(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + str string + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + str: "", + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + str: "", + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithWriteContentDisposition(test.args.str) + obj := new(T) + if err := test.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 := WithWriteContentDisposition(test.args.str) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithWriteContentEncoding(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + str string + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + str: "", + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + str: "", + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithWriteContentEncoding(test.args.str) + obj := new(T) + if err := test.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 := WithWriteContentEncoding(test.args.str) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithWriteContentLanguage(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + str string + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + str: "", + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + str: "", + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithWriteContentLanguage(test.args.str) + obj := new(T) + if err := test.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 := WithWriteContentLanguage(test.args.str) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithWriteContentType(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + str string + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + str: "", + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + str: "", + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithWriteContentType(test.args.str) + obj := new(T) + if err := test.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 := WithWriteContentType(test.args.str) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithWriteContentMD5(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + b []byte + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + b: nil, + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + b: nil, + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithWriteContentMD5(test.args.b) + obj := new(T) + if err := test.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 := WithWriteContentMD5(test.args.b) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithWriteMetadata(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + meta map[string]string + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + meta: nil, + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + meta: nil, + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithWriteMetadata(test.args.meta) + obj := new(T) + if err := test.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 := WithWriteMetadata(test.args.meta) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithBeforeWrite(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + f func(asFunc func(interface{}) bool) error + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + f: nil, + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + f: nil, + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithBeforeWrite(test.args.f) + obj := new(T) + if err := test.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 := WithBeforeWrite(test.args.f) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} diff --git a/internal/db/storage/blob/cloudstorage/urlopener/option_test.go b/internal/db/storage/blob/cloudstorage/urlopener/option_test.go new file mode 100644 index 0000000000..975ca5893b --- /dev/null +++ b/internal/db/storage/blob/cloudstorage/urlopener/option_test.go @@ -0,0 +1,371 @@ +// +// Copyright (C) 2019-2021 vdaas.org vald team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package urlopener + +import ( + "net/http" + "testing" + + "go.uber.org/goleak" +) + +func TestWithCredentialsFile(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + path string + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + path: "", + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + path: "", + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithCredentialsFile(test.args.path) + obj := new(T) + if err := test.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 := WithCredentialsFile(test.args.path) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithCredentialsJSON(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + str string + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + str: "", + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + str: "", + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithCredentialsJSON(test.args.str) + obj := new(T) + if err := test.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 := WithCredentialsJSON(test.args.str) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithHTTPClient(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + c *http.Client + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + c: nil, + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + c: nil, + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithHTTPClient(test.args.c) + obj := new(T) + if err := test.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 := WithHTTPClient(test.args.c) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} diff --git a/internal/db/storage/blob/cloudstorage/urlopener/urlopener_test.go b/internal/db/storage/blob/cloudstorage/urlopener/urlopener_test.go new file mode 100644 index 0000000000..0dd7bfe77a --- /dev/null +++ b/internal/db/storage/blob/cloudstorage/urlopener/urlopener_test.go @@ -0,0 +1,202 @@ +// +// Copyright (C) 2019-2021 vdaas.org vald team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package urlopener + +import ( + "context" + "net/http" + "reflect" + "testing" + + "github.com/vdaas/vald/internal/errors" + "go.uber.org/goleak" + "gocloud.dev/blob/gcsblob" +) + +func TestNew(t *testing.T) { + type args struct { + opts []Option + } + type want struct { + want URLOpener + err error + } + type test struct { + name string + args args + want want + checkFunc func(want, URLOpener, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got URLOpener, 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(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + opts: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + opts: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got, err := New(test.args.opts...) + if err := test.checkFunc(test.want, got, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_urlOpener_URLOpener(t *testing.T) { + type args struct { + ctx context.Context + } + type fields struct { + credentialsFilePath string + credentialsJSON string + client *http.Client + } + type want struct { + wantGuo *gcsblob.URLOpener + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, *gcsblob.URLOpener, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotGuo *gcsblob.URLOpener, 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(gotGuo, w.wantGuo) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotGuo, w.wantGuo) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + ctx: nil, + }, + fields: fields { + credentialsFilePath: "", + credentialsJSON: "", + client: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + }, + fields: fields { + credentialsFilePath: "", + credentialsJSON: "", + client: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + uo := &urlOpener{ + credentialsFilePath: test.fields.credentialsFilePath, + credentialsJSON: test.fields.credentialsJSON, + client: test.fields.client, + } + + gotGuo, err := uo.URLOpener(test.args.ctx) + if err := test.checkFunc(test.want, gotGuo, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} diff --git a/internal/errors/errors_test.go b/internal/errors/errors_test.go index af477c9c01..10348bac32 100644 --- a/internal/errors/errors_test.go +++ b/internal/errors/errors_test.go @@ -714,7 +714,7 @@ func TestNew(t *testing.T) { } tests := []test{ func() test { - wantErr := errors.New("error is occurred") + wantErr := New("error is occurred") return test{ name: "return a New error when msg is not empty.", args: args{ diff --git a/internal/io/copy_test.go b/internal/io/copy_test.go index a61ff4e8a9..5d4edc43a6 100644 --- a/internal/io/copy_test.go +++ b/internal/io/copy_test.go @@ -19,7 +19,11 @@ import ( "bytes" "errors" "io" + "reflect" + "sync" "testing" + + "go.uber.org/goleak" ) // A version of bytes.Buffer without ReadFrom and WriteTo @@ -163,3 +167,175 @@ func TestCopyLargeWriter(t *testing.T) { t.Errorf("Copy error: got %v, want %v", err, want) } } + +func TestNewCopier(t *testing.T) { + type args struct { + size int + } + type want struct { + want Copier + } + type test struct { + name string + args args + want want + checkFunc func(want, Copier) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got Copier) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + size: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + size: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := NewCopier(test.args.size) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_copier_Copy(t *testing.T) { + type args struct { + src io.Reader + } + type fields struct { + pool sync.Pool + bufSize int64 + } + type want struct { + wantWritten int64 + wantDst string + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, int64, string, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotWritten int64, gotDst string, 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(gotWritten, w.wantWritten) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotWritten, w.wantWritten) + } + if !reflect.DeepEqual(gotDst, w.wantDst) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotDst, w.wantDst) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + src: nil, + }, + fields: fields { + pool: sync.Pool{}, + bufSize: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + src: nil, + }, + fields: fields { + pool: sync.Pool{}, + bufSize: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + c := &copier{ + pool: test.fields.pool, + bufSize: test.fields.bufSize, + } + dst := &bytes.Buffer{} + + gotWritten, err := c.Copy(dst, test.args.src) + if err := test.checkFunc(test.want, gotWritten, err, dst.String()); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} diff --git a/internal/net/grpc/status/status_test.go b/internal/net/grpc/status/status_test.go index 14681c388f..330667610b 100644 --- a/internal/net/grpc/status/status_test.go +++ b/internal/net/grpc/status/status_test.go @@ -1680,3 +1680,93 @@ func TestFromError(t *testing.T) { }) } } + +func TestParseError(t *testing.T) { + type args struct { + err error + defaultCode codes.Code + defaultMsg string + details []interface{} + } + type want struct { + wantSt *Status + wantMsg string + err error + } + type test struct { + name string + args args + want want + checkFunc func(want, *Status, string, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotSt *Status, gotMsg string, 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(gotSt, w.wantSt) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotSt, w.wantSt) + } + if !reflect.DeepEqual(gotMsg, w.wantMsg) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotMsg, w.wantMsg) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + err: nil, + defaultCode: nil, + defaultMsg: "", + details: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + err: nil, + defaultCode: nil, + defaultMsg: "", + details: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + gotSt, gotMsg, err := ParseError(test.args.err, test.args.defaultCode, test.args.defaultMsg, test.args.details...) + if err := test.checkFunc(test.want, gotSt, gotMsg, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} diff --git a/internal/observability/trace/status_test.go b/internal/observability/trace/status_test.go index fab9f0eaaf..285b3d0539 100644 --- a/internal/observability/trace/status_test.go +++ b/internal/observability/trace/status_test.go @@ -22,7 +22,9 @@ import ( "testing" "github.com/vdaas/vald/internal/errors" + "github.com/vdaas/vald/internal/net/grpc/codes" "go.opencensus.io/trace" + "go.uber.org/goleak" ) func TestStatusCodeOK(t *testing.T) { @@ -1197,3 +1199,79 @@ func TestStatusCodeUnauthenticated(t *testing.T) { }) } } + +func TestFromGRPCStatus(t *testing.T) { + type args struct { + code codes.Code + msg string + } + type want struct { + want Status + } + type test struct { + name string + args args + want want + checkFunc func(want, Status) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got Status) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + code: nil, + msg: "", + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + code: nil, + msg: "", + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := FromGRPCStatus(test.args.code, test.args.msg) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} diff --git a/internal/servers/server/option_test.go b/internal/servers/server/option_test.go index ddb569a417..c9ed26377a 100644 --- a/internal/servers/server/option_test.go +++ b/internal/servers/server/option_test.go @@ -25,7 +25,9 @@ import ( "github.com/vdaas/vald/internal/errgroup" "github.com/vdaas/vald/internal/errors" "github.com/vdaas/vald/internal/net" + "github.com/vdaas/vald/internal/net/control" "github.com/vdaas/vald/internal/net/grpc" + "go.uber.org/goleak" ) func TestWithHost(t *testing.T) { @@ -2485,3 +2487,467 @@ func TestDefaultHealthServerOption(t *testing.T) { }) } } + +func TestWithNetwork(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + network string + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + network: "", + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + network: "", + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithNetwork(test.args.network) + obj := new(T) + if err := test.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 := WithNetwork(test.args.network) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithSocketPath(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + path string + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + path: "", + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + path: "", + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithSocketPath(test.args.path) + obj := new(T) + if err := test.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 := WithSocketPath(test.args.path) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithSocketFlag(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + flg control.SocketFlag + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + flg: nil, + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + flg: nil, + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithSocketFlag(test.args.flg) + obj := new(T) + if err := test.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 := WithSocketFlag(test.args.flg) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithPreStopFunction(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + f func() error + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + f: nil, + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + f: nil, + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithPreStopFunction(test.args.f) + obj := new(T) + if err := test.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 := WithPreStopFunction(test.args.f) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} diff --git a/internal/servers/server/server_test.go b/internal/servers/server/server_test.go index bee01fb44d..eaba30c8cb 100644 --- a/internal/servers/server/server_test.go +++ b/internal/servers/server/server_test.go @@ -20,6 +20,8 @@ import ( "crypto/tls" "net/http" "net/http/httptest" + "reflect" + "sync" "testing" "time" @@ -27,7 +29,9 @@ import ( "github.com/vdaas/vald/internal/errors" "github.com/vdaas/vald/internal/log" "github.com/vdaas/vald/internal/net" + "github.com/vdaas/vald/internal/net/control" "github.com/vdaas/vald/internal/net/grpc" + "go.uber.org/goleak" ) func TestString(t *testing.T) { @@ -657,3 +661,830 @@ func TestShutdown(t *testing.T) { }) } } + +func TestServerMode_String(t *testing.T) { + type want struct { + want string + } + type test struct { + name string + m ServerMode + want want + checkFunc func(want, string) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, got string) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := test.m.String() + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_server_IsRunning(t *testing.T) { + type fields struct { + mode ServerMode + name string + mu sync.RWMutex + wg sync.WaitGroup + eg errgroup.Group + http struct { + srv *http.Server + h http.Handler + starter func(net.Listener) error + } + grpc struct { + srv *grpc.Server + keepAlive *grpcKeepAlive + opts []grpc.ServerOption + regs []func(*grpc.Server) + } + lc *net.ListenConfig + tcfg *tls.Config + pwt time.Duration + sddur time.Duration + rht time.Duration + rt time.Duration + wt time.Duration + it time.Duration + ctrl control.SocketController + sockFlg control.SocketFlag + network net.NetworkType + socketPath string + port uint16 + host string + enableRestart bool + shuttingDown bool + running bool + preStartFunc func() error + preStopFunc func() error + } + type want struct { + want bool + } + type test struct { + name string + fields fields + want want + checkFunc func(want, bool) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, got bool) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + mode: nil, + name: "", + mu: sync.RWMutex{}, + wg: sync.WaitGroup{}, + eg: nil, + http: nil, + grpc: nil, + lc: nil, + tcfg: nil, + pwt: nil, + sddur: nil, + rht: nil, + rt: nil, + wt: nil, + it: nil, + ctrl: nil, + sockFlg: nil, + network: nil, + socketPath: "", + port: 0, + host: "", + enableRestart: false, + shuttingDown: false, + running: false, + preStartFunc: nil, + preStopFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + mode: nil, + name: "", + mu: sync.RWMutex{}, + wg: sync.WaitGroup{}, + eg: nil, + http: nil, + grpc: nil, + lc: nil, + tcfg: nil, + pwt: nil, + sddur: nil, + rht: nil, + rt: nil, + wt: nil, + it: nil, + ctrl: nil, + sockFlg: nil, + network: nil, + socketPath: "", + port: 0, + host: "", + enableRestart: false, + shuttingDown: false, + running: false, + preStartFunc: nil, + preStopFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + s := &server{ + mode: test.fields.mode, + name: test.fields.name, + mu: test.fields.mu, + wg: test.fields.wg, + eg: test.fields.eg, + http: test.fields.http, + grpc: test.fields.grpc, + lc: test.fields.lc, + tcfg: test.fields.tcfg, + pwt: test.fields.pwt, + sddur: test.fields.sddur, + rht: test.fields.rht, + rt: test.fields.rt, + wt: test.fields.wt, + it: test.fields.it, + ctrl: test.fields.ctrl, + sockFlg: test.fields.sockFlg, + network: test.fields.network, + socketPath: test.fields.socketPath, + port: test.fields.port, + host: test.fields.host, + enableRestart: test.fields.enableRestart, + shuttingDown: test.fields.shuttingDown, + running: test.fields.running, + preStartFunc: test.fields.preStartFunc, + preStopFunc: test.fields.preStopFunc, + } + + got := s.IsRunning() + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_server_Name(t *testing.T) { + type fields struct { + mode ServerMode + name string + mu sync.RWMutex + wg sync.WaitGroup + eg errgroup.Group + http struct { + srv *http.Server + h http.Handler + starter func(net.Listener) error + } + grpc struct { + srv *grpc.Server + keepAlive *grpcKeepAlive + opts []grpc.ServerOption + regs []func(*grpc.Server) + } + lc *net.ListenConfig + tcfg *tls.Config + pwt time.Duration + sddur time.Duration + rht time.Duration + rt time.Duration + wt time.Duration + it time.Duration + ctrl control.SocketController + sockFlg control.SocketFlag + network net.NetworkType + socketPath string + port uint16 + host string + enableRestart bool + shuttingDown bool + running bool + preStartFunc func() error + preStopFunc func() error + } + type want struct { + want string + } + type test struct { + name string + fields fields + want want + checkFunc func(want, string) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, got string) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + mode: nil, + name: "", + mu: sync.RWMutex{}, + wg: sync.WaitGroup{}, + eg: nil, + http: nil, + grpc: nil, + lc: nil, + tcfg: nil, + pwt: nil, + sddur: nil, + rht: nil, + rt: nil, + wt: nil, + it: nil, + ctrl: nil, + sockFlg: nil, + network: nil, + socketPath: "", + port: 0, + host: "", + enableRestart: false, + shuttingDown: false, + running: false, + preStartFunc: nil, + preStopFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + mode: nil, + name: "", + mu: sync.RWMutex{}, + wg: sync.WaitGroup{}, + eg: nil, + http: nil, + grpc: nil, + lc: nil, + tcfg: nil, + pwt: nil, + sddur: nil, + rht: nil, + rt: nil, + wt: nil, + it: nil, + ctrl: nil, + sockFlg: nil, + network: nil, + socketPath: "", + port: 0, + host: "", + enableRestart: false, + shuttingDown: false, + running: false, + preStartFunc: nil, + preStopFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + s := &server{ + mode: test.fields.mode, + name: test.fields.name, + mu: test.fields.mu, + wg: test.fields.wg, + eg: test.fields.eg, + http: test.fields.http, + grpc: test.fields.grpc, + lc: test.fields.lc, + tcfg: test.fields.tcfg, + pwt: test.fields.pwt, + sddur: test.fields.sddur, + rht: test.fields.rht, + rt: test.fields.rt, + wt: test.fields.wt, + it: test.fields.it, + ctrl: test.fields.ctrl, + sockFlg: test.fields.sockFlg, + network: test.fields.network, + socketPath: test.fields.socketPath, + port: test.fields.port, + host: test.fields.host, + enableRestart: test.fields.enableRestart, + shuttingDown: test.fields.shuttingDown, + running: test.fields.running, + preStartFunc: test.fields.preStartFunc, + preStopFunc: test.fields.preStopFunc, + } + + got := s.Name() + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_server_ListenAndServe(t *testing.T) { + type args struct { + ctx context.Context + ech chan<- error + } + type fields struct { + mode ServerMode + name string + mu sync.RWMutex + wg sync.WaitGroup + eg errgroup.Group + http struct { + srv *http.Server + h http.Handler + starter func(net.Listener) error + } + grpc struct { + srv *grpc.Server + keepAlive *grpcKeepAlive + opts []grpc.ServerOption + regs []func(*grpc.Server) + } + lc *net.ListenConfig + tcfg *tls.Config + pwt time.Duration + sddur time.Duration + rht time.Duration + rt time.Duration + wt time.Duration + it time.Duration + ctrl control.SocketController + sockFlg control.SocketFlag + network net.NetworkType + socketPath string + port uint16 + host string + enableRestart bool + shuttingDown bool + running bool + preStartFunc func() error + preStopFunc func() error + } + type want struct { + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, err error) error { + if !errors.Is(err, w.err) { + return errors.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 { + ctx: nil, + ech: nil, + }, + fields: fields { + mode: nil, + name: "", + mu: sync.RWMutex{}, + wg: sync.WaitGroup{}, + eg: nil, + http: nil, + grpc: nil, + lc: nil, + tcfg: nil, + pwt: nil, + sddur: nil, + rht: nil, + rt: nil, + wt: nil, + it: nil, + ctrl: nil, + sockFlg: nil, + network: nil, + socketPath: "", + port: 0, + host: "", + enableRestart: false, + shuttingDown: false, + running: false, + preStartFunc: nil, + preStopFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + ech: nil, + }, + fields: fields { + mode: nil, + name: "", + mu: sync.RWMutex{}, + wg: sync.WaitGroup{}, + eg: nil, + http: nil, + grpc: nil, + lc: nil, + tcfg: nil, + pwt: nil, + sddur: nil, + rht: nil, + rt: nil, + wt: nil, + it: nil, + ctrl: nil, + sockFlg: nil, + network: nil, + socketPath: "", + port: 0, + host: "", + enableRestart: false, + shuttingDown: false, + running: false, + preStartFunc: nil, + preStopFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + s := &server{ + mode: test.fields.mode, + name: test.fields.name, + mu: test.fields.mu, + wg: test.fields.wg, + eg: test.fields.eg, + http: test.fields.http, + grpc: test.fields.grpc, + lc: test.fields.lc, + tcfg: test.fields.tcfg, + pwt: test.fields.pwt, + sddur: test.fields.sddur, + rht: test.fields.rht, + rt: test.fields.rt, + wt: test.fields.wt, + it: test.fields.it, + ctrl: test.fields.ctrl, + sockFlg: test.fields.sockFlg, + network: test.fields.network, + socketPath: test.fields.socketPath, + port: test.fields.port, + host: test.fields.host, + enableRestart: test.fields.enableRestart, + shuttingDown: test.fields.shuttingDown, + running: test.fields.running, + preStartFunc: test.fields.preStartFunc, + preStopFunc: test.fields.preStopFunc, + } + + err := s.ListenAndServe(test.args.ctx, test.args.ech) + if err := test.checkFunc(test.want, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_server_Shutdown(t *testing.T) { + type args struct { + ctx context.Context + } + type fields struct { + mode ServerMode + name string + mu sync.RWMutex + wg sync.WaitGroup + eg errgroup.Group + http struct { + srv *http.Server + h http.Handler + starter func(net.Listener) error + } + grpc struct { + srv *grpc.Server + keepAlive *grpcKeepAlive + opts []grpc.ServerOption + regs []func(*grpc.Server) + } + lc *net.ListenConfig + tcfg *tls.Config + pwt time.Duration + sddur time.Duration + rht time.Duration + rt time.Duration + wt time.Duration + it time.Duration + ctrl control.SocketController + sockFlg control.SocketFlag + network net.NetworkType + socketPath string + port uint16 + host string + enableRestart bool + shuttingDown bool + running bool + preStartFunc func() error + preStopFunc func() error + } + type want struct { + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, err error) error { + if !errors.Is(err, w.err) { + return errors.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 { + ctx: nil, + }, + fields: fields { + mode: nil, + name: "", + mu: sync.RWMutex{}, + wg: sync.WaitGroup{}, + eg: nil, + http: nil, + grpc: nil, + lc: nil, + tcfg: nil, + pwt: nil, + sddur: nil, + rht: nil, + rt: nil, + wt: nil, + it: nil, + ctrl: nil, + sockFlg: nil, + network: nil, + socketPath: "", + port: 0, + host: "", + enableRestart: false, + shuttingDown: false, + running: false, + preStartFunc: nil, + preStopFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + }, + fields: fields { + mode: nil, + name: "", + mu: sync.RWMutex{}, + wg: sync.WaitGroup{}, + eg: nil, + http: nil, + grpc: nil, + lc: nil, + tcfg: nil, + pwt: nil, + sddur: nil, + rht: nil, + rt: nil, + wt: nil, + it: nil, + ctrl: nil, + sockFlg: nil, + network: nil, + socketPath: "", + port: 0, + host: "", + enableRestart: false, + shuttingDown: false, + running: false, + preStartFunc: nil, + preStopFunc: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + s := &server{ + mode: test.fields.mode, + name: test.fields.name, + mu: test.fields.mu, + wg: test.fields.wg, + eg: test.fields.eg, + http: test.fields.http, + grpc: test.fields.grpc, + lc: test.fields.lc, + tcfg: test.fields.tcfg, + pwt: test.fields.pwt, + sddur: test.fields.sddur, + rht: test.fields.rht, + rt: test.fields.rt, + wt: test.fields.wt, + it: test.fields.it, + ctrl: test.fields.ctrl, + sockFlg: test.fields.sockFlg, + network: test.fields.network, + socketPath: test.fields.socketPath, + port: test.fields.port, + host: test.fields.host, + enableRestart: test.fields.enableRestart, + shuttingDown: test.fields.shuttingDown, + running: test.fields.running, + preStartFunc: test.fields.preStartFunc, + preStopFunc: test.fields.preStopFunc, + } + + err := s.Shutdown(test.args.ctx) + if err := test.checkFunc(test.want, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} diff --git a/pkg/agent/core/ngt/service/ngt_test.go b/pkg/agent/core/ngt/service/ngt_test.go index b8e3367c32..8d7525c8ca 100644 --- a/pkg/agent/core/ngt/service/ngt_test.go +++ b/pkg/agent/core/ngt/service/ngt_test.go @@ -5755,3 +5755,175 @@ func Test_ngt_Close(t *testing.T) { }) } } + +func Test_ngt_GetDimensionSize(t *testing.T) { + type fields struct { + core core.NGT + eg errgroup.Group + kvs kvs.BidiMap + vq vqueue.Queue + indexing atomic.Value + saving atomic.Value + lastNoice uint64 + nocie uint64 + nogce uint64 + inMem bool + dim int + alen int + lim time.Duration + dur time.Duration + sdur time.Duration + minLit time.Duration + maxLit time.Duration + litFactor time.Duration + enableProactiveGC bool + path string + poolSize uint32 + radius float32 + epsilon float32 + idelay time.Duration + dcd bool + } + type want struct { + want int + } + type test struct { + name string + fields fields + want want + checkFunc func(want, int) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, got int) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + core: nil, + eg: nil, + kvs: nil, + vq: nil, + indexing: nil, + saving: nil, + lastNoice: 0, + nocie: 0, + nogce: 0, + inMem: false, + dim: 0, + alen: 0, + lim: nil, + dur: nil, + sdur: nil, + minLit: nil, + maxLit: nil, + litFactor: nil, + enableProactiveGC: false, + path: "", + poolSize: 0, + radius: 0, + epsilon: 0, + idelay: nil, + dcd: false, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + core: nil, + eg: nil, + kvs: nil, + vq: nil, + indexing: nil, + saving: nil, + lastNoice: 0, + nocie: 0, + nogce: 0, + inMem: false, + dim: 0, + alen: 0, + lim: nil, + dur: nil, + sdur: nil, + minLit: nil, + maxLit: nil, + litFactor: nil, + enableProactiveGC: false, + path: "", + poolSize: 0, + radius: 0, + epsilon: 0, + idelay: nil, + dcd: false, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + n := &ngt{ + core: test.fields.core, + eg: test.fields.eg, + kvs: test.fields.kvs, + vq: test.fields.vq, + indexing: test.fields.indexing, + saving: test.fields.saving, + lastNoice: test.fields.lastNoice, + nocie: test.fields.nocie, + nogce: test.fields.nogce, + inMem: test.fields.inMem, + dim: test.fields.dim, + alen: test.fields.alen, + lim: test.fields.lim, + dur: test.fields.dur, + sdur: test.fields.sdur, + minLit: test.fields.minLit, + maxLit: test.fields.maxLit, + litFactor: test.fields.litFactor, + enableProactiveGC: test.fields.enableProactiveGC, + path: test.fields.path, + poolSize: test.fields.poolSize, + radius: test.fields.radius, + epsilon: test.fields.epsilon, + idelay: test.fields.idelay, + dcd: test.fields.dcd, + } + + got := n.GetDimensionSize() + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} diff --git a/pkg/agent/core/ngt/service/vqueue/queue.go b/pkg/agent/core/ngt/service/vqueue/queue.go index 652ea1e05d..0e45d1176c 100644 --- a/pkg/agent/core/ngt/service/vqueue/queue.go +++ b/pkg/agent/core/ngt/service/vqueue/queue.go @@ -48,14 +48,14 @@ type Queue interface { } type vqueue struct { - ich chan index // ich is insert channel - uii []index // uii is un inserted index - imu sync.Mutex // insert mutex - uiim map[string]index // uiim is un inserted index map (this value is used for GetVector operation to return queued vector cache data) - dch chan key // dch is delete channel - udk []key // udk is un deleted key - dmu sync.Mutex // delete mutex - udim map[string]int64 // udim is un deleted index map (this value is used for Exists operation to return cache data existence) + ich chan index // ich is insert channel + uii []index // uii is un inserted index + imu sync.Mutex // insert mutex + uiim uiim // uiim is un inserted index map (this value is used for GetVector operation to return queued vector cache data) + dch chan key // dch is delete channel + udk []key // udk is un deleted key + dmu sync.Mutex // delete mutex + udim udim // udim is un deleted index map (this value is used for Exists operation to return cache data existence) eg errgroup.Group finalizingInsert atomic.Value finalizingDelete atomic.Value @@ -96,10 +96,8 @@ func New(opts ...Option) (Queue, error) { } vq.ich = make(chan index, vq.ichSize) vq.uii = make([]index, 0, vq.iBufSize) - vq.uiim = make(map[string]index, vq.iBufSize) vq.dch = make(chan key, vq.dchSize) vq.udk = make([]key, 0, vq.dBufSize) - vq.udim = make(map[string]int64, vq.dBufSize) vq.finalizingInsert.Store(false) vq.finalizingDelete.Store(false) vq.closed.Store(true) @@ -164,11 +162,13 @@ func (v *vqueue) PushInsert(uuid string, vector []float32, date int64) error { if date == 0 { date = time.Now().UnixNano() } - v.ich <- index{ + idx := index{ uuid: uuid, vector: vector, date: date, } + v.uiim.Store(uuid, idx) + v.ich <- idx return nil } @@ -180,6 +180,7 @@ func (v *vqueue) PushDelete(uuid string, date int64) error { if date == 0 { date = time.Now().UnixNano() } + v.udim.Store(uuid, date) v.dch <- key{ uuid: uuid, date: date, @@ -230,16 +231,12 @@ func (v *vqueue) RangePopDelete(ctx context.Context, f func(uuid string) bool) { } func (v *vqueue) GetVector(uuid string) ([]float32, bool) { - v.imu.Lock() - vec, ok := v.uiim[uuid] - v.imu.Unlock() + vec, ok := v.uiim.Load(uuid) if !ok { // data not in the insert queue then return not exists(false) return nil, false } - v.dmu.Lock() - di, ok := v.udim[uuid] - v.dmu.Unlock() + di, ok := v.udim.Load(uuid) if !ok { // data not in the delete queue but exists in insert queue then return exists(true) return vec.vector, true @@ -252,16 +249,12 @@ func (v *vqueue) GetVector(uuid string) ([]float32, bool) { } func (v *vqueue) IVExists(uuid string) bool { - v.imu.Lock() - vec, ok := v.uiim[uuid] - v.imu.Unlock() + vec, ok := v.uiim.Load(uuid) if !ok { // data not in the insert queue then return not exists(false) return false } - v.dmu.Lock() - di, ok := v.udim[uuid] - v.dmu.Unlock() + di, ok := v.udim.Load(uuid) if !ok { // data not in the delete queue but exists in insert queue then return exists(true) return true @@ -271,15 +264,11 @@ func (v *vqueue) IVExists(uuid string) bool { } func (v *vqueue) DVExists(uuid string) bool { - v.dmu.Lock() - di, ok := v.udim[uuid] - v.dmu.Unlock() + di, ok := v.udim.Load(uuid) if !ok { return false } - v.imu.Lock() - vec, ok := v.uiim[uuid] - v.imu.Unlock() + vec, ok := v.uiim.Load(uuid) if !ok { // data not in the insert queue then return not exists(false) return true @@ -292,14 +281,12 @@ func (v *vqueue) DVExists(uuid string) bool { func (v *vqueue) addInsert(i index) { v.imu.Lock() v.uii = append(v.uii, i) - v.uiim[i.uuid] = i v.imu.Unlock() } func (v *vqueue) addDelete(d key) { v.dmu.Lock() v.udk = append(v.udk, d) - v.udim[d.uuid] = d.date v.dmu.Unlock() } @@ -316,9 +303,7 @@ func (v *vqueue) flushAndLoadInsert() (uii []index) { dup := make(map[string]bool, len(uii)/2) dl := make([]int, 0, len(uii)/2) for i, idx := range uii { - v.imu.Lock() - delete(v.uiim, idx.uuid) - v.imu.Unlock() + v.uiim.Delete(idx.uuid) // if duplicated data exists current loop's data is old due to the uii's sort order if dup[idx.uuid] { // if duplicated add id to delete wait list @@ -351,9 +336,7 @@ func (v *vqueue) flushAndLoadDelete() (udk []key) { dup := make(map[string]bool, len(udk)/2) dl := make([]int, 0, len(udk)/2) for i, idx := range udk { - v.dmu.Lock() - delete(v.udim, idx.uuid) - v.dmu.Unlock() + v.udim.Delete(idx.uuid) if dup[idx.uuid] { dl = append(dl, i) } else { @@ -392,11 +375,13 @@ func (v *vqueue) flushAndLoadDelete() (udk []key) { sort.Sort(sort.Reverse(sort.IntSlice(dl))) for _, i := range dl { v.imu.Lock() - // remove from existing map - delete(v.uiim, v.uii[i].uuid) + // load removal target uuid + uuid := v.uii[i].uuid // remove unnecessary insert vector queue data v.uii = append(v.uii[:i], v.uii[i+1:]...) v.imu.Unlock() + // remove from existing map + v.uiim.Delete(uuid) } return udk } diff --git a/pkg/agent/core/ngt/service/vqueue/queue_test.go b/pkg/agent/core/ngt/service/vqueue/queue_test.go index 1d91deb384..12cca30584 100644 --- a/pkg/agent/core/ngt/service/vqueue/queue_test.go +++ b/pkg/agent/core/ngt/service/vqueue/queue_test.go @@ -19,7 +19,6 @@ package vqueue import ( "context" - "os" "reflect" "sync" "sync/atomic" @@ -27,18 +26,10 @@ import ( "github.com/vdaas/vald/internal/errgroup" "github.com/vdaas/vald/internal/errors" - "github.com/vdaas/vald/internal/log" - "github.com/vdaas/vald/internal/log/logger" "go.uber.org/goleak" ) -func TestMain(m *testing.M) { - log.Init(log.WithLoggerType(logger.NOP.String())) - os.Exit(m.Run()) -} - func TestNew(t *testing.T) { - t.Parallel() type args struct { opts []Option } @@ -110,12 +101,12 @@ func TestNew(t *testing.T) { if err := test.checkFunc(test.want, got, err); err != nil { tt.Errorf("error = %v", err) } + }) } } func Test_vqueue_Start(t *testing.T) { - t.Parallel() type args struct { ctx context.Context } @@ -123,10 +114,11 @@ func Test_vqueue_Start(t *testing.T) { ich chan index uii []index imu sync.Mutex - uiim map[string]index + uiim uiim dch chan key udk []key dmu sync.Mutex + udim udim eg errgroup.Group finalizingInsert atomic.Value finalizingDelete atomic.Value @@ -170,10 +162,11 @@ func Test_vqueue_Start(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -200,10 +193,11 @@ func Test_vqueue_Start(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -242,6 +236,7 @@ func Test_vqueue_Start(t *testing.T) { dch: test.fields.dch, udk: test.fields.udk, dmu: test.fields.dmu, + udim: test.fields.udim, eg: test.fields.eg, finalizingInsert: test.fields.finalizingInsert, finalizingDelete: test.fields.finalizingDelete, @@ -256,12 +251,12 @@ func Test_vqueue_Start(t *testing.T) { if err := test.checkFunc(test.want, got, err); err != nil { tt.Errorf("error = %v", err) } + }) } } func Test_vqueue_PushInsert(t *testing.T) { - t.Parallel() type args struct { uuid string vector []float32 @@ -271,10 +266,11 @@ func Test_vqueue_PushInsert(t *testing.T) { ich chan index uii []index imu sync.Mutex - uiim map[string]index + uiim uiim dch chan key udk []key dmu sync.Mutex + udim udim eg errgroup.Group finalizingInsert atomic.Value finalizingDelete atomic.Value @@ -316,10 +312,11 @@ func Test_vqueue_PushInsert(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -348,10 +345,11 @@ func Test_vqueue_PushInsert(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -390,6 +388,7 @@ func Test_vqueue_PushInsert(t *testing.T) { dch: test.fields.dch, udk: test.fields.udk, dmu: test.fields.dmu, + udim: test.fields.udim, eg: test.fields.eg, finalizingInsert: test.fields.finalizingInsert, finalizingDelete: test.fields.finalizingDelete, @@ -404,12 +403,12 @@ func Test_vqueue_PushInsert(t *testing.T) { if err := test.checkFunc(test.want, err); err != nil { tt.Errorf("error = %v", err) } + }) } } func Test_vqueue_PushDelete(t *testing.T) { - t.Parallel() type args struct { uuid string date int64 @@ -418,10 +417,11 @@ func Test_vqueue_PushDelete(t *testing.T) { ich chan index uii []index imu sync.Mutex - uiim map[string]index + uiim uiim dch chan key udk []key dmu sync.Mutex + udim udim eg errgroup.Group finalizingInsert atomic.Value finalizingDelete atomic.Value @@ -462,10 +462,11 @@ func Test_vqueue_PushDelete(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -493,10 +494,11 @@ func Test_vqueue_PushDelete(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -535,6 +537,7 @@ func Test_vqueue_PushDelete(t *testing.T) { dch: test.fields.dch, udk: test.fields.udk, dmu: test.fields.dmu, + udim: test.fields.udim, eg: test.fields.eg, finalizingInsert: test.fields.finalizingInsert, finalizingDelete: test.fields.finalizingDelete, @@ -549,12 +552,12 @@ func Test_vqueue_PushDelete(t *testing.T) { if err := test.checkFunc(test.want, err); err != nil { tt.Errorf("error = %v", err) } + }) } } func Test_vqueue_RangePopInsert(t *testing.T) { - t.Parallel() type args struct { ctx context.Context f func(uuid string, vector []float32) bool @@ -563,10 +566,11 @@ func Test_vqueue_RangePopInsert(t *testing.T) { ich chan index uii []index imu sync.Mutex - uiim map[string]index + uiim uiim dch chan key udk []key dmu sync.Mutex + udim udim eg errgroup.Group finalizingInsert atomic.Value finalizingDelete atomic.Value @@ -576,7 +580,8 @@ func Test_vqueue_RangePopInsert(t *testing.T) { iBufSize int dBufSize int } - type want struct{} + type want struct { + } type test struct { name string args args @@ -602,10 +607,11 @@ func Test_vqueue_RangePopInsert(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -633,10 +639,11 @@ func Test_vqueue_RangePopInsert(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -675,6 +682,7 @@ func Test_vqueue_RangePopInsert(t *testing.T) { dch: test.fields.dch, udk: test.fields.udk, dmu: test.fields.dmu, + udim: test.fields.udim, eg: test.fields.eg, finalizingInsert: test.fields.finalizingInsert, finalizingDelete: test.fields.finalizingDelete, @@ -694,7 +702,6 @@ func Test_vqueue_RangePopInsert(t *testing.T) { } func Test_vqueue_RangePopDelete(t *testing.T) { - t.Parallel() type args struct { ctx context.Context f func(uuid string) bool @@ -703,10 +710,11 @@ func Test_vqueue_RangePopDelete(t *testing.T) { ich chan index uii []index imu sync.Mutex - uiim map[string]index + uiim uiim dch chan key udk []key dmu sync.Mutex + udim udim eg errgroup.Group finalizingInsert atomic.Value finalizingDelete atomic.Value @@ -716,7 +724,8 @@ func Test_vqueue_RangePopDelete(t *testing.T) { iBufSize int dBufSize int } - type want struct{} + type want struct { + } type test struct { name string args args @@ -742,10 +751,11 @@ func Test_vqueue_RangePopDelete(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -773,10 +783,11 @@ func Test_vqueue_RangePopDelete(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -815,6 +826,7 @@ func Test_vqueue_RangePopDelete(t *testing.T) { dch: test.fields.dch, udk: test.fields.udk, dmu: test.fields.dmu, + udim: test.fields.udim, eg: test.fields.eg, finalizingInsert: test.fields.finalizingInsert, finalizingDelete: test.fields.finalizingDelete, @@ -834,7 +846,6 @@ func Test_vqueue_RangePopDelete(t *testing.T) { } func Test_vqueue_GetVector(t *testing.T) { - t.Parallel() type args struct { uuid string } @@ -842,10 +853,11 @@ func Test_vqueue_GetVector(t *testing.T) { ich chan index uii []index imu sync.Mutex - uiim map[string]index + uiim uiim dch chan key udk []key dmu sync.Mutex + udim udim eg errgroup.Group finalizingInsert atomic.Value finalizingDelete atomic.Value @@ -889,10 +901,11 @@ func Test_vqueue_GetVector(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -919,10 +932,11 @@ func Test_vqueue_GetVector(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -961,6 +975,7 @@ func Test_vqueue_GetVector(t *testing.T) { dch: test.fields.dch, udk: test.fields.udk, dmu: test.fields.dmu, + udim: test.fields.udim, eg: test.fields.eg, finalizingInsert: test.fields.finalizingInsert, finalizingDelete: test.fields.finalizingDelete, @@ -975,12 +990,304 @@ func Test_vqueue_GetVector(t *testing.T) { if err := test.checkFunc(test.want, got, got1); err != nil { tt.Errorf("error = %v", err) } + + }) + } +} + +func Test_vqueue_IVExists(t *testing.T) { + type args struct { + uuid string + } + type fields struct { + ich chan index + uii []index + imu sync.Mutex + uiim uiim + dch chan key + udk []key + dmu sync.Mutex + udim udim + eg errgroup.Group + finalizingInsert atomic.Value + finalizingDelete atomic.Value + closed atomic.Value + ichSize int + dchSize int + iBufSize int + dBufSize int + } + type want struct { + want bool + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, bool) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got bool) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + uuid: "", + }, + fields: fields { + ich: nil, + uii: nil, + imu: sync.Mutex{}, + uiim: uiim{}, + dch: nil, + udk: nil, + dmu: sync.Mutex{}, + udim: udim{}, + eg: nil, + finalizingInsert: nil, + finalizingDelete: nil, + closed: nil, + ichSize: 0, + dchSize: 0, + iBufSize: 0, + dBufSize: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + uuid: "", + }, + fields: fields { + ich: nil, + uii: nil, + imu: sync.Mutex{}, + uiim: uiim{}, + dch: nil, + udk: nil, + dmu: sync.Mutex{}, + udim: udim{}, + eg: nil, + finalizingInsert: nil, + finalizingDelete: nil, + closed: nil, + ichSize: 0, + dchSize: 0, + iBufSize: 0, + dBufSize: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + v := &vqueue{ + ich: test.fields.ich, + uii: test.fields.uii, + imu: test.fields.imu, + uiim: test.fields.uiim, + dch: test.fields.dch, + udk: test.fields.udk, + dmu: test.fields.dmu, + udim: test.fields.udim, + eg: test.fields.eg, + finalizingInsert: test.fields.finalizingInsert, + finalizingDelete: test.fields.finalizingDelete, + closed: test.fields.closed, + ichSize: test.fields.ichSize, + dchSize: test.fields.dchSize, + iBufSize: test.fields.iBufSize, + dBufSize: test.fields.dBufSize, + } + + got := v.IVExists(test.args.uuid) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_vqueue_DVExists(t *testing.T) { + type args struct { + uuid string + } + type fields struct { + ich chan index + uii []index + imu sync.Mutex + uiim uiim + dch chan key + udk []key + dmu sync.Mutex + udim udim + eg errgroup.Group + finalizingInsert atomic.Value + finalizingDelete atomic.Value + closed atomic.Value + ichSize int + dchSize int + iBufSize int + dBufSize int + } + type want struct { + want bool + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, bool) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got bool) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + uuid: "", + }, + fields: fields { + ich: nil, + uii: nil, + imu: sync.Mutex{}, + uiim: uiim{}, + dch: nil, + udk: nil, + dmu: sync.Mutex{}, + udim: udim{}, + eg: nil, + finalizingInsert: nil, + finalizingDelete: nil, + closed: nil, + ichSize: 0, + dchSize: 0, + iBufSize: 0, + dBufSize: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + uuid: "", + }, + fields: fields { + ich: nil, + uii: nil, + imu: sync.Mutex{}, + uiim: uiim{}, + dch: nil, + udk: nil, + dmu: sync.Mutex{}, + udim: udim{}, + eg: nil, + finalizingInsert: nil, + finalizingDelete: nil, + closed: nil, + ichSize: 0, + dchSize: 0, + iBufSize: 0, + dBufSize: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + v := &vqueue{ + ich: test.fields.ich, + uii: test.fields.uii, + imu: test.fields.imu, + uiim: test.fields.uiim, + dch: test.fields.dch, + udk: test.fields.udk, + dmu: test.fields.dmu, + udim: test.fields.udim, + eg: test.fields.eg, + finalizingInsert: test.fields.finalizingInsert, + finalizingDelete: test.fields.finalizingDelete, + closed: test.fields.closed, + ichSize: test.fields.ichSize, + dchSize: test.fields.dchSize, + iBufSize: test.fields.iBufSize, + dBufSize: test.fields.dBufSize, + } + + got := v.DVExists(test.args.uuid) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + }) } } func Test_vqueue_addInsert(t *testing.T) { - t.Parallel() type args struct { i index } @@ -988,10 +1295,11 @@ func Test_vqueue_addInsert(t *testing.T) { ich chan index uii []index imu sync.Mutex - uiim map[string]index + uiim uiim dch chan key udk []key dmu sync.Mutex + udim udim eg errgroup.Group finalizingInsert atomic.Value finalizingDelete atomic.Value @@ -1001,7 +1309,8 @@ func Test_vqueue_addInsert(t *testing.T) { iBufSize int dBufSize int } - type want struct{} + type want struct { + } type test struct { name string args args @@ -1026,10 +1335,11 @@ func Test_vqueue_addInsert(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -1056,10 +1366,11 @@ func Test_vqueue_addInsert(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -1098,6 +1409,7 @@ func Test_vqueue_addInsert(t *testing.T) { dch: test.fields.dch, udk: test.fields.udk, dmu: test.fields.dmu, + udim: test.fields.udim, eg: test.fields.eg, finalizingInsert: test.fields.finalizingInsert, finalizingDelete: test.fields.finalizingDelete, @@ -1117,7 +1429,6 @@ func Test_vqueue_addInsert(t *testing.T) { } func Test_vqueue_addDelete(t *testing.T) { - t.Parallel() type args struct { d key } @@ -1125,10 +1436,11 @@ func Test_vqueue_addDelete(t *testing.T) { ich chan index uii []index imu sync.Mutex - uiim map[string]index + uiim uiim dch chan key udk []key dmu sync.Mutex + udim udim eg errgroup.Group finalizingInsert atomic.Value finalizingDelete atomic.Value @@ -1138,7 +1450,8 @@ func Test_vqueue_addDelete(t *testing.T) { iBufSize int dBufSize int } - type want struct{} + type want struct { + } type test struct { name string args args @@ -1163,10 +1476,11 @@ func Test_vqueue_addDelete(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -1193,10 +1507,11 @@ func Test_vqueue_addDelete(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -1235,6 +1550,7 @@ func Test_vqueue_addDelete(t *testing.T) { dch: test.fields.dch, udk: test.fields.udk, dmu: test.fields.dmu, + udim: test.fields.udim, eg: test.fields.eg, finalizingInsert: test.fields.finalizingInsert, finalizingDelete: test.fields.finalizingDelete, @@ -1254,15 +1570,15 @@ func Test_vqueue_addDelete(t *testing.T) { } func Test_vqueue_flushAndLoadInsert(t *testing.T) { - t.Parallel() type fields struct { ich chan index uii []index imu sync.Mutex - uiim map[string]index + uiim uiim dch chan key udk []key dmu sync.Mutex + udim udim eg errgroup.Group finalizingInsert atomic.Value finalizingDelete atomic.Value @@ -1298,10 +1614,11 @@ func Test_vqueue_flushAndLoadInsert(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -1325,10 +1642,11 @@ func Test_vqueue_flushAndLoadInsert(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -1367,6 +1685,7 @@ func Test_vqueue_flushAndLoadInsert(t *testing.T) { dch: test.fields.dch, udk: test.fields.udk, dmu: test.fields.dmu, + udim: test.fields.udim, eg: test.fields.eg, finalizingInsert: test.fields.finalizingInsert, finalizingDelete: test.fields.finalizingDelete, @@ -1381,20 +1700,21 @@ func Test_vqueue_flushAndLoadInsert(t *testing.T) { if err := test.checkFunc(test.want, gotUii); err != nil { tt.Errorf("error = %v", err) } + }) } } func Test_vqueue_flushAndLoadDelete(t *testing.T) { - t.Parallel() type fields struct { ich chan index uii []index imu sync.Mutex - uiim map[string]index + uiim uiim dch chan key udk []key dmu sync.Mutex + udim udim eg errgroup.Group finalizingInsert atomic.Value finalizingDelete atomic.Value @@ -1430,10 +1750,11 @@ func Test_vqueue_flushAndLoadDelete(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -1457,10 +1778,11 @@ func Test_vqueue_flushAndLoadDelete(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -1499,6 +1821,7 @@ func Test_vqueue_flushAndLoadDelete(t *testing.T) { dch: test.fields.dch, udk: test.fields.udk, dmu: test.fields.dmu, + udim: test.fields.udim, eg: test.fields.eg, finalizingInsert: test.fields.finalizingInsert, finalizingDelete: test.fields.finalizingDelete, @@ -1513,20 +1836,21 @@ func Test_vqueue_flushAndLoadDelete(t *testing.T) { if err := test.checkFunc(test.want, gotUdk); err != nil { tt.Errorf("error = %v", err) } + }) } } func Test_vqueue_IVQLen(t *testing.T) { - t.Parallel() type fields struct { ich chan index uii []index imu sync.Mutex - uiim map[string]index + uiim uiim dch chan key udk []key dmu sync.Mutex + udim udim eg errgroup.Group finalizingInsert atomic.Value finalizingDelete atomic.Value @@ -1562,10 +1886,11 @@ func Test_vqueue_IVQLen(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -1589,10 +1914,11 @@ func Test_vqueue_IVQLen(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -1631,6 +1957,7 @@ func Test_vqueue_IVQLen(t *testing.T) { dch: test.fields.dch, udk: test.fields.udk, dmu: test.fields.dmu, + udim: test.fields.udim, eg: test.fields.eg, finalizingInsert: test.fields.finalizingInsert, finalizingDelete: test.fields.finalizingDelete, @@ -1645,20 +1972,21 @@ func Test_vqueue_IVQLen(t *testing.T) { if err := test.checkFunc(test.want, gotL); err != nil { tt.Errorf("error = %v", err) } + }) } } func Test_vqueue_DVQLen(t *testing.T) { - t.Parallel() type fields struct { ich chan index uii []index imu sync.Mutex - uiim map[string]index + uiim uiim dch chan key udk []key dmu sync.Mutex + udim udim eg errgroup.Group finalizingInsert atomic.Value finalizingDelete atomic.Value @@ -1694,10 +2022,11 @@ func Test_vqueue_DVQLen(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -1721,10 +2050,11 @@ func Test_vqueue_DVQLen(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -1763,6 +2093,7 @@ func Test_vqueue_DVQLen(t *testing.T) { dch: test.fields.dch, udk: test.fields.udk, dmu: test.fields.dmu, + udim: test.fields.udim, eg: test.fields.eg, finalizingInsert: test.fields.finalizingInsert, finalizingDelete: test.fields.finalizingDelete, @@ -1777,20 +2108,21 @@ func Test_vqueue_DVQLen(t *testing.T) { if err := test.checkFunc(test.want, gotL); err != nil { tt.Errorf("error = %v", err) } + }) } } func Test_vqueue_IVCLen(t *testing.T) { - t.Parallel() type fields struct { ich chan index uii []index imu sync.Mutex - uiim map[string]index + uiim uiim dch chan key udk []key dmu sync.Mutex + udim udim eg errgroup.Group finalizingInsert atomic.Value finalizingDelete atomic.Value @@ -1826,10 +2158,11 @@ func Test_vqueue_IVCLen(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -1853,10 +2186,11 @@ func Test_vqueue_IVCLen(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -1895,6 +2229,7 @@ func Test_vqueue_IVCLen(t *testing.T) { dch: test.fields.dch, udk: test.fields.udk, dmu: test.fields.dmu, + udim: test.fields.udim, eg: test.fields.eg, finalizingInsert: test.fields.finalizingInsert, finalizingDelete: test.fields.finalizingDelete, @@ -1909,20 +2244,21 @@ func Test_vqueue_IVCLen(t *testing.T) { if err := test.checkFunc(test.want, got); err != nil { tt.Errorf("error = %v", err) } + }) } } func Test_vqueue_DVCLen(t *testing.T) { - t.Parallel() type fields struct { ich chan index uii []index imu sync.Mutex - uiim map[string]index + uiim uiim dch chan key udk []key dmu sync.Mutex + udim udim eg errgroup.Group finalizingInsert atomic.Value finalizingDelete atomic.Value @@ -1958,10 +2294,11 @@ func Test_vqueue_DVCLen(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -1985,10 +2322,11 @@ func Test_vqueue_DVCLen(t *testing.T) { ich: nil, uii: nil, imu: sync.Mutex{}, - uiim: nil, + uiim: uiim{}, dch: nil, udk: nil, dmu: sync.Mutex{}, + udim: udim{}, eg: nil, finalizingInsert: nil, finalizingDelete: nil, @@ -2027,6 +2365,7 @@ func Test_vqueue_DVCLen(t *testing.T) { dch: test.fields.dch, udk: test.fields.udk, dmu: test.fields.dmu, + udim: test.fields.udim, eg: test.fields.eg, finalizingInsert: test.fields.finalizingInsert, finalizingDelete: test.fields.finalizingDelete, @@ -2041,6 +2380,7 @@ func Test_vqueue_DVCLen(t *testing.T) { if err := test.checkFunc(test.want, got); err != nil { tt.Errorf("error = %v", err) } + }) } } diff --git a/pkg/agent/core/ngt/service/vqueue/undeleted_index_map.go b/pkg/agent/core/ngt/service/vqueue/undeleted_index_map.go new file mode 100644 index 0000000000..69c4d098ac --- /dev/null +++ b/pkg/agent/core/ngt/service/vqueue/undeleted_index_map.go @@ -0,0 +1,271 @@ +// +// Copyright (C) 2019-2021 vdaas.org vald team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package vqueue + +import ( + "sync" + "sync/atomic" + "unsafe" +) + +type udim struct { + mu sync.Mutex + read atomic.Value + dirty map[string]*entryUdim + misses int +} + +type readOnlyUdim struct { + m map[string]*entryUdim + amended bool +} + +var expungedUdim = unsafe.Pointer(new(int64)) + +type entryUdim struct { + p unsafe.Pointer +} + +func newEntryUdim(i int64) *entryUdim { + return &entryUdim{p: unsafe.Pointer(&i)} +} + +func (m *udim) Load(key string) (value int64, ok bool) { + read, _ := m.read.Load().(readOnlyUdim) + e, ok := read.m[key] + if !ok && read.amended { + m.mu.Lock() + read, _ = m.read.Load().(readOnlyUdim) + e, ok = read.m[key] + if !ok && read.amended { + e, ok = m.dirty[key] + m.missLocked() + } + m.mu.Unlock() + } + if !ok { + return value, false + } + return e.load() +} + +func (e *entryUdim) load() (value int64, ok bool) { + p := atomic.LoadPointer(&e.p) + if p == nil || p == expungedUdim { + return value, false + } + return *(*int64)(p), true +} + +func (m *udim) Store(key string, value int64) { + read, _ := m.read.Load().(readOnlyUdim) + if e, ok := read.m[key]; ok && e.tryStore(&value) { + return + } + + m.mu.Lock() + read, _ = m.read.Load().(readOnlyUdim) + if e, ok := read.m[key]; ok { + if e.unexpungeLocked() { + m.dirty[key] = e + } + e.storeLocked(&value) + } else if e, ok := m.dirty[key]; ok { + e.storeLocked(&value) + } else { + if !read.amended { + m.dirtyLocked() + m.read.Store(readOnlyUdim{m: read.m, amended: true}) + } + m.dirty[key] = newEntryUdim(value) + } + m.mu.Unlock() +} + +func (e *entryUdim) tryStore(i *int64) bool { + for { + p := atomic.LoadPointer(&e.p) + if p == expungedUdim { + return false + } + if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) { + return true + } + } +} + +func (e *entryUdim) unexpungeLocked() (wasExpunged bool) { + return atomic.CompareAndSwapPointer(&e.p, expungedUdim, nil) +} + +func (e *entryUdim) storeLocked(i *int64) { + atomic.StorePointer(&e.p, unsafe.Pointer(i)) +} + +func (m *udim) LoadOrStore(key string, value int64) (actual int64, loaded bool) { + read, _ := m.read.Load().(readOnlyUdim) + if e, ok := read.m[key]; ok { + actual, loaded, ok := e.tryLoadOrStore(value) + if ok { + return actual, loaded + } + } + + m.mu.Lock() + read, _ = m.read.Load().(readOnlyUdim) + if e, ok := read.m[key]; ok { + if e.unexpungeLocked() { + m.dirty[key] = e + } + actual, loaded, _ = e.tryLoadOrStore(value) + } else if e, ok := m.dirty[key]; ok { + actual, loaded, _ = e.tryLoadOrStore(value) + m.missLocked() + } else { + if !read.amended { + + m.dirtyLocked() + m.read.Store(readOnlyUdim{m: read.m, amended: true}) + } + m.dirty[key] = newEntryUdim(value) + actual, loaded = value, false + } + m.mu.Unlock() + + return actual, loaded +} + +func (e *entryUdim) tryLoadOrStore(i int64) (actual int64, loaded, ok bool) { + p := atomic.LoadPointer(&e.p) + if p == expungedUdim { + return actual, false, false + } + if p != nil { + return *(*int64)(p), true, true + } + + ic := i + for { + if atomic.CompareAndSwapPointer(&e.p, nil, unsafe.Pointer(&ic)) { + return i, false, true + } + p = atomic.LoadPointer(&e.p) + if p == expungedUdim { + return actual, false, false + } + if p != nil { + return *(*int64)(p), true, true + } + } +} + +func (m *udim) LoadAndDelete(key string) (value int64, loaded bool) { + read, _ := m.read.Load().(readOnlyUdim) + e, ok := read.m[key] + if !ok && read.amended { + m.mu.Lock() + read, _ = m.read.Load().(readOnlyUdim) + e, ok = read.m[key] + if !ok && read.amended { + e, ok = m.dirty[key] + delete(m.dirty, key) + + m.missLocked() + } + m.mu.Unlock() + } + if ok { + return e.delete() + } + return value, false +} + +func (m *udim) Delete(key string) { + m.LoadAndDelete(key) +} + +func (e *entryUdim) delete() (value int64, ok bool) { + for { + p := atomic.LoadPointer(&e.p) + if p == nil || p == expungedUdim { + return value, false + } + if atomic.CompareAndSwapPointer(&e.p, p, nil) { + return *(*int64)(p), true + } + } +} + +func (m *udim) Range(f func(key string, value int64) bool) { + read, _ := m.read.Load().(readOnlyUdim) + if read.amended { + + m.mu.Lock() + read, _ = m.read.Load().(readOnlyUdim) + if read.amended { + read = readOnlyUdim{m: m.dirty} + m.read.Store(read) + m.dirty = nil + m.misses = 0 + } + m.mu.Unlock() + } + + for k, e := range read.m { + v, ok := e.load() + if !ok { + continue + } + if !f(k, v) { + break + } + } +} + +func (m *udim) missLocked() { + m.misses++ + if m.misses < len(m.dirty) { + return + } + m.read.Store(readOnlyUdim{m: m.dirty}) + m.dirty = nil + m.misses = 0 +} + +func (m *udim) dirtyLocked() { + if m.dirty != nil { + return + } + + read, _ := m.read.Load().(readOnlyUdim) + m.dirty = make(map[string]*entryUdim, len(read.m)) + for k, e := range read.m { + if !e.tryExpungeLocked() { + m.dirty[k] = e + } + } +} + +func (e *entryUdim) tryExpungeLocked() (isExpunged bool) { + p := atomic.LoadPointer(&e.p) + for p == nil { + if atomic.CompareAndSwapPointer(&e.p, nil, expungedUdim) { + return true + } + p = atomic.LoadPointer(&e.p) + } + return p == expungedUdim +} diff --git a/pkg/agent/core/ngt/service/vqueue/undeleted_index_map_test.go b/pkg/agent/core/ngt/service/vqueue/undeleted_index_map_test.go new file mode 100644 index 0000000000..c42f61eabf --- /dev/null +++ b/pkg/agent/core/ngt/service/vqueue/undeleted_index_map_test.go @@ -0,0 +1,1430 @@ +// +// Copyright (C) 2019-2021 vdaas.org vald team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package vqueue + +import ( + "reflect" + "sync" + "sync/atomic" + "testing" + "unsafe" + + "github.com/vdaas/vald/internal/errors" + "go.uber.org/goleak" +) + +func Test_newEntryUdim(t *testing.T) { + type args struct { + i int64 + } + type want struct { + want *entryUdim + } + type test struct { + name string + args args + want want + checkFunc func(want, *entryUdim) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got *entryUdim) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + i: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + i: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := newEntryUdim(test.args.i) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_udim_Load(t *testing.T) { + type args struct { + key string + } + type fields struct { + mu sync.Mutex + read atomic.Value + dirty map[string]*entryUdim + misses int + } + type want struct { + wantValue int64 + wantOk bool + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, int64, bool) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotValue int64, gotOk bool) error { + if !reflect.DeepEqual(gotValue, w.wantValue) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotValue, w.wantValue) + } + if !reflect.DeepEqual(gotOk, w.wantOk) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotOk, w.wantOk) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + key: "", + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + key: "", + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &udim{ + mu: test.fields.mu, + read: test.fields.read, + dirty: test.fields.dirty, + misses: test.fields.misses, + } + + gotValue, gotOk := m.Load(test.args.key) + if err := test.checkFunc(test.want, gotValue, gotOk); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_entryUdim_load(t *testing.T) { + type fields struct { + p unsafe.Pointer + } + type want struct { + wantValue int64 + wantOk bool + } + type test struct { + name string + fields fields + want want + checkFunc func(want, int64, bool) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, gotValue int64, gotOk bool) error { + if !reflect.DeepEqual(gotValue, w.wantValue) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotValue, w.wantValue) + } + if !reflect.DeepEqual(gotOk, w.wantOk) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotOk, w.wantOk) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + e := &entryUdim{ + p: test.fields.p, + } + + gotValue, gotOk := e.load() + if err := test.checkFunc(test.want, gotValue, gotOk); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_udim_Store(t *testing.T) { + type args struct { + key string + value int64 + } + type fields struct { + mu sync.Mutex + read atomic.Value + dirty map[string]*entryUdim + misses int + } + type want struct { + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + key: "", + value: 0, + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + key: "", + value: 0, + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &udim{ + mu: test.fields.mu, + read: test.fields.read, + dirty: test.fields.dirty, + misses: test.fields.misses, + } + + m.Store(test.args.key, test.args.value) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func Test_entryUdim_tryStore(t *testing.T) { + type args struct { + i *int64 + } + type fields struct { + p unsafe.Pointer + } + type want struct { + want bool + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, bool) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got bool) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + i: nil, + }, + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + i: nil, + }, + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + e := &entryUdim{ + p: test.fields.p, + } + + got := e.tryStore(test.args.i) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_entryUdim_unexpungeLocked(t *testing.T) { + type fields struct { + p unsafe.Pointer + } + type want struct { + wantWasExpunged bool + } + type test struct { + name string + fields fields + want want + checkFunc func(want, bool) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, gotWasExpunged bool) error { + if !reflect.DeepEqual(gotWasExpunged, w.wantWasExpunged) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotWasExpunged, w.wantWasExpunged) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + e := &entryUdim{ + p: test.fields.p, + } + + gotWasExpunged := e.unexpungeLocked() + if err := test.checkFunc(test.want, gotWasExpunged); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_entryUdim_storeLocked(t *testing.T) { + type args struct { + i *int64 + } + type fields struct { + p unsafe.Pointer + } + type want struct { + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + i: nil, + }, + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + i: nil, + }, + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + e := &entryUdim{ + p: test.fields.p, + } + + e.storeLocked(test.args.i) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func Test_udim_LoadOrStore(t *testing.T) { + type args struct { + key string + value int64 + } + type fields struct { + mu sync.Mutex + read atomic.Value + dirty map[string]*entryUdim + misses int + } + type want struct { + wantActual int64 + wantLoaded bool + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, int64, bool) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotActual int64, gotLoaded bool) error { + if !reflect.DeepEqual(gotActual, w.wantActual) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotActual, w.wantActual) + } + if !reflect.DeepEqual(gotLoaded, w.wantLoaded) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotLoaded, w.wantLoaded) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + key: "", + value: 0, + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + key: "", + value: 0, + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &udim{ + mu: test.fields.mu, + read: test.fields.read, + dirty: test.fields.dirty, + misses: test.fields.misses, + } + + gotActual, gotLoaded := m.LoadOrStore(test.args.key, test.args.value) + if err := test.checkFunc(test.want, gotActual, gotLoaded); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_entryUdim_tryLoadOrStore(t *testing.T) { + type args struct { + i int64 + } + type fields struct { + p unsafe.Pointer + } + type want struct { + wantActual int64 + wantLoaded bool + wantOk bool + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, int64, bool, bool) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotActual int64, gotLoaded bool, gotOk bool) error { + if !reflect.DeepEqual(gotActual, w.wantActual) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotActual, w.wantActual) + } + if !reflect.DeepEqual(gotLoaded, w.wantLoaded) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotLoaded, w.wantLoaded) + } + if !reflect.DeepEqual(gotOk, w.wantOk) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotOk, w.wantOk) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + i: 0, + }, + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + i: 0, + }, + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + e := &entryUdim{ + p: test.fields.p, + } + + gotActual, gotLoaded, gotOk := e.tryLoadOrStore(test.args.i) + if err := test.checkFunc(test.want, gotActual, gotLoaded, gotOk); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_udim_LoadAndDelete(t *testing.T) { + type args struct { + key string + } + type fields struct { + mu sync.Mutex + read atomic.Value + dirty map[string]*entryUdim + misses int + } + type want struct { + wantValue int64 + wantLoaded bool + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, int64, bool) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotValue int64, gotLoaded bool) error { + if !reflect.DeepEqual(gotValue, w.wantValue) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotValue, w.wantValue) + } + if !reflect.DeepEqual(gotLoaded, w.wantLoaded) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotLoaded, w.wantLoaded) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + key: "", + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + key: "", + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &udim{ + mu: test.fields.mu, + read: test.fields.read, + dirty: test.fields.dirty, + misses: test.fields.misses, + } + + gotValue, gotLoaded := m.LoadAndDelete(test.args.key) + if err := test.checkFunc(test.want, gotValue, gotLoaded); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_udim_Delete(t *testing.T) { + type args struct { + key string + } + type fields struct { + mu sync.Mutex + read atomic.Value + dirty map[string]*entryUdim + misses int + } + type want struct { + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + key: "", + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + key: "", + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &udim{ + mu: test.fields.mu, + read: test.fields.read, + dirty: test.fields.dirty, + misses: test.fields.misses, + } + + m.Delete(test.args.key) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func Test_entryUdim_delete(t *testing.T) { + type fields struct { + p unsafe.Pointer + } + type want struct { + wantValue int64 + wantOk bool + } + type test struct { + name string + fields fields + want want + checkFunc func(want, int64, bool) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, gotValue int64, gotOk bool) error { + if !reflect.DeepEqual(gotValue, w.wantValue) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotValue, w.wantValue) + } + if !reflect.DeepEqual(gotOk, w.wantOk) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotOk, w.wantOk) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + e := &entryUdim{ + p: test.fields.p, + } + + gotValue, gotOk := e.delete() + if err := test.checkFunc(test.want, gotValue, gotOk); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_udim_Range(t *testing.T) { + type args struct { + f func(key string, value int64) bool + } + type fields struct { + mu sync.Mutex + read atomic.Value + dirty map[string]*entryUdim + misses int + } + type want struct { + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + f: nil, + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + f: nil, + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &udim{ + mu: test.fields.mu, + read: test.fields.read, + dirty: test.fields.dirty, + misses: test.fields.misses, + } + + m.Range(test.args.f) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func Test_udim_missLocked(t *testing.T) { + type fields struct { + mu sync.Mutex + read atomic.Value + dirty map[string]*entryUdim + misses int + } + type want struct { + } + type test struct { + name string + fields fields + want want + checkFunc func(want) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &udim{ + mu: test.fields.mu, + read: test.fields.read, + dirty: test.fields.dirty, + misses: test.fields.misses, + } + + m.missLocked() + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func Test_udim_dirtyLocked(t *testing.T) { + type fields struct { + mu sync.Mutex + read atomic.Value + dirty map[string]*entryUdim + misses int + } + type want struct { + } + type test struct { + name string + fields fields + want want + checkFunc func(want) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &udim{ + mu: test.fields.mu, + read: test.fields.read, + dirty: test.fields.dirty, + misses: test.fields.misses, + } + + m.dirtyLocked() + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func Test_entryUdim_tryExpungeLocked(t *testing.T) { + type fields struct { + p unsafe.Pointer + } + type want struct { + wantIsExpunged bool + } + type test struct { + name string + fields fields + want want + checkFunc func(want, bool) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, gotIsExpunged bool) error { + if !reflect.DeepEqual(gotIsExpunged, w.wantIsExpunged) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotIsExpunged, w.wantIsExpunged) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + e := &entryUdim{ + p: test.fields.p, + } + + gotIsExpunged := e.tryExpungeLocked() + if err := test.checkFunc(test.want, gotIsExpunged); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} diff --git a/pkg/agent/core/ngt/service/vqueue/uninserted_index_map.go b/pkg/agent/core/ngt/service/vqueue/uninserted_index_map.go new file mode 100644 index 0000000000..fe3fa23140 --- /dev/null +++ b/pkg/agent/core/ngt/service/vqueue/uninserted_index_map.go @@ -0,0 +1,277 @@ +// +// Copyright (C) 2019-2021 vdaas.org vald team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package vqueue + +import ( + "sync" + "sync/atomic" + "unsafe" +) + +type uiim struct { + mu sync.Mutex + + read atomic.Value + + dirty map[string]*entryUiim + + misses int +} + +type readOnlyUiim struct { + m map[string]*entryUiim + amended bool +} + +var expungedUiim = unsafe.Pointer(new(index)) + +type entryUiim struct { + p unsafe.Pointer +} + +func newEntryUiim(i index) *entryUiim { + return &entryUiim{p: unsafe.Pointer(&i)} +} + +func (m *uiim) Load(key string) (value index, ok bool) { + read, _ := m.read.Load().(readOnlyUiim) + e, ok := read.m[key] + if !ok && read.amended { + m.mu.Lock() + + read, _ = m.read.Load().(readOnlyUiim) + e, ok = read.m[key] + if !ok && read.amended { + e, ok = m.dirty[key] + + m.missLocked() + } + m.mu.Unlock() + } + if !ok { + return value, false + } + return e.load() +} + +func (e *entryUiim) load() (value index, ok bool) { + p := atomic.LoadPointer(&e.p) + if p == nil || p == expungedUiim { + return value, false + } + return *(*index)(p), true +} + +func (m *uiim) Store(key string, value index) { + read, _ := m.read.Load().(readOnlyUiim) + if e, ok := read.m[key]; ok && e.tryStore(&value) { + return + } + + m.mu.Lock() + read, _ = m.read.Load().(readOnlyUiim) + if e, ok := read.m[key]; ok { + if e.unexpungeLocked() { + m.dirty[key] = e + } + e.storeLocked(&value) + } else if e, ok := m.dirty[key]; ok { + e.storeLocked(&value) + } else { + if !read.amended { + + m.dirtyLocked() + m.read.Store(readOnlyUiim{m: read.m, amended: true}) + } + m.dirty[key] = newEntryUiim(value) + } + m.mu.Unlock() +} + +func (e *entryUiim) tryStore(i *index) bool { + for { + p := atomic.LoadPointer(&e.p) + if p == expungedUiim { + return false + } + if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) { + return true + } + } +} + +func (e *entryUiim) unexpungeLocked() (wasExpunged bool) { + return atomic.CompareAndSwapPointer(&e.p, expungedUiim, nil) +} + +func (e *entryUiim) storeLocked(i *index) { + atomic.StorePointer(&e.p, unsafe.Pointer(i)) +} + +func (m *uiim) LoadOrStore(key string, value index) (actual index, loaded bool) { + read, _ := m.read.Load().(readOnlyUiim) + if e, ok := read.m[key]; ok { + actual, loaded, ok := e.tryLoadOrStore(value) + if ok { + return actual, loaded + } + } + + m.mu.Lock() + read, _ = m.read.Load().(readOnlyUiim) + if e, ok := read.m[key]; ok { + if e.unexpungeLocked() { + m.dirty[key] = e + } + actual, loaded, _ = e.tryLoadOrStore(value) + } else if e, ok := m.dirty[key]; ok { + actual, loaded, _ = e.tryLoadOrStore(value) + m.missLocked() + } else { + if !read.amended { + + m.dirtyLocked() + m.read.Store(readOnlyUiim{m: read.m, amended: true}) + } + m.dirty[key] = newEntryUiim(value) + actual, loaded = value, false + } + m.mu.Unlock() + + return actual, loaded +} + +func (e *entryUiim) tryLoadOrStore(i index) (actual index, loaded, ok bool) { + p := atomic.LoadPointer(&e.p) + if p == expungedUiim { + return actual, false, false + } + if p != nil { + return *(*index)(p), true, true + } + + ic := i + for { + if atomic.CompareAndSwapPointer(&e.p, nil, unsafe.Pointer(&ic)) { + return i, false, true + } + p = atomic.LoadPointer(&e.p) + if p == expungedUiim { + return actual, false, false + } + if p != nil { + return *(*index)(p), true, true + } + } +} + +func (m *uiim) LoadAndDelete(key string) (value index, loaded bool) { + read, _ := m.read.Load().(readOnlyUiim) + e, ok := read.m[key] + if !ok && read.amended { + m.mu.Lock() + read, _ = m.read.Load().(readOnlyUiim) + e, ok = read.m[key] + if !ok && read.amended { + e, ok = m.dirty[key] + delete(m.dirty, key) + + m.missLocked() + } + m.mu.Unlock() + } + if ok { + return e.delete() + } + return value, false +} + +func (m *uiim) Delete(key string) { + m.LoadAndDelete(key) +} + +func (e *entryUiim) delete() (value index, ok bool) { + for { + p := atomic.LoadPointer(&e.p) + if p == nil || p == expungedUiim { + return value, false + } + if atomic.CompareAndSwapPointer(&e.p, p, nil) { + return *(*index)(p), true + } + } +} + +func (m *uiim) Range(f func(key string, value index) bool) { + read, _ := m.read.Load().(readOnlyUiim) + if read.amended { + + m.mu.Lock() + read, _ = m.read.Load().(readOnlyUiim) + if read.amended { + read = readOnlyUiim{m: m.dirty} + m.read.Store(read) + m.dirty = nil + m.misses = 0 + } + m.mu.Unlock() + } + + for k, e := range read.m { + v, ok := e.load() + if !ok { + continue + } + if !f(k, v) { + break + } + } +} + +func (m *uiim) missLocked() { + m.misses++ + if m.misses < len(m.dirty) { + return + } + m.read.Store(readOnlyUiim{m: m.dirty}) + m.dirty = nil + m.misses = 0 +} + +func (m *uiim) dirtyLocked() { + if m.dirty != nil { + return + } + + read, _ := m.read.Load().(readOnlyUiim) + m.dirty = make(map[string]*entryUiim, len(read.m)) + for k, e := range read.m { + if !e.tryExpungeLocked() { + m.dirty[k] = e + } + } +} + +func (e *entryUiim) tryExpungeLocked() (isExpunged bool) { + p := atomic.LoadPointer(&e.p) + for p == nil { + if atomic.CompareAndSwapPointer(&e.p, nil, expungedUiim) { + return true + } + p = atomic.LoadPointer(&e.p) + } + return p == expungedUiim +} diff --git a/pkg/agent/core/ngt/service/vqueue/uninserted_index_map_test.go b/pkg/agent/core/ngt/service/vqueue/uninserted_index_map_test.go new file mode 100644 index 0000000000..8412c524ac --- /dev/null +++ b/pkg/agent/core/ngt/service/vqueue/uninserted_index_map_test.go @@ -0,0 +1,1430 @@ +// +// Copyright (C) 2019-2021 vdaas.org vald team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package vqueue + +import ( + "reflect" + "sync" + "sync/atomic" + "testing" + "unsafe" + + "github.com/vdaas/vald/internal/errors" + "go.uber.org/goleak" +) + +func Test_newEntryUiim(t *testing.T) { + type args struct { + i index + } + type want struct { + want *entryUiim + } + type test struct { + name string + args args + want want + checkFunc func(want, *entryUiim) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got *entryUiim) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + i: index{}, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + i: index{}, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := newEntryUiim(test.args.i) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_uiim_Load(t *testing.T) { + type args struct { + key string + } + type fields struct { + mu sync.Mutex + read atomic.Value + dirty map[string]*entryUiim + misses int + } + type want struct { + wantValue index + wantOk bool + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, index, bool) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotValue index, gotOk bool) error { + if !reflect.DeepEqual(gotValue, w.wantValue) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotValue, w.wantValue) + } + if !reflect.DeepEqual(gotOk, w.wantOk) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotOk, w.wantOk) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + key: "", + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + key: "", + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &uiim{ + mu: test.fields.mu, + read: test.fields.read, + dirty: test.fields.dirty, + misses: test.fields.misses, + } + + gotValue, gotOk := m.Load(test.args.key) + if err := test.checkFunc(test.want, gotValue, gotOk); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_entryUiim_load(t *testing.T) { + type fields struct { + p unsafe.Pointer + } + type want struct { + wantValue index + wantOk bool + } + type test struct { + name string + fields fields + want want + checkFunc func(want, index, bool) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, gotValue index, gotOk bool) error { + if !reflect.DeepEqual(gotValue, w.wantValue) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotValue, w.wantValue) + } + if !reflect.DeepEqual(gotOk, w.wantOk) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotOk, w.wantOk) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + e := &entryUiim{ + p: test.fields.p, + } + + gotValue, gotOk := e.load() + if err := test.checkFunc(test.want, gotValue, gotOk); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_uiim_Store(t *testing.T) { + type args struct { + key string + value index + } + type fields struct { + mu sync.Mutex + read atomic.Value + dirty map[string]*entryUiim + misses int + } + type want struct { + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + key: "", + value: index{}, + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + key: "", + value: index{}, + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &uiim{ + mu: test.fields.mu, + read: test.fields.read, + dirty: test.fields.dirty, + misses: test.fields.misses, + } + + m.Store(test.args.key, test.args.value) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func Test_entryUiim_tryStore(t *testing.T) { + type args struct { + i *index + } + type fields struct { + p unsafe.Pointer + } + type want struct { + want bool + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, bool) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got bool) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + i: index{}, + }, + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + i: index{}, + }, + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + e := &entryUiim{ + p: test.fields.p, + } + + got := e.tryStore(test.args.i) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_entryUiim_unexpungeLocked(t *testing.T) { + type fields struct { + p unsafe.Pointer + } + type want struct { + wantWasExpunged bool + } + type test struct { + name string + fields fields + want want + checkFunc func(want, bool) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, gotWasExpunged bool) error { + if !reflect.DeepEqual(gotWasExpunged, w.wantWasExpunged) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotWasExpunged, w.wantWasExpunged) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + e := &entryUiim{ + p: test.fields.p, + } + + gotWasExpunged := e.unexpungeLocked() + if err := test.checkFunc(test.want, gotWasExpunged); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_entryUiim_storeLocked(t *testing.T) { + type args struct { + i *index + } + type fields struct { + p unsafe.Pointer + } + type want struct { + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + i: index{}, + }, + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + i: index{}, + }, + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + e := &entryUiim{ + p: test.fields.p, + } + + e.storeLocked(test.args.i) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func Test_uiim_LoadOrStore(t *testing.T) { + type args struct { + key string + value index + } + type fields struct { + mu sync.Mutex + read atomic.Value + dirty map[string]*entryUiim + misses int + } + type want struct { + wantActual index + wantLoaded bool + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, index, bool) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotActual index, gotLoaded bool) error { + if !reflect.DeepEqual(gotActual, w.wantActual) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotActual, w.wantActual) + } + if !reflect.DeepEqual(gotLoaded, w.wantLoaded) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotLoaded, w.wantLoaded) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + key: "", + value: index{}, + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + key: "", + value: index{}, + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &uiim{ + mu: test.fields.mu, + read: test.fields.read, + dirty: test.fields.dirty, + misses: test.fields.misses, + } + + gotActual, gotLoaded := m.LoadOrStore(test.args.key, test.args.value) + if err := test.checkFunc(test.want, gotActual, gotLoaded); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_entryUiim_tryLoadOrStore(t *testing.T) { + type args struct { + i index + } + type fields struct { + p unsafe.Pointer + } + type want struct { + wantActual index + wantLoaded bool + wantOk bool + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, index, bool, bool) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotActual index, gotLoaded bool, gotOk bool) error { + if !reflect.DeepEqual(gotActual, w.wantActual) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotActual, w.wantActual) + } + if !reflect.DeepEqual(gotLoaded, w.wantLoaded) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotLoaded, w.wantLoaded) + } + if !reflect.DeepEqual(gotOk, w.wantOk) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotOk, w.wantOk) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + i: index{}, + }, + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + i: index{}, + }, + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + e := &entryUiim{ + p: test.fields.p, + } + + gotActual, gotLoaded, gotOk := e.tryLoadOrStore(test.args.i) + if err := test.checkFunc(test.want, gotActual, gotLoaded, gotOk); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_uiim_LoadAndDelete(t *testing.T) { + type args struct { + key string + } + type fields struct { + mu sync.Mutex + read atomic.Value + dirty map[string]*entryUiim + misses int + } + type want struct { + wantValue index + wantLoaded bool + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, index, bool) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotValue index, gotLoaded bool) error { + if !reflect.DeepEqual(gotValue, w.wantValue) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotValue, w.wantValue) + } + if !reflect.DeepEqual(gotLoaded, w.wantLoaded) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotLoaded, w.wantLoaded) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + key: "", + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + key: "", + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &uiim{ + mu: test.fields.mu, + read: test.fields.read, + dirty: test.fields.dirty, + misses: test.fields.misses, + } + + gotValue, gotLoaded := m.LoadAndDelete(test.args.key) + if err := test.checkFunc(test.want, gotValue, gotLoaded); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_uiim_Delete(t *testing.T) { + type args struct { + key string + } + type fields struct { + mu sync.Mutex + read atomic.Value + dirty map[string]*entryUiim + misses int + } + type want struct { + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + key: "", + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + key: "", + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &uiim{ + mu: test.fields.mu, + read: test.fields.read, + dirty: test.fields.dirty, + misses: test.fields.misses, + } + + m.Delete(test.args.key) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func Test_entryUiim_delete(t *testing.T) { + type fields struct { + p unsafe.Pointer + } + type want struct { + wantValue index + wantOk bool + } + type test struct { + name string + fields fields + want want + checkFunc func(want, index, bool) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, gotValue index, gotOk bool) error { + if !reflect.DeepEqual(gotValue, w.wantValue) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotValue, w.wantValue) + } + if !reflect.DeepEqual(gotOk, w.wantOk) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotOk, w.wantOk) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + e := &entryUiim{ + p: test.fields.p, + } + + gotValue, gotOk := e.delete() + if err := test.checkFunc(test.want, gotValue, gotOk); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_uiim_Range(t *testing.T) { + type args struct { + f func(key string, value index) bool + } + type fields struct { + mu sync.Mutex + read atomic.Value + dirty map[string]*entryUiim + misses int + } + type want struct { + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + f: nil, + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + f: nil, + }, + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &uiim{ + mu: test.fields.mu, + read: test.fields.read, + dirty: test.fields.dirty, + misses: test.fields.misses, + } + + m.Range(test.args.f) + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func Test_uiim_missLocked(t *testing.T) { + type fields struct { + mu sync.Mutex + read atomic.Value + dirty map[string]*entryUiim + misses int + } + type want struct { + } + type test struct { + name string + fields fields + want want + checkFunc func(want) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &uiim{ + mu: test.fields.mu, + read: test.fields.read, + dirty: test.fields.dirty, + misses: test.fields.misses, + } + + m.missLocked() + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func Test_uiim_dirtyLocked(t *testing.T) { + type fields struct { + mu sync.Mutex + read atomic.Value + dirty map[string]*entryUiim + misses int + } + type want struct { + } + type test struct { + name string + fields fields + want want + checkFunc func(want) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want) error { + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + mu: sync.Mutex{}, + read: nil, + dirty: nil, + misses: 0, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + m := &uiim{ + mu: test.fields.mu, + read: test.fields.read, + dirty: test.fields.dirty, + misses: test.fields.misses, + } + + m.dirtyLocked() + if err := test.checkFunc(test.want); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func Test_entryUiim_tryExpungeLocked(t *testing.T) { + type fields struct { + p unsafe.Pointer + } + type want struct { + wantIsExpunged bool + } + type test struct { + name string + fields fields + want want + checkFunc func(want, bool) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, gotIsExpunged bool) error { + if !reflect.DeepEqual(gotIsExpunged, w.wantIsExpunged) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotIsExpunged, w.wantIsExpunged) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + fields: fields { + p: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + e := &entryUiim{ + p: test.fields.p, + } + + gotIsExpunged := e.tryExpungeLocked() + if err := test.checkFunc(test.want, gotIsExpunged); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} diff --git a/pkg/agent/sidecar/service/restorer/restorer_test.go b/pkg/agent/sidecar/service/restorer/restorer_test.go index dfdbbc1209..601a4a0075 100644 --- a/pkg/agent/sidecar/service/restorer/restorer_test.go +++ b/pkg/agent/sidecar/service/restorer/restorer_test.go @@ -19,12 +19,14 @@ package restorer import ( "context" + "io/fs" "reflect" "testing" "github.com/vdaas/vald/internal/backoff" "github.com/vdaas/vald/internal/errgroup" "github.com/vdaas/vald/internal/errors" + "github.com/vdaas/vald/internal/io" "github.com/vdaas/vald/pkg/agent/sidecar/service/storage" "go.uber.org/goleak" ) @@ -431,3 +433,191 @@ func Test_restorer_restore(t *testing.T) { }) } } + +func Test_restorer_PreStop(t *testing.T) { + type args struct { + ctx context.Context + } + type fields struct { + dir string + eg errgroup.Group + storage storage.Storage + backoffEnabled bool + backoffOpts []backoff.Option + bo backoff.Backoff + } + type want struct { + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, err error) error { + if !errors.Is(err, w.err) { + return errors.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 { + ctx: nil, + }, + fields: fields { + dir: "", + eg: nil, + storage: nil, + backoffEnabled: false, + backoffOpts: nil, + bo: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + }, + fields: fields { + dir: "", + eg: nil, + storage: nil, + backoffEnabled: false, + backoffOpts: nil, + bo: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + r := &restorer{ + dir: test.fields.dir, + eg: test.fields.eg, + storage: test.fields.storage, + backoffEnabled: test.fields.backoffEnabled, + backoffOpts: test.fields.backoffOpts, + bo: test.fields.bo, + } + + err := r.PreStop(test.args.ctx) + if err := test.checkFunc(test.want, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_copyFile(t *testing.T) { + type args struct { + ctx context.Context + target string + tr io.Reader + mode fs.FileMode + } + type want struct { + err error + } + type test struct { + name string + args args + want want + checkFunc func(want, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, err error) error { + if !errors.Is(err, w.err) { + return errors.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 { + ctx: nil, + target: "", + tr: nil, + mode: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + target: "", + tr: nil, + mode: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + err := copyFile(test.args.ctx, test.args.target, test.args.tr, test.args.mode) + if err := test.checkFunc(test.want, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} diff --git a/pkg/agent/sidecar/service/storage/option_test.go b/pkg/agent/sidecar/service/storage/option_test.go index 586fcf4660..bc0d0f826b 100644 --- a/pkg/agent/sidecar/service/storage/option_test.go +++ b/pkg/agent/sidecar/service/storage/option_test.go @@ -20,6 +20,8 @@ package storage import ( "testing" + "github.com/vdaas/vald/internal/db/storage/blob/cloudstorage" + "github.com/vdaas/vald/internal/db/storage/blob/cloudstorage/urlopener" "github.com/vdaas/vald/internal/db/storage/blob/s3" "github.com/vdaas/vald/internal/db/storage/blob/s3/session" "github.com/vdaas/vald/internal/errgroup" @@ -1078,3 +1080,235 @@ func TestWithCompressionLevel(t *testing.T) { }) } } + +func TestWithCloudStorageOpts(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + opts []cloudstorage.Option + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + opts: nil, + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + opts: nil, + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithCloudStorageOpts(test.args.opts...) + obj := new(T) + if err := test.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 := WithCloudStorageOpts(test.args.opts...) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithCloudStorageURLOpenerOpts(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + opts []urlopener.Option + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + opts: nil, + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + opts: nil, + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithCloudStorageURLOpenerOpts(test.args.opts...) + obj := new(T) + if err := test.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 := WithCloudStorageURLOpenerOpts(test.args.opts...) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} diff --git a/pkg/agent/sidecar/service/storage/storage_test.go b/pkg/agent/sidecar/service/storage/storage_test.go index c8fb578140..a9f64da309 100644 --- a/pkg/agent/sidecar/service/storage/storage_test.go +++ b/pkg/agent/sidecar/service/storage/storage_test.go @@ -24,6 +24,8 @@ import ( "github.com/vdaas/vald/internal/compress" "github.com/vdaas/vald/internal/db/storage/blob" + "github.com/vdaas/vald/internal/db/storage/blob/cloudstorage" + "github.com/vdaas/vald/internal/db/storage/blob/cloudstorage/urlopener" "github.com/vdaas/vald/internal/db/storage/blob/s3" "github.com/vdaas/vald/internal/db/storage/blob/s3/session" "github.com/vdaas/vald/internal/errgroup" @@ -850,3 +852,137 @@ func Test_bs_StorageInfo(t *testing.T) { }) } } + +func Test_bs_Stop(t *testing.T) { + type args struct { + ctx context.Context + } + type fields struct { + eg errgroup.Group + storageType string + bucketName string + filename string + suffix string + s3Opts []s3.Option + s3SessionOpts []session.Option + cloudStorageOpts []cloudstorage.Option + cloudStorageURLOpenerOpts []urlopener.Option + compressAlgorithm string + compressionLevel int + bucket blob.Bucket + compressor compress.Compressor + } + type want struct { + err error + } + type test struct { + name string + args args + fields fields + want want + checkFunc func(want, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, err error) error { + if !errors.Is(err, w.err) { + return errors.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 { + ctx: nil, + }, + fields: fields { + eg: nil, + storageType: "", + bucketName: "", + filename: "", + suffix: "", + s3Opts: nil, + s3SessionOpts: nil, + cloudStorageOpts: nil, + cloudStorageURLOpenerOpts: nil, + compressAlgorithm: "", + compressionLevel: 0, + bucket: nil, + compressor: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ctx: nil, + }, + fields: fields { + eg: nil, + storageType: "", + bucketName: "", + filename: "", + suffix: "", + s3Opts: nil, + s3SessionOpts: nil, + cloudStorageOpts: nil, + cloudStorageURLOpenerOpts: nil, + compressAlgorithm: "", + compressionLevel: 0, + bucket: nil, + compressor: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + b := &bs{ + eg: test.fields.eg, + storageType: test.fields.storageType, + bucketName: test.fields.bucketName, + filename: test.fields.filename, + suffix: test.fields.suffix, + s3Opts: test.fields.s3Opts, + s3SessionOpts: test.fields.s3SessionOpts, + cloudStorageOpts: test.fields.cloudStorageOpts, + cloudStorageURLOpenerOpts: test.fields.cloudStorageURLOpenerOpts, + compressAlgorithm: test.fields.compressAlgorithm, + compressionLevel: test.fields.compressionLevel, + bucket: test.fields.bucket, + compressor: test.fields.compressor, + } + + err := b.Stop(test.args.ctx) + if err := test.checkFunc(test.want, err); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} diff --git a/pkg/discoverer/k8s/handler/grpc/option_test.go b/pkg/discoverer/k8s/handler/grpc/option_test.go index fa944e011b..251900af93 100644 --- a/pkg/discoverer/k8s/handler/grpc/option_test.go +++ b/pkg/discoverer/k8s/handler/grpc/option_test.go @@ -140,3 +140,235 @@ func TestWithDiscoverer(t *testing.T) { }) } } + +func TestWithName(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + name string + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + name: "", + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + name: "", + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithName(test.args.name) + obj := new(T) + if err := test.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 := WithName(test.args.name) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithIP(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + ip string + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + ip: "", + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ip: "", + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithIP(test.args.ip) + obj := new(T) + if err := test.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 := WithIP(test.args.ip) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} diff --git a/pkg/gateway/lb/handler/grpc/option_test.go b/pkg/gateway/lb/handler/grpc/option_test.go index 028cf9b44f..a44d383539 100644 --- a/pkg/gateway/lb/handler/grpc/option_test.go +++ b/pkg/gateway/lb/handler/grpc/option_test.go @@ -609,3 +609,235 @@ func TestWithStreamConcurrency(t *testing.T) { }) } } + +func TestWithIP(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + ip string + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + ip: "", + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ip: "", + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithIP(test.args.ip) + obj := new(T) + if err := test.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 := WithIP(test.args.ip) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithName(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + name string + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + name: "", + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + name: "", + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithName(test.args.name) + obj := new(T) + if err := test.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 := WithName(test.args.name) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} diff --git a/pkg/gateway/meta/handler/grpc/option_test.go b/pkg/gateway/meta/handler/grpc/option_test.go index 8a9b6e8c05..31ebfdbe70 100644 --- a/pkg/gateway/meta/handler/grpc/option_test.go +++ b/pkg/gateway/meta/handler/grpc/option_test.go @@ -493,3 +493,235 @@ func TestWithStreamConcurrency(t *testing.T) { }) } } + +func TestWithIP(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + ip string + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + ip: "", + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + ip: "", + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithIP(test.args.ip) + obj := new(T) + if err := test.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 := WithIP(test.args.ip) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} + +func TestWithName(t *testing.T) { + // Change interface type to the type of object you are testing + type T = interface{} + type args struct { + name string + } + type want struct { + obj *T + // Uncomment this line if the option returns an error, otherwise delete it + // err error + } + 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) + } + + // 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 + /* + { + name: "test_case_1", + args: args { + name: "", + }, + want: want { + obj: new(T), + }, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + name: "", + }, + want: want { + obj: new(T), + }, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + + // Uncomment this block if the option returns an error, otherwise delete it + /* + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := WithName(test.args.name) + obj := new(T) + if err := test.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 := WithName(test.args.name) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } + */ + }) + } +} diff --git a/pkg/meta/cassandra/handler/grpc/handler_test.go b/pkg/meta/cassandra/handler/grpc/handler_test.go index e150264f02..addd067875 100644 --- a/pkg/meta/cassandra/handler/grpc/handler_test.go +++ b/pkg/meta/cassandra/handler/grpc/handler_test.go @@ -25,6 +25,7 @@ import ( "github.com/vdaas/vald/apis/grpc/v1/meta" "github.com/vdaas/vald/apis/grpc/v1/payload" "github.com/vdaas/vald/internal/errors" + "github.com/vdaas/vald/internal/net/grpc/errdetails" "github.com/vdaas/vald/pkg/meta/cassandra/service" "go.uber.org/goleak" ) @@ -1031,3 +1032,222 @@ func Test_server_DeleteMetasInverse(t *testing.T) { }) } } + +func Test_isInternalError(t *testing.T) { + type args struct { + err error + } + type want struct { + want bool + } + type test struct { + name string + args args + want want + checkFunc func(want, bool) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got bool) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + err: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + err: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := isInternalError(test.args.err) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_isFailedPrecondition(t *testing.T) { + type args struct { + err error + } + type want struct { + want bool + } + type test struct { + name string + args args + want want + checkFunc func(want, bool) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got bool) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + err: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + err: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := isFailedPrecondition(test.args.err) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_getPreconditionFailureDetails(t *testing.T) { + type args struct { + err error + } + type want struct { + wantRes []*errdetails.PreconditionFailureViolation + } + type test struct { + name string + args args + want want + checkFunc func(want, []*errdetails.PreconditionFailureViolation) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, gotRes []*errdetails.PreconditionFailureViolation) error { + if !reflect.DeepEqual(gotRes, w.wantRes) { + return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotRes, w.wantRes) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + err: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + err: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + 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(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + gotRes := getPreconditionFailureDetails(test.args.err) + if err := test.checkFunc(test.want, gotRes); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +}