diff --git a/internal/safety/safety_test.go b/internal/safety/safety_test.go index 94c37edbfad..d51fb4ea071 100644 --- a/internal/safety/safety_test.go +++ b/internal/safety/safety_test.go @@ -16,6 +16,7 @@ package safety import ( + "reflect" "testing" "github.com/vdaas/vald/internal/errors" @@ -159,3 +160,148 @@ func TestRecoverFunc(t *testing.T) { }) } } + +func TestRecoverWithoutPanicFunc(t *testing.T) { + type args struct { + fn func() error + } + type want struct { + want func() error + } + type test struct { + name string + args args + want want + checkFunc func(want, func() error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got func() error) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got = %v, want %v", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + fn: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + fn: nil, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt) + 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 := RecoverWithoutPanicFunc(test.args.fn) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +} + +func Test_recoverFunc(t *testing.T) { + type args struct { + fn func() error + withPanic bool + } + type want struct { + want func() error + } + type test struct { + name string + args args + want want + checkFunc func(want, func() error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got func() error) error { + if !reflect.DeepEqual(got, w.want) { + return errors.Errorf("got = %v, want %v", got, w.want) + } + return nil + } + tests := []test{ + // TODO test cases + /* + { + name: "test_case_1", + args: args { + fn: nil, + withPanic: false, + }, + want: want{}, + checkFunc: defaultCheckFunc, + }, + */ + + // TODO test cases + /* + func() test { + return test { + name: "test_case_2", + args: args { + fn: nil, + withPanic: false, + }, + want: want{}, + checkFunc: defaultCheckFunc, + } + }(), + */ + } + + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + defer goleak.VerifyNone(tt) + 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 := recoverFunc(test.args.fn, test.args.withPanic) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + + }) + } +}