Skip to content

Commit

Permalink
✅ fix test for internal/db/kvs/redis
Browse files Browse the repository at this point in the history
Signed-off-by: Rintaro Okamura <rintaro.okamura@gmail.com>
  • Loading branch information
rinx committed Aug 13, 2020
1 parent 22227d0 commit 3a3c7d9
Show file tree
Hide file tree
Showing 7 changed files with 969 additions and 329 deletions.
210 changes: 205 additions & 5 deletions internal/db/kvs/redis/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ import (
redis "github.com/go-redis/redis/v7"
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/net"
"github.com/vdaas/vald/internal/net/tcp"
"go.uber.org/goleak"
)

func TestWithDialer(t *testing.T) {
type T = redisClient
type args struct {
der func(ctx context.Context, addr, port string) (net.Conn, error)
der tcp.Dialer
}
type want struct {
obj *T
Expand All @@ -53,18 +54,20 @@ func TestWithDialer(t *testing.T) {
return errors.Errorf("got error = %v, want %v", err, w.err)
}

if reflect.ValueOf(w.obj.dialer).Pointer() != reflect.ValueOf(obj.dialer).Pointer() {
return errors.Errorf("got dialer = %p, want %p", obj.dialer, w.obj.dialer)
if !reflect.DeepEqual(obj, w.obj) {
return errors.Errorf("got = %v, want %v", obj, w.obj)
}

return nil
}

tests := []test{
func() test {
der := func(ctx context.Context, addr, port string) (net.Conn, error) {
return nil, nil
der, err := tcp.NewDialer()
if err != nil {
panic(err)
}

return test{
name: "set success when der is not nil",
args: args{
Expand Down Expand Up @@ -118,6 +121,94 @@ func TestWithDialer(t *testing.T) {
}
}

func TestWithDialerFunc(t *testing.T) {
type T = redisClient
type args struct {
der func(ctx context.Context, addr, port string) (net.Conn, error)
}
type want struct {
obj *T
err error
}
type test struct {
name string
args args
want want
checkFunc func(want, *T, error) error
beforeFunc func(args)
afterFunc func(args)
}

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.ValueOf(w.obj.dialerFunc).Pointer() != reflect.ValueOf(obj.dialerFunc).Pointer() {
return errors.Errorf("got dialer = %p, want %p", obj.dialerFunc, w.obj.dialerFunc)
}

return nil
}

tests := []test{
func() test {
der := func(ctx context.Context, addr, port string) (net.Conn, error) {
return nil, nil
}
return test{
name: "set success when der is not nil",
args: args{
der: der,
},
want: want{
obj: &T{
dialerFunc: der,
},
err: nil,
},
}
}(),

func() test {
return test{
name: "set nothing when der is nil",
args: args{
der: nil,
},
want: want{
obj: &T{
dialerFunc: nil,
},
err: nil,
},
}
}(),
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
defer goleak.VerifyNone(tt, goleakIgnoreOptions...)
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 := WithDialerFunc(test.args.der)
obj := new(T)
if err := test.checkFunc(test.want, obj, got(obj)); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

func TestWithAddrs(t *testing.T) {
type T = redisClient
type args struct {
Expand Down Expand Up @@ -2179,3 +2270,112 @@ func TestWithInitialPingDuration(t *testing.T) {
})
}
}

func TestWithHooks(t *testing.T) {
type T = redisClient
type args struct {
hooks []redis.Hook
}
type fields struct {
hooks []redis.Hook
}
type want struct {
obj *T
err error
}
type test struct {
name string
args args
fields fields
want want
checkFunc func(want, *T, error) error
beforeFunc func(args)
afterFunc func(args)
}

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{
func() test {
dhs := []redis.Hook{
&dummyHook{},
}

return test{
name: "overwrites when fields.hooks is nil",
args: args{
hooks: dhs,
},
want: want{
obj: &T{
hooks: dhs,
},
},
}
}(),

func() test {
return test{
name: "appends when fields.hooks is not nil",
args: args{
hooks: []redis.Hook{
&dummyHook{
name: "b",
},
},
},
fields: fields{
hooks: []redis.Hook{
&dummyHook{
name: "a",
},
},
},
want: want{
obj: &T{
hooks: []redis.Hook{
&dummyHook{
name: "a",
},
&dummyHook{
name: "b",
},
},
},
},
}
}(),
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
defer goleak.VerifyNone(tt, goleakIgnoreOptions...)
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 := WithHooks(test.args.hooks...)
obj := &T{
hooks: test.fields.hooks,
}
if err := test.checkFunc(test.want, obj, got(obj)); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}
4 changes: 4 additions & 0 deletions internal/db/kvs/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ func (rc *redisClient) newSentinelClient() (*redis.Client, error) {
}

func (rc *redisClient) newClusterClient(ctx context.Context) (*redis.ClusterClient, error) {
if len(rc.addrs[0]) == 0 {
return nil, errors.ErrRedisAddrsNotFound
}

c := redis.NewClusterClient(&redis.ClusterOptions{
Addrs: rc.addrs,
Dialer: rc.dialerFunc,
Expand Down
32 changes: 31 additions & 1 deletion internal/db/kvs/redis/redis_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
//
package redis

import redis "github.com/go-redis/redis/v7"
import (
"context"

redis "github.com/go-redis/redis/v7"
)

type MockRedis struct {
TxPipelineFunc func() redis.Pipeliner
Expand Down Expand Up @@ -51,3 +55,29 @@ func (m *MockRedis) MGet(keys ...string) *redis.SliceCmd {
func (m *MockRedis) Del(keys ...string) *redis.IntCmd {
return m.DelFunc(keys...)
}

type dummyHook struct {
name string
}

func (d *dummyHook) BeforeProcess(ctx context.Context, cmd Cmder) (context.Context, error) {
return ctx, nil
}

func (d *dummyHook) AfterProcess(ctx context.Context, cmd Cmder) error {
return nil
}

func (d *dummyHook) BeforeProcessPipeline(ctx context.Context, cmds []Cmder) (context.Context, error) {
return ctx, nil
}

func (d *dummyHook) AfterProcessPipeline(ctx context.Context, cmds []Cmder) error {
return nil
}

func dummyWithFunc(err error) Option {
return func(*redisClient) error {
return err
}
}
Loading

0 comments on commit 3a3c7d9

Please sign in to comment.