-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_test.go
50 lines (47 loc) · 1019 Bytes
/
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
package gopool
import (
"context"
"errors"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewTask(t *testing.T) {
cases := map[string]struct {
args any
callback CallbackFunc
taskFunc TaskFunc
expectval any
}{
"success": {
args: 1,
taskFunc: func(args any) (any, error) {
a := args.(int) + args.(int)
return a, nil
},
callback: func(result any) (any, error) {
return result, nil
},
expectval: 2,
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
task := NewTask(tc.taskFunc, tc.callback, tc.args)
err := task.Execute()
if err != nil {
if errors.Is(err, TimecoutError) {
t.Log(TimecoutError.Error())
} else if errors.Is(err, context.DeadlineExceeded) {
t.Log(context.DeadlineExceeded.Error())
} else if os.IsTimeout(err) {
t.Log("IsTimeoutError:" + err.Error())
} else {
assert.NoError(t, err)
}
} else {
assert.Equal(t, tc.expectval, task.GetResult())
}
})
}
}