-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_test.go
86 lines (76 loc) · 1.87 KB
/
task_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package periodic
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewTask(t *testing.T) {
testCases := []struct {
expected error
taskFunc interface{}
taskFuncParams []interface{}
}{
{
expected: nil,
taskFunc: func() {},
taskFuncParams: []interface{}{},
},
{
expected: ErrNoFunction,
taskFunc: "test",
taskFuncParams: []interface{}{},
},
{
expected: ErrNotMatchedNumParams,
taskFunc: func(val1 int, val2 string) {},
taskFuncParams: []interface{}{1, "str", 1, []string{"1"}},
},
{
expected: ErrNotMatchedNumParams,
taskFunc: func() {},
taskFuncParams: []interface{}{1},
},
}
for _, tc := range testCases {
tc := tc
_, err := NewTask(tc.taskFunc, tc.taskFuncParams...)
assert.Equal(t, tc.expected, err)
}
}
func TestRunTask(t *testing.T) {
testCases := []struct {
expected interface{}
taskFunc interface{}
taskFuncParams []interface{}
}{
{
expected: 123,
taskFunc: func() int { return 123 },
taskFuncParams: []interface{}{},
},
{
expected: 20,
taskFunc: func(val1 int, val2 int) int { return val1 * val2 },
taskFuncParams: []interface{}{10, 2},
},
{
expected: "string",
taskFunc: func(val string) string { return val },
taskFuncParams: []interface{}{"string"},
},
{
expected: []float64{1.0, 2.0},
taskFunc: func() []float64 { return []float64{1.0, 2.0} },
taskFuncParams: []interface{}{},
},
}
for _, tc := range testCases {
tc := tc
job, _ := NewTask(tc.taskFunc, tc.taskFuncParams...)
f := job.GetTaskFunc()
p := job.GetTaskFuncParams()
actual := f.Call(p)
assert.Equal(t, reflect.ValueOf(tc.expected).Type(), actual[0].Type())
assert.Equal(t, reflect.ValueOf(tc.expected).Interface(), actual[0].Interface())
}
}