Skip to content

Commit

Permalink
add cache test
Browse files Browse the repository at this point in the history
  • Loading branch information
kevindiu authored and actions-user committed Jul 13, 2020
1 parent 634e3e5 commit cab1052
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
2 changes: 2 additions & 0 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,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...) {
Expand Down
57 changes: 32 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{
goleak.IgnoreTopFunction("github.com/kpango/fastime.(*Fastime).StartTimerD.func1"),
}
)

func TestNew(t *testing.T) {
type args struct {
opts []Option
Expand All @@ -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)
}
Expand Down

0 comments on commit cab1052

Please sign in to comment.