Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cache test #576

Merged
merged 3 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -41,13 +42,11 @@ 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...) {
err = opt(c)
if err != nil {
return nil, err
}
opt(c)
}
switch c.cacher {
case cacher.GACHE:
Expand Down
82 changes: 57 additions & 25 deletions internal/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[golangci] reported by reviewdog 🐶
goleakIgnoreOptions is a global variable (gochecknoglobals)

goleak.IgnoreTopFunction("github.com/kpango/fastime.(*Fastime).StartTimerD.func1"),
}
)

func TestNew(t *testing.T) {
type args struct {
opts []Option
Expand All @@ -52,36 +59,61 @@ 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")
}

return nil
},
},
{
name: "return unknown error when type is unknown",
args: args{
opts: []Option{WithType("unknown")},
},
want: want{
err: errors.ErrInvalidCacherType,
},
},
{
name: "return cache when type is empty",
args: args{
opts: []Option{WithType("")},
},
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 when type is dummy string",
args: args{
opts: []Option{WithType("dummy")},
},
want: want{
err: errors.ErrInvalidCacherType,
},
},
}

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)
}
Expand Down
32 changes: 12 additions & 20 deletions internal/cache/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ import (
"context"

"github.com/vdaas/vald/internal/cache/cacher"
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/timeutil"
)

type Option func(*cache) error
type Option func(*cache)

var (
defaultOpts = []Option{
Expand All @@ -36,52 +35,45 @@ var (
)

func WithExpiredHook(f func(context.Context, string)) Option {
return func(c *cache) error {
return func(c *cache) {
if f != nil {
c.expiredHook = f
}
return nil
}
}

func WithType(mo string) Option {
return func(c *cache) error {
return func(c *cache) {
if len(mo) == 0 {
kevindiu marked this conversation as resolved.
Show resolved Hide resolved
return nil
return
}
m := cacher.ToType(mo)
if m == cacher.Unknown {
return errors.ErrInvalidCacherType
}
c.cacher = m
return nil

c.cacher = cacher.ToType(mo)
}
}

func WithExpireDuration(dur string) Option {
return func(c *cache) error {
return func(c *cache) {
if len(dur) == 0 {
return nil
return
}
d, err := timeutil.Parse(dur)
if err != nil {
return nil
return
}
c.expireDur = d
return nil
}
}

func WithExpireCheckDuration(dur string) Option {
return func(c *cache) error {
return func(c *cache) {
if len(dur) == 0 {
return nil
return
}
d, err := timeutil.Parse(dur)
if err != nil {
return nil
return
}
c.expireCheckDur = d
return nil
}
}