-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool_test.go
More file actions
98 lines (84 loc) · 1.58 KB
/
pool_test.go
File metadata and controls
98 lines (84 loc) · 1.58 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
//go:build !race
package gopool
import (
"fmt"
"strconv"
"testing"
"time"
)
// func BenchmarkPoolRun(b *testing.B) {
// pool := NewPool(10000)
// pool.SetTaskNum(b.N)
// go func() {
// for i := 0; i < b.N; i++ {
// pool.AddTask(NewTask(taskFunc, callbackFunc, i))
// }
// }()
// pool.Run()
// }
type MyTask struct {
ITask
}
func (m *MyTask) Execute() error {
fmt.Println("my task running...")
return nil
}
func (m *MyTask) GetResult() any {
return 1
}
func TestNewPool(t *testing.T) {
cases := map[string]struct {
cap int
taskNum int
customTask ITask
}{
"5/10": {
cap: 5,
taskNum: 10,
},
"custom task": {
cap: 5,
taskNum: 10,
customTask: &MyTask{},
},
// "5/100": {
// cap: 5,
// taskNum: 100,
// },
// "10/1000": {
// cap: 10,
// taskNum: 1000,
// },
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
pool := NewPool(tc.cap)
pool.SetDebug(true)
pool.SetTaskNum(tc.taskNum)
pool.SetTimeout(5 * time.Second)
go func() {
for i := 0; i < tc.taskNum; i++ {
if tc.customTask != nil {
pool.AddTask(tc.customTask)
} else {
pool.AddTask(NewTask(taskFunc, callbackFunc, i))
}
t.Log("task:" + strconv.Itoa(i))
}
}()
pool.Run()
for _, v := range pool.GetResult() {
t.Logf("result: %v", v)
}
})
}
}
func taskFunc(args any) (any, error) {
a := args.(int) + args.(int)
return a, nil
}
func callbackFunc(result any) (any, error) {
// 处理
//fmt.Println("callback completed [", result, "]")
return result, nil
}