diff --git a/internal/cache/cache.go b/internal/cache/cache.go index 49ed3ae4616..92231dae930 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -26,6 +26,7 @@ import ( "github.com/vdaas/vald/internal/errors" ) +// Cache represent the cache interface to store cache type Cache interface { Start(context.Context) Get(string) (interface{}, bool) @@ -41,6 +42,7 @@ type cache struct { expiredHook func(context.Context, string) } +// New returns the Cache instance or error func New(opts ...Option) (cc Cache, err error) { c := new(cache) for _, opt := range append(defaultOpts, opts...) { diff --git a/internal/cache/cache_test.go b/internal/cache/cache_test.go index 1e37531b4d5..8adff649055 100644 --- a/internal/cache/cache_test.go +++ b/internal/cache/cache_test.go @@ -26,6 +26,13 @@ import ( "go.uber.org/goleak" ) +var ( + // Goroutine leak is detected by `fastime`, but it should be ignored in the test because it is an external package. + goleakIgnoreOptions = []goleak.Option{ + goleak.IgnoreTopFunction("github.com/kpango/fastime.(*Fastime).StartTimerD.func1"), + } +) + func TestNew(t *testing.T) { type args struct { opts []Option @@ -52,36 +59,36 @@ func TestNew(t *testing.T) { return nil } tests := []test{ - // TODO test cases - /* - { - name: "test_case_1", - args: args { - opts: nil, - }, - want: want{}, - checkFunc: defaultCheckFunc, - }, - */ + { + name: "return gache cacher", + args: args{ + opts: []Option{WithType("gache")}, + }, + checkFunc: func(w want, got Cache, err error) error { + if err != nil { + return err + } + if got == nil { + return errors.New("got cache is nil") + } - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - args: args { - opts: nil, - }, - want: want{}, - checkFunc: defaultCheckFunc, - } - }(), - */ + return nil + }, + }, + { + name: "return unknown error", + args: args{ + opts: []Option{WithType("unknown")}, + }, + want: want{ + err: errors.ErrInvalidCacherType, + }, + }, } for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(t, goleakIgnoreOptions...) if test.beforeFunc != nil { test.beforeFunc(test.args) }