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 internal/log/mock/retry test #549

Merged
merged 2 commits into from
Jul 6, 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
3 changes: 3 additions & 0 deletions internal/log/mock/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//
package mock

// Retry represents struct of mock retry structure
type Retry struct {
OutFunc func(
fn func(vals ...interface{}) error,
Expand All @@ -28,13 +29,15 @@ type Retry struct {
)
}

// Out calls OutFunc
func (r *Retry) Out(
fn func(vals ...interface{}) error,
vals ...interface{},
) {
r.OutFunc(fn, vals...)
}

// Outf calls OutfFunc
func (r *Retry) Outf(
fn func(format string, vals ...interface{}) error,
format string, vals ...interface{},
Expand Down
175 changes: 92 additions & 83 deletions internal/log/mock/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
package mock

import (
"reflect"
"testing"

"github.com/vdaas/vald/internal/errors"
"go.uber.org/goleak"
)

Expand All @@ -27,15 +29,14 @@ func TestRetry_Out(t *testing.T) {
vals []interface{}
}
type fields struct {
OutFunc func(fn func(vals ...interface{}) error, vals ...interface{})
OutfFunc func(fn func(format string, vals ...interface{}) error, format string, vals ...interface{})
OutFunc func(fn func(vals ...interface{}) error, vals ...interface{})
}
type want struct {
}
type test struct {
name string
args args
fields fields
fieldsFunc func(*testing.T) fields
want want
checkFunc func(want) error
beforeFunc func(args)
Expand All @@ -45,46 +46,49 @@ func TestRetry_Out(t *testing.T) {
return nil
}
tests := []test{
// TODO test cases
/*
{
name: "test_case_1",
args: args {
fn: nil,
vals: nil,
},
fields: fields {
OutFunc: nil,
OutfFunc: nil,
},
want: want{},
checkFunc: defaultCheckFunc,
},
*/

// TODO test cases
/*
func() test {
return test {
name: "test_case_2",
args: args {
fn: nil,
vals: nil,
},
fields: fields {
OutFunc: nil,
OutfFunc: nil,
},
want: want{},
checkFunc: defaultCheckFunc,
}
}(),
*/
func() test {
var (
wantFn = func(vals ...interface{}) error {
return nil
}
wantVals = []interface{}{
"Vald",
}
cnt int
)
return test{
name: "Call Out",
args: args{
fn: wantFn,
vals: wantVals,
},
fieldsFunc: func(t *testing.T) fields {
t.Helper()
return fields{
OutFunc: func(fn func(vals ...interface{}) error, vals ...interface{}) {
if !reflect.DeepEqual(vals, wantVals) {
t.Errorf("vals got = %v, want = %v", vals, wantVals)
}
if reflect.ValueOf(fn).Pointer() != reflect.ValueOf(wantFn).Pointer() {
t.Errorf("fn got = %p, want = %p", fn, wantFn)
}
cnt++
},
}
},
checkFunc: func(w want) error {
if cnt != 1 {
return errors.Errorf("cnt got = %d, want = %d", cnt, 1)
}
return nil
},
}
}(),
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
defer goleak.VerifyNone(t)
defer goleak.VerifyNone(tt)
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
Expand All @@ -94,9 +98,9 @@ func TestRetry_Out(t *testing.T) {
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}
fields := test.fieldsFunc(tt)
r := &Retry{
OutFunc: test.fields.OutFunc,
OutfFunc: test.fields.OutfFunc,
OutFunc: fields.OutFunc,
}

r.Out(test.args.fn, test.args.vals...)
Expand All @@ -114,15 +118,14 @@ func TestRetry_Outf(t *testing.T) {
vals []interface{}
}
type fields struct {
OutFunc func(fn func(vals ...interface{}) error, vals ...interface{})
OutfFunc func(fn func(format string, vals ...interface{}) error, format string, vals ...interface{})
}
type want struct {
}
type test struct {
name string
args args
fields fields
fieldsFunc func(*testing.T) fields
want want
checkFunc func(want) error
beforeFunc func(args)
Expand All @@ -132,48 +135,54 @@ func TestRetry_Outf(t *testing.T) {
return nil
}
tests := []test{
// TODO test cases
/*
{
name: "test_case_1",
args: args {
fn: nil,
format: "",
vals: nil,
},
fields: fields {
OutFunc: nil,
OutfFunc: nil,
},
want: want{},
checkFunc: defaultCheckFunc,
},
*/

// TODO test cases
/*
func() test {
return test {
name: "test_case_2",
args: args {
fn: nil,
format: "",
vals: nil,
},
fields: fields {
OutFunc: nil,
OutfFunc: nil,
},
want: want{},
checkFunc: defaultCheckFunc,
}
}(),
*/
func() test {
var (
wantFn = func(format string, vals ...interface{}) error {
return nil
}
wantVals = []interface{}{
"Vald",
}
wantFormat = "json"
cnt int
)
return test{
name: "Call Outf",
args: args{
fn: wantFn,
format: wantFormat,
vals: wantVals,
},
fieldsFunc: func(t *testing.T) fields {
t.Helper()
return fields{
OutfFunc: func(fn func(format string, vals ...interface{}) error, format string, vals ...interface{}) {
if !reflect.DeepEqual(vals, wantVals) {
t.Errorf("vals got = %v, want = %v", vals, wantVals)
}
if format != wantFormat {
t.Errorf("format got = %s, want = %s", format, wantFormat)
}
if reflect.ValueOf(fn).Pointer() != reflect.ValueOf(wantFn).Pointer() {
t.Errorf("fn got = %p, want = %p", fn, wantFn)
}
cnt++
},
}
},
checkFunc: func(w want) error {
if cnt != 1 {
return errors.Errorf("cnt got = %d, want = %d", cnt, 1)
}
return nil
},
}
}(),
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
defer goleak.VerifyNone(t)
defer goleak.VerifyNone(tt)
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
Expand All @@ -183,9 +192,9 @@ func TestRetry_Outf(t *testing.T) {
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}
fields := test.fieldsFunc(tt)
r := &Retry{
OutFunc: test.fields.OutFunc,
OutfFunc: test.fields.OutfFunc,
OutfFunc: fields.OutfFunc,
}

r.Outf(test.args.fn, test.args.format, test.args.vals...)
Expand Down