Skip to content

Commit

Permalink
Fix internal/log/retry pacakge (#458)
Browse files Browse the repository at this point in the history
* fix: retry package based on generated test code

Signed-off-by: hlts2 <hiroto.funakoshi.hiroto@gmail.com>

* fix: deleted eary return

Signed-off-by: hlts2 <hiroto.funakoshi.hiroto@gmail.com>

* fix: deleted unnecessary test case

Signed-off-by: hlts2 <hiroto.funakoshi.hiroto@gmail.com>
  • Loading branch information
hlts2 authored Jun 10, 2020
1 parent 59327cb commit 44dff4a
Show file tree
Hide file tree
Showing 3 changed files with 303 additions and 427 deletions.
176 changes: 104 additions & 72 deletions internal/log/retry/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,120 +16,152 @@
package retry

import (
"errors"
"reflect"
"testing"

"github.com/vdaas/vald/internal/errors"

"go.uber.org/goleak"
)

func TestWithError(t *testing.T) {
type T = retry
type args struct {
fn func(vals ...interface{})
}
type want struct {
obj *T
}
type test struct {
name string
fn func(vals ...interface{})
checkFunc func(Option) error
name string
args args
want want
checkFunc func(want, *T) error
beforeFunc func(args)
afterFunc func(args)
}

defaultCheckFunc := func(w want, obj *T) error {
if reflect.ValueOf(w.obj.errorFn).Pointer() != reflect.ValueOf(obj.errorFn).Pointer() {
return errors.Errorf("got = %v, want %v", obj, w.obj)
}
return nil
}

tests := []test{
func() test {
fn := func(vals ...interface{}) {}

return test{
name: "set success when fn is not nil",
fn: fn,
checkFunc: func(opt Option) error {
got := new(retry)
opt(got)

if reflect.ValueOf(fn).Pointer() != reflect.ValueOf(got.errorFn).Pointer() {
return errors.New("invalid params was set")
}
return nil
args: args{
fn: fn,
},
want: want{
obj: &T{
errorFn: fn,
},
},
}
}(),

func() test {
fn := func(vals ...interface{}) {}
{
name: "set nothing when fn is nil",
want: want{
obj: new(T),
},
},
}

return test{
name: "returns nothing when fn is nil",
fn: nil,
checkFunc: func(opt Option) error {
got := &retry{
errorFn: fn,
}
opt(got)
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 reflect.ValueOf(fn).Pointer() != reflect.ValueOf(got.errorFn).Pointer() {
return errors.New("invalid params was set")
}
return nil
},
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}
}(),
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opt := WithError(tt.fn)
if err := tt.checkFunc(opt); err != nil {
t.Error(err)
got := WithError(test.args.fn)
obj := new(T)
got(obj)
if err := test.checkFunc(test.want, obj); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

func TestWithWarn(t *testing.T) {
type T = retry
type args struct {
fn func(vals ...interface{})
}
type want struct {
obj *T
}
type test struct {
name string
fn func(vals ...interface{})
checkFunc func(Option) error
name string
args args
want want
checkFunc func(want, *T) error
beforeFunc func(args)
afterFunc func(args)
}

defaultCheckFunc := func(w want, obj *T) error {
if reflect.ValueOf(w.obj.warnFn).Pointer() != reflect.ValueOf(obj.warnFn).Pointer() {
return errors.Errorf("got = %v, want %v", obj, w.obj)
}
return nil
}

tests := []test{
func() test {
fn := func(vals ...interface{}) {}

return test{
name: "set success when fn is not nil",
fn: fn,
checkFunc: func(opt Option) error {
got := new(retry)
opt(got)

if reflect.ValueOf(fn).Pointer() != reflect.ValueOf(got.warnFn).Pointer() {
return errors.New("invalid params was set")
}
return nil
args: args{
fn: fn,
},
}
}(),

func() test {
fn := func(vals ...interface{}) {}

return test{
name: "returns nothing when fn is nil",
fn: nil,
checkFunc: func(opt Option) error {
got := &retry{
want: want{
obj: &T{
warnFn: fn,
}
opt(got)

if reflect.ValueOf(fn).Pointer() != reflect.ValueOf(got.warnFn).Pointer() {
return errors.New("invalid params was set")
}
return nil
},
},
}
}(),

{
name: "set nothing when fn is nil",
want: want{
obj: new(T),
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opt := WithWarn(tt.fn)
if err := tt.checkFunc(opt); err != nil {
t.Error(err)
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 := WithWarn(test.args.fn)
obj := new(T)
got(obj)
if err := test.checkFunc(test.want, obj); err != nil {
tt.Errorf("error = %v", err)
}
})
}
Expand Down
32 changes: 14 additions & 18 deletions internal/log/retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,31 @@ func (r *retry) Out(
fn func(vals ...interface{}) error,
vals ...interface{},
) {
if fn == nil {
return
if fn != nil {
r.Outf(func(format string, vals ...interface{}) error {
return fn(vals...)
}, "", vals...)
}

r.Outf(func(format string, vals ...interface{}) error {
return fn(vals...)
}, "", vals...)
}

func (r *retry) Outf(
fn func(format string, vals ...interface{}) error,
format string, vals ...interface{},
) {
if fn == nil {
return
}

if err := fn(format, vals...); err != nil {
rv := reflect.ValueOf(fn)
if fn != nil {
if err := fn(format, vals...); err != nil {
rv := reflect.ValueOf(fn)

r.warnFn(errors.ErrLoggingRetry(err, rv))

err = fn(format, vals...)
if err != nil {
r.errorFn(errors.ErrLoggingFailed(err, rv))
r.warnFn(errors.ErrLoggingRetry(err, rv))

err = fn(format, vals...)
if err != nil {
panic(err)
r.errorFn(errors.ErrLoggingFailed(err, rv))

err = fn(format, vals...)
if err != nil {
panic(err)
}
}
}
}
Expand Down
Loading

0 comments on commit 44dff4a

Please sign in to comment.