-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon_test.go
113 lines (95 loc) · 2.01 KB
/
common_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
package conexec
import (
"errors"
"fmt"
"testing"
"time"
. "github.com/go-playground/assert/v2"
)
func testTimeout(t *testing.T, c TimedActuator) {
st := time.Now().UnixNano()
err := c.Exec(getTasks()...)
Equal(t, err, ErrorTimeOut)
et := time.Now().UnixNano()
t.Logf("used time:%dms", (et-st)/1000000)
time.Sleep(time.Millisecond * 500)
}
func testError(t *testing.T, c TimedActuator) {
st := time.Now().UnixNano()
tasks, te := getErrorTask()
err := c.Exec(tasks...)
Equal(t, err, te)
et := time.Now().UnixNano()
t.Logf("used time:%dms", (et-st)/1000000)
time.Sleep(time.Millisecond * 500)
}
func testManyError(t *testing.T, c TimedActuator) {
tasks := make([]Task, 0)
tmp, te := getErrorTask()
tasks = append(tasks, tmp...)
for i := 0; i < 100; i++ {
tmp, _ = getErrorTask()
tasks = append(tasks, tmp...)
}
st := time.Now().UnixNano()
err := c.Exec(tasks...)
Equal(t, err, te)
et := time.Now().UnixNano()
t.Logf("used time:%dms", (et-st)/1000000)
time.Sleep(time.Millisecond * 500)
}
func testNormal(t *testing.T, c TimedActuator) {
Equal(t, c.Exec(getTasks()...), nil)
}
func testPanic(t *testing.T, c TimedActuator) {
NotEqual(t, c.Exec(getPanicTask()), nil)
}
func testEmpty(t *testing.T, c TimedActuator) {
Equal(t, c.Exec(), nil)
}
func getTasks() []Task {
return []Task{
func() error {
fmt.Println(1)
time.Sleep(time.Millisecond * 100)
return nil
},
func() error {
fmt.Println(2)
return nil
},
func() error {
time.Sleep(time.Millisecond * 200)
fmt.Println(3)
return nil
},
}
}
func getErrorTask() ([]Task, error) {
te := errors.New("TestErr")
tasks := getTasks()
tasks = append(tasks,
func() error {
fmt.Println("4")
return te
},
func() error {
time.Sleep(time.Millisecond * 300)
fmt.Println("5")
return te
},
func() error {
time.Sleep(time.Second)
fmt.Println("6")
return te
})
return tasks, te
}
func getPanicTask() Task {
return func() error {
var i *int64
num := *i + 1
fmt.Println(num)
return nil
}
}