From d4fa50b03a1d5bf134582536b9765fe2fceacb28 Mon Sep 17 00:00:00 2001 From: Kiichiro YUKAWA Date: Fri, 7 Aug 2020 10:57:50 +0900 Subject: [PATCH] :white_check_mark: Add test of internal/worker/queue_option (#613) * :white_check_mark: Add test of internal/worker/queue_option Signed-off-by: vankichi * :white_check_mark: Update internal/worker/queue_option_test.go Co-authored-by: Hiroto Funakoshi * :white_check_mark: Update internal/worker/queue_option_test.go Co-authored-by: Hiroto Funakoshi Co-authored-by: Hiroto Funakoshi --- internal/worker/queue_option.go | 5 + internal/worker/queue_option_test.go | 406 ++++++++++----------------- 2 files changed, 153 insertions(+), 258 deletions(-) diff --git a/internal/worker/queue_option.go b/internal/worker/queue_option.go index 706ce84ed3..78ea4c353a 100644 --- a/internal/worker/queue_option.go +++ b/internal/worker/queue_option.go @@ -22,6 +22,7 @@ import ( "github.com/vdaas/vald/internal/timeutil" ) +// QueueOption represents the functional option for queue. type QueueOption func(q *queue) error var ( @@ -32,6 +33,7 @@ var ( } ) +// WithQueueBuffer returns the option to set the buffer for queue. func WithQueueBuffer(buffer int) QueueOption { return func(q *queue) error { if buffer > 0 { @@ -41,6 +43,7 @@ func WithQueueBuffer(buffer int) QueueOption { } } +// WithQueueErrGroup returns the options to set the eg for queue. func WithQueueErrGroup(eg errgroup.Group) QueueOption { return func(q *queue) error { if eg != nil { @@ -50,6 +53,8 @@ func WithQueueErrGroup(eg errgroup.Group) QueueOption { } } +// WithQueueCheckDuration returns the option to set the qcdur for queue. +// If dur is invalid string, it returns errror. func WithQueueCheckDuration(dur string) QueueOption { return func(q *queue) error { if len(dur) == 0 { diff --git a/internal/worker/queue_option_test.go b/internal/worker/queue_option_test.go index e08e1ccca4..af1f5d3528 100644 --- a/internal/worker/queue_option_test.go +++ b/internal/worker/queue_option_test.go @@ -18,348 +18,238 @@ package worker import ( + "reflect" "testing" + "time" - "github.com/vdaas/vald/internal/errgroup" + stderrors "errors" + "github.com/vdaas/vald/internal/errgroup" + "github.com/vdaas/vald/internal/errors" "go.uber.org/goleak" ) func TestWithQueueBuffer(t *testing.T) { - type T = interface{} + type T = queue type args struct { buffer int } type want struct { obj *T - // Uncomment this line if the option returns an error, otherwise delete it - // err error + 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 + name string + args args + want want + checkFunc func(want, *T, error) 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, want %v", err, w.err) - } - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %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, want %v", obj, w.c) - } - return nil - } - */ + defaultCheckFunc := func(w want, got *T, err error) error { + if !errors.Is(err, w.err) { + return errors.Errorf("got error = %v, want %v", err, w.err) + } + if !reflect.DeepEqual(got, w.obj) { + return errors.Errorf("got = %v, want %v", got, w.obj) + } + return nil + } tests := []test{ - // TODO test cases - /* - { - name: "test_case_1", - args: args { - buffer: 0, - }, - want: want { - obj: new(T), - }, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - args: args { - buffer: 0, - }, - want: want { - obj: new(T), - }, - } - }(), - */ + { + name: "set success when buffer is more than 0.", + args: args{ + buffer: 20, + }, + want: want{ + obj: &queue{ + buffer: 20, + }, + }, + }, + { + name: "set success when buffer is less than 0.", + args: args{ + buffer: -1, + }, + want: want{ + obj: &queue{ + buffer: 0, + }, + }, + }, } for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) 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 := WithQueueBuffer(test.args.buffer) - 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 returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - got := WithQueueBuffer(test.args.buffer) - obj := new(T) - got(obj) - if err := test.checkFunc(tt.want, obj); err != nil { - tt.Errorf("error = %v", err) - } - */ + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + got := WithQueueBuffer(test.args.buffer) + obj := new(T) + if err := test.checkFunc(test.want, obj, got(obj)); err != nil { + tt.Errorf("error = %v", err) + } }) } } func TestWithQueueErrGroup(t *testing.T) { - type T = interface{} + type T = queue type args struct { eg errgroup.Group } type want struct { obj *T - // Uncomment this line if the option returns an error, otherwise delete it - // err error + 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 + name string + args args + want want + checkFunc func(want, *T, error) 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, want %v", err, w.err) - } - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %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, want %v", obj, w.c) - } - return nil - } - */ + defaultCheckFunc := func(w want, got *T, err error) error { + if !errors.Is(err, w.err) { + return errors.Errorf("got error = %v, want %v", err, w.err) + } + if !reflect.DeepEqual(got, w.obj) { + return errors.Errorf("got = %v, want %v", got, w.obj) + } + return nil + } tests := []test{ - // TODO test cases - /* - { - name: "test_case_1", - args: args { - eg: nil, - }, - want: want { - obj: new(T), - }, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - args: args { - eg: nil, - }, - want: want { - obj: new(T), - }, - } - }(), - */ + { + name: "set success when eg is not nil", + args: args{ + eg: errgroup.Get(), + }, + want: want{ + obj: &T{ + eg: errgroup.Get(), + }, + }, + }, { + name: "set success when eg is nil", + args: args{ + eg: nil, + }, + want: want{ + obj: new(T), + }, + }, } for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) 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 := WithQueueErrGroup(test.args.eg) - 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 returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - got := WithQueueErrGroup(test.args.eg) - obj := new(T) - got(obj) - if err := test.checkFunc(tt.want, obj); err != nil { - tt.Errorf("error = %v", err) - } - */ + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + got := WithQueueErrGroup(test.args.eg) + obj := new(T) + if err := test.checkFunc(test.want, obj, got(obj)); err != nil { + tt.Errorf("error = %v", err) + } }) } } func TestWithQueueCheckDuration(t *testing.T) { - type T = interface{} + type T = queue type args struct { dur string } type want struct { obj *T - // Uncomment this line if the option returns an error, otherwise delete it - // err error + 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 + name string + args args + want want + checkFunc func(want, *T, error) 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, want %v", err, w.err) - } - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %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, want %v", obj, w.c) - } - return nil - } - */ + defaultCheckFunc := func(w want, obj *T, err error) error { + if !errors.Is(err, w.err) { + return errors.Errorf("got error = %v, want = %v", err, w.err) + } + if !reflect.DeepEqual(obj, w.obj) { + return errors.Errorf("got = %v, want = %v", obj, w.obj) + } + return nil + } tests := []test{ - // TODO test cases - /* - { - name: "test_case_1", - args: args { - dur: "", - }, - want: want { - obj: new(T), - }, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - args: args { - dur: "", - }, - want: want { - obj: new(T), - }, - } - }(), - */ + { + name: "set success when dur is empty.", + args: args{ + dur: "", + }, + want: want{ + obj: new(T), + }, + }, + { + name: "set success when dur is 100ms.", + args: args{ + dur: "100ms", + }, + want: want{ + obj: &T{ + qcdur: 100 * time.Millisecond, + }, + }, + }, + { + name: "return error when success when dur is invalid.", + args: args{ + dur: "invalid", + }, + want: want{ + obj: new(T), + err: stderrors.New("time: invalid duration invalid"), + }, + }, } for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt, goleakIgnoreOptions...) 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 := WithQueueCheckDuration(test.args.dur) - 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 returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - got := WithQueueCheckDuration(test.args.dur) - obj := new(T) - got(obj) - if err := test.checkFunc(tt.want, obj); err != nil { - tt.Errorf("error = %v", err) - } - */ + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + got := WithQueueCheckDuration(test.args.dur) + obj := new(T) + if err := test.checkFunc(test.want, obj, got(obj)); err != nil { + tt.Errorf("error = %v", err) + } }) } }