-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.go
53 lines (45 loc) · 876 Bytes
/
helpers_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
package flash
import (
"errors"
"fmt"
"math/rand"
"time"
)
type testTask struct {
ID int
Fail bool
Delay string
Status string
}
func (t *testTask) Execute() error {
duration, err := time.ParseDuration(t.Delay)
if err != nil {
logger.Warnf("parse duration error... overriding Delay as 1 second")
duration = time.Second
}
time.Sleep(duration)
if t.Fail {
return errors.New("some error")
}
return nil
}
func (t *testTask) OnFailure(err error) {
t.Status = "failed"
}
func (t *testTask) OnSuccess() {
t.Status = "completed"
}
func (t *testTask) IsCompleted() bool {
return t.Status == "completed"
}
func nTasks(n int) []Executable {
tasks := make([]Executable, 0)
for i := 0; i < n; i++ {
rand.Seed(time.Now().Unix())
tasks = append(tasks, &testTask{
ID: i,
Delay: fmt.Sprintf("%dms", rand.Intn(999)),
})
}
return tasks
}