This repository was archived by the owner on Jun 24, 2026. It is now read-only.
forked from katanomi/pkg
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtask_test.go
More file actions
320 lines (268 loc) · 8.74 KB
/
Copy pathtask_test.go
File metadata and controls
320 lines (268 loc) · 8.74 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
Copyright 2021 The AlaudaDevops Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package parallel_test
import (
"context"
"errors"
"fmt"
"sync"
"testing"
"time"
"github.com/AlaudaDevops/pkg/parallel"
"go.uber.org/zap"
apierrors "k8s.io/apimachinery/pkg/util/errors"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
type executeFlag struct {
executed bool
sync.RWMutex
}
func (e *executeFlag) set(executed bool) {
e.Lock()
e.executed = executed
e.Unlock()
}
func (e *executeFlag) get() bool {
e.RLock()
defer e.RUnlock()
return e.executed
}
func TestParallelTask(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "pkg/parallel")
}
func generateTask(index int, sleep time.Duration, err error, excuted *executeFlag) (task parallel.Task) {
return func() (interface{}, error) {
time.Sleep(sleep * time.Second)
excuted.set(true)
return fmt.Sprintf("task-%d", index), err
}
}
var _ = Describe("P().Do().Wait()", func() {
var (
t1 parallel.Task
t1Excuted *executeFlag
t2 parallel.Task
t2Excuted *executeFlag
t3 parallel.Task
t3Excuted *executeFlag
t4 parallel.Task
t4Excuted *executeFlag
ptasks *parallel.ParallelTasks
res []interface{}
errs error
elapsed float64
zaplog, _ = zap.NewDevelopment()
log = zaplog.Sugar()
)
BeforeEach(func() {
t1Excuted = &executeFlag{}
t2Excuted = &executeFlag{}
t3Excuted = &executeFlag{}
t4Excuted = &executeFlag{}
t1 = generateTask(1, 2, nil, t1Excuted)
t2 = generateTask(2, 2, nil, t2Excuted)
t3 = generateTask(3, 3, nil, t3Excuted)
t4 = func() (interface{}, error) {
t4Excuted.set(true)
return nil, nil
}
ptasks = parallel.P(log, "custom case", t1, t2, t3, t4)
})
JustBeforeEach(func() {
begin := time.Now()
res, errs = ptasks.Do().Wait()
elapsed = time.Since(begin).Seconds()
})
Context("when none error happened", func() {
It("should execute task parallel and collect the results", func() {
Expect(errs).To(BeNil())
Expect(t1Excuted.get()).To(BeTrue())
Expect(t2Excuted.get()).To(BeTrue())
Expect(t3Excuted.get()).To(BeTrue())
Expect(elapsed < 4 && elapsed > 3).To(BeTrue())
Expect(len(res)).To(BeEquivalentTo(3))
Expect(res).To(ContainElement("task-1"))
Expect(res).To(ContainElement("task-2"))
Expect(res).To(ContainElement("task-3"))
})
})
Context("when some errors happened", func() {
errT1 := errors.New("task-1 error")
errT2 := errors.New("task-2 error")
BeforeEach(func() {
t1 = generateTask(1, 2, errT1, t1Excuted)
t2 = generateTask(2, 2, errT2, t2Excuted)
ptasks = parallel.P(log, "errors case", t1, t2, t3)
})
It("should return all errors happened and execute other task", func() {
Expect(errs).ToNot(BeNil())
multiErrs, ok := errs.(apierrors.Aggregate)
Expect(ok).To(BeTrue())
Expect(multiErrs.Errors()).To(ContainElement(errT1))
Expect(multiErrs.Errors()).To(ContainElement(errT2))
Expect(multiErrs.Error()).NotTo(BeEmpty())
Expect(t1Excuted.get()).To(BeTrue())
Expect(t2Excuted.get()).To(BeTrue())
Expect(t3Excuted.get()).To(BeTrue())
Expect(elapsed < 4 && elapsed > 3).To(BeTrue())
Expect(len(res)).To(BeEquivalentTo(1))
Expect(res).To(ContainElement("task-3"))
})
})
Context("when set failfast and errors happened", func() {
errT1 := errors.New("task-1 error")
errT2 := errors.New("task-2 error")
BeforeEach(func() {
t1 = generateTask(1, 2, errT1, t1Excuted)
t2 = generateTask(2, 1, errT2, t2Excuted)
ptasks = parallel.P(log, "failfast case", t1, t2, t3).FailFast()
})
It("should fail immediately and return first error", func() {
Expect(errs).ToNot(BeNil())
Expect(errs).To(BeEquivalentTo(errT2))
Expect(t1Excuted.get()).To(BeFalse())
Expect(t2Excuted.get()).To(BeTrue())
Expect(t3Excuted.get()).To(BeFalse())
Expect(elapsed < 2 && elapsed > 1).To(BeTrue())
Expect(len(res)).To(BeEquivalentTo(0))
})
})
Context("when canceld by caller", func() {
It("should return reason of canceld", func() {
var cancelFunc context.CancelFunc
var ctx context.Context
ctx, cancelFunc = context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancelFunc()
ptasksCancelable := parallel.P(log, "canceld case", t1, t2, t3).Context(ctx)
begin := time.Now()
res, errs = ptasksCancelable.Do().Wait()
elapsed = time.Since(begin).Seconds()
Expect(errs).ToNot(BeNil())
Expect(errs).To(BeEquivalentTo(context.DeadlineExceeded))
fmt.Printf("%v,%v,%v,", t1Excuted.get(), t2Excuted.get(), t3Excuted.get())
// up to now, task is not support cancel
// Expect(t1Excuted.executed).To(BeFalse())
// Expect(t2Excuted.executed).To(BeFalse())
// Expect(t3Excuted.executed).To(BeFalse())
Expect(elapsed > 0.1 && elapsed < 5).To(BeTrue(), fmt.Sprintf("actual elapsed: %v", elapsed))
Expect(len(res)).To(BeEquivalentTo(0))
})
})
Context("when many task ", func() {
flag4 := &executeFlag{}
flag5 := &executeFlag{}
flag6 := &executeFlag{}
flag7 := &executeFlag{}
flag8 := &executeFlag{}
flag9 := &executeFlag{}
flag10 := &executeFlag{}
BeforeEach(func() {
t4 := generateTask(4, 2, nil, flag4)
t5 := generateTask(5, 1, nil, flag5)
t6 := generateTask(6, 2, nil, flag6)
t7 := generateTask(7, 1, nil, flag7)
t8 := generateTask(8, 2, nil, flag8)
t9 := generateTask(9, 3, nil, flag9)
t10 := generateTask(10, 3, nil, flag10)
ptasks = parallel.P(log, "many-tasks", t1, t2, t3, t4, t5, t6).Add(t7).Add(t8).Add(t9).Add(t10).Name("custom case")
})
It("should return results of all tasks", func() {
Expect(errs).To(BeNil())
Expect(t1Excuted.get()).To(BeTrue())
Expect(t2Excuted.get()).To(BeTrue())
Expect(t3Excuted.get()).To(BeTrue())
Expect(flag4.get()).To(BeTrue())
Expect(flag5.get()).To(BeTrue())
Expect(flag6.get()).To(BeTrue())
Expect(flag7.get()).To(BeTrue())
Expect(flag8.get()).To(BeTrue())
Expect(flag9.get()).To(BeTrue())
Expect(flag10.get()).To(BeTrue())
Expect(elapsed < 4 && elapsed > 3).To(BeTrue())
Expect(len(res)).To(BeEquivalentTo(10))
for i := 1; i <= 10; i++ {
Expect(res).To(ContainElement(fmt.Sprintf("task-%d", i)))
}
})
It("should return with same sort", func() {
Expect(errs).To(BeNil())
for i := 0; i <= 9; i++ {
Expect(res[i]).To(Equal(fmt.Sprintf("task-%d", i+1)))
}
})
})
Context("when set conccurrent", func() {
flag4 := &executeFlag{}
flag5 := &executeFlag{}
flag6 := &executeFlag{}
flag7 := &executeFlag{}
flag8 := &executeFlag{}
flag9 := &executeFlag{}
flag10 := &executeFlag{}
BeforeEach(func() {
t4 := generateTask(4, 2, nil, flag4)
t5 := generateTask(5, 1, nil, flag5)
t6 := generateTask(6, 3, nil, flag6)
t7 := generateTask(7, 1, nil, flag7)
t8 := generateTask(8, 2, nil, flag8)
t9 := generateTask(9, 3, nil, flag9)
t10 := generateTask(10, 3, nil, flag10)
ptasks = parallel.P(log, "many-tasks", t1, t2, t3, t4, t5, t6).Add(t7).Add(t8).Add(t9).Add(t10).Name("custom case")
ptasks.SetConcurrent(5)
})
It("should run tasks in conccurrent count", func() {
Expect(errs).To(BeNil())
Expect(t1Excuted.get()).To(BeTrue())
Expect(t2Excuted.get()).To(BeTrue())
Expect(t3Excuted.get()).To(BeTrue())
Expect(flag4.get()).To(BeTrue())
Expect(flag5.get()).To(BeTrue())
Expect(flag6.get()).To(BeTrue())
Expect(flag7.get()).To(BeTrue())
Expect(flag8.get()).To(BeTrue())
Expect(flag9.get()).To(BeTrue())
Expect(flag10.get()).To(BeTrue())
Expect(elapsed < 7 && elapsed > 6).To(BeTrue())
Expect(len(res)).To(BeEquivalentTo(10))
for i := 1; i <= 10; i++ {
Expect(res).To(ContainElement(fmt.Sprintf("task-%d", i)))
}
})
})
})
var _ = Describe("SetMaxConcurrent", func() {
var (
ptasks *parallel.ParallelTasks
zapLog, _ = zap.NewDevelopment()
log = zapLog.Sugar()
)
BeforeEach(func() {
ptasks = parallel.P(log, "custom case")
})
Context("SetMaxConcurrent", func() {
It("when max great concurrent", func() {
ptasks.SetMaxConcurrent(5, 6)
Expect(ptasks.Options.ConcurrencyCount).To(BeEquivalentTo(5))
})
It("when max less concurrent", func() {
ptasks.SetMaxConcurrent(5, 4)
Expect(ptasks.Options.ConcurrencyCount).To(BeEquivalentTo(4))
})
It("when max equal concurrent", func() {
ptasks.SetMaxConcurrent(5, 5)
Expect(ptasks.Options.ConcurrencyCount).To(BeEquivalentTo(5))
})
})
})