-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkqueue_test.go
206 lines (194 loc) · 4.35 KB
/
workqueue_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package workqueue
import (
"errors"
"math/rand"
"testing"
"time"
)
type testT struct {
id int
cnt int
}
func TestWorkQ(t *testing.T) {
f := func() bool {
flag := rand.Intn(1)
// t.Log("flag: ", flag)
if flag == 1 {
return true
}
return false
}
randErr := func() error {
if f() {
return nil
}
return errors.New("random error")
}
// 初始化间隔
SetDuration(time.Second)
// 开始运行
Start()
// 设置该组开始条件 f => func() bool
// 该函数可以后期设置
SetGroupStart("testOrder", f)
Bound := 50
// 第一次重试
for i := 1; i <= Bound; i++ {
Ch <- &Item{
Type: Order,
Group: "testOrder",
GroupItem: GroupItem{
Task: &testT{id: i},
Do: func(args ...interface{}) error {
if len(args) == 0 {
return errors.New("no params")
}
time.Sleep(100 * time.Millisecond)
tmp, _ := args[0].(*testT)
tmp.cnt++
t.Logf("[Order] id: %d Retry cnt: %d\n", tmp.id, tmp.cnt)
if tmp.cnt > 5 {
return errors.New("timeout")
}
return randErr()
},
Retry: func(args ...interface{}) bool {
if len(args) == 0 {
return false
}
tmp, _ := args[0].(*testT)
tmp.cnt++
t.Logf("[Order] id: %d Retry cnt: %d\n", tmp.id, tmp.cnt)
if tmp.cnt > 5 {
t.Logf("[Rand] id: %d Retry func repeat overflow", tmp.id)
return false
}
return true
},
Callback: func(args ...interface{}) {
t.Logf("Callback!!!")
},
},
}
Ch <- &Item{
Type: Rand,
Group: "testRand",
GroupItem: GroupItem{
Task: &testT{id: i + Bound},
Do: func(args ...interface{}) error {
if len(args) == 0 {
return errors.New("no params")
}
time.Sleep(100 * time.Millisecond)
tmp, _ := args[0].(*testT)
tmp.cnt++
t.Logf("[Rand] id: %d Retry cnt: %d\n", tmp.id, tmp.cnt)
if tmp.cnt > 5 {
return errors.New("[Rand] timeout")
}
return randErr()
},
Retry: func(args ...interface{}) bool {
if len(args) == 0 {
return false
}
tmp, _ := args[0].(*testT)
tmp.cnt++
t.Logf("[Rand] id: %d Retry cnt: %d\n", tmp.id, tmp.cnt)
if tmp.cnt > 5 {
t.Logf("[Rand] id: %d Retry func repeat overflow", tmp.id)
return false
}
return true
},
Callback: func(args ...interface{}) {
t.Logf("Callback!!!")
},
},
}
}
time.Sleep(time.Second * 5)
// 删除指定分组
DelGroup("testOrder")
// 第二次重试
for i := Bound*2 + 1; i <= Bound*3; i++ {
Ch <- &Item{
Type: Order,
Group: "testOrder",
GroupItem: GroupItem{
Task: &testT{id: i},
Do: func(args ...interface{}) error {
time.Sleep(10 * time.Millisecond)
tmp, _ := args[0].(*testT)
tmp.cnt++
t.Logf("[Order] id: %d Retry cnt: %d\n", tmp.id, tmp.cnt)
if tmp.cnt > 5 {
return errors.New("timeout")
}
return randErr()
},
Retry: func(args ...interface{}) bool {
tmp, _ := args[0].(*testT)
tmp.cnt++
t.Logf("[Order] id: %d Retry cnt: %d\n", tmp.id, tmp.cnt)
if tmp.cnt > 5 {
t.Logf("[Rand] id: %d Retry func repeat overflow", tmp.id)
return false
}
return true
},
Callback: func(args ...interface{}) {
t.Logf("Callback!!!")
},
},
}
Ch <- &Item{
Type: Rand,
Group: "testRand",
GroupItem: GroupItem{
Task: &testT{id: i + Bound},
Do: func(args ...interface{}) error {
time.Sleep(200 * time.Millisecond)
tmp, _ := args[0].(*testT)
tmp.cnt++
t.Logf("[Rand] id: %d Retry cnt: %d\n", tmp.id, tmp.cnt)
if tmp.cnt > 5 {
return errors.New("[Rand] timeout")
}
return randErr()
},
Retry: func(args ...interface{}) bool {
tmp, _ := args[0].(*testT)
tmp.cnt++
t.Logf("[Rand] id: %d Retry cnt: %d\n", tmp.id, tmp.cnt)
if tmp.cnt > 5 {
t.Log("[Rand] Retry func repeat overflow")
return false
}
return true
},
Callback: func(args ...interface{}) {
t.Logf("Callback!!!")
},
},
}
}
time.Sleep(time.Second * 10)
}
func sendEmptyTask(f func() bool) {
Ch <- &Item{
Type: Order,
Group: "groupName",
GroupItem: GroupItem{
Do: func(args ...interface{}) error {
if f() {
return nil
}
return errors.New("[sendEmptyTask] retry task")
},
Retry: func(args ...interface{}) bool {
return true
},
},
}
}