-
Notifications
You must be signed in to change notification settings - Fork 40
/
runner_test.go
189 lines (176 loc) · 3.59 KB
/
runner_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
package gopter
import (
"errors"
"reflect"
"testing"
"time"
)
func TestRunnerSingleWorker(t *testing.T) {
parameters := DefaultTestParameters()
testRunner := &runner{
parameters: parameters,
worker: func(num int, shouldStop shouldStop) *TestResult {
return &TestResult{
Status: TestPassed,
Succeeded: 1,
Discarded: 0,
}
},
}
result := testRunner.runWorkers()
if result.Status != TestPassed ||
result.Succeeded != 1 ||
result.Discarded != 0 {
t.Errorf("Invalid result: %#v", result)
}
}
func TestRunnerParallelWorkers(t *testing.T) {
parameters := DefaultTestParameters()
specs := []struct {
workers int
res []TestResult
exp *TestResult
wait []int
}{
// Test all pass
{
workers: 50,
res: []TestResult{
{
Status: TestPassed,
Succeeded: 10,
Discarded: 1,
},
},
exp: &TestResult{
Status: TestPassed,
Succeeded: 500,
Discarded: 50,
},
},
// Test exhausted
{
workers: 50,
res: []TestResult{
{
Status: TestExhausted,
Succeeded: 1,
Discarded: 10,
},
},
exp: &TestResult{
Status: TestExhausted,
Succeeded: 50,
Discarded: 500,
},
},
// Test all fail
{
workers: 50,
res: []TestResult{
{
Status: TestFailed,
Succeeded: 0,
Discarded: 0,
Labels: []string{"some label"},
Error: errors.New("invalid result 0 != 1"),
},
},
exp: &TestResult{
Status: TestFailed,
Succeeded: 0,
Discarded: 0,
Labels: []string{"some label"},
Error: errors.New("invalid result 0 != 1"),
},
},
// a pass and failure
{
workers: 2,
res: []TestResult{
{
Status: TestPassed,
Succeeded: 94,
Discarded: 1,
},
{
Status: TestFailed,
Succeeded: 4,
Discarded: 3,
Labels: []string{"some label"},
Error: errors.New("invalid result 0 != 2"),
},
},
exp: &TestResult{
Status: TestFailed,
Succeeded: 98,
Discarded: 4,
Labels: []string{"some label"},
Error: errors.New("invalid result 0 != 2"),
},
wait: []int{1, 0},
},
// a pass and multiple failures (first failure returned)
{
workers: 3,
res: []TestResult{
{
Status: TestPassed,
Succeeded: 94,
Discarded: 1,
},
{
Status: TestFailed,
Succeeded: 3,
Discarded: 2,
Labels: []string{"worker 1"},
Error: errors.New("worker 1 error"),
},
{
Status: TestFailed,
Succeeded: 1,
Discarded: 1,
Labels: []string{"worker 2"},
Error: errors.New("worker 2 error"),
},
},
exp: &TestResult{
Status: TestFailed,
Succeeded: 98,
Discarded: 4,
Labels: []string{"worker 1"},
Error: errors.New("worker 1 error"),
},
wait: []int{0, 1, 2},
},
}
for specIdx, spec := range specs {
parameters.Workers = spec.workers
testRunner := &runner{
parameters: parameters,
worker: func(num int, shouldStop shouldStop) *TestResult {
if num < len(spec.wait) {
time.Sleep(time.Duration(spec.wait[num]) * time.Second)
}
if num < len(spec.res) {
return &spec.res[num]
}
return &spec.res[0]
},
}
result := testRunner.runWorkers()
if result.Time < 0 {
t.Errorf("[%d] expected result time to be positive number but got %s", specIdx, result.Time)
}
// This is not deterministic and
// have validated above the time
result.Time = 0
if !reflect.DeepEqual(result, spec.exp) {
t.Errorf("[%d] expected test result %#v but got %#v",
specIdx,
spec.exp,
result,
)
}
}
}