-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine_test.go
170 lines (159 loc) · 4.34 KB
/
engine_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
package engine
import (
"context"
"testing"
)
func TestEngine_Stop(t *testing.T) {
type fields struct {
stop chan struct{}
}
tests := []struct {
name string
fields fields
}{
{
name: "wantSuccess - close worker",
fields: fields{stop: make(chan struct{})},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := NewEngine(context.TODO())
d.Start(0)
d.stop = tt.fields.stop
d.Stop()
})
}
}
func TestEngine_dispatch(t *testing.T) {
d := NewEngine(context.TODO())
w := NewWorker(context.TODO(), d.pool)
d.workers = append(d.workers, w)
t.Parallel()
type fields struct {
task *testTask
}
tests := []struct {
name string
fields fields
want string
wantSuccess bool
}{
{
name: "wantSuccess - work test task 1 - Fail false - Delay 100ms",
fields: fields{task: &testTask{ID: 1, Fail: false, Delay: "100ms"}},
want: "completed",
wantSuccess: true,
},
{
name: "Fail - work test task 2 - Delay 200ms - Fail true",
fields: fields{task: &testTask{ID: 2, Delay: "200ms", Fail: true}},
want: "failed",
wantSuccess: false,
},
{
name: "wantSuccess - work test task 3 - Fail false - Delay 300ms",
fields: fields{task: &testTask{ID: 3, Fail: false, Delay: "300ms"}},
want: "completed",
wantSuccess: true,
},
{
name: "Fail - work test task 4 - Fail true - Delay 400ms",
fields: fields{task: &testTask{ID: 4, Fail: true, Delay: "400ms"}},
want: "failed",
wantSuccess: false,
},
{
name: "wantSuccess - work test task 5 - Fail false - Delay 500ms",
fields: fields{task: &testTask{ID: 5, Fail: false, Delay: "500ms"}},
want: "completed",
wantSuccess: true,
},
{
name: "Fail - work test task 6 - Delay 600ms - Fail true",
fields: fields{task: &testTask{ID: 6, Delay: "600ms", Fail: true}},
want: "failed",
wantSuccess: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d.Start(0)
done := make(chan bool)
d.input <- work{
Executable: tt.fields.task,
success: done,
}
if success := <-done; success != tt.wantSuccess {
t.Errorf("worker <- task failed wanted: %v got %v", tt.wantSuccess, success)
}
if tt.fields.task.Status != tt.want {
t.Errorf("worker <- task failed wanted: %s got %s", tt.want, tt.fields.task.Status)
}
})
}
}
func TestEngine_Do(t *testing.T) {
d := NewEngine(context.TODO())
w := NewWorker(context.TODO(), d.pool)
d.workers = append(d.workers, w)
t.Parallel()
type fields struct {
task *testTask
}
tests := []struct {
name string
fields fields
want string
wantSuccess bool
}{
{
name: "wantSuccess - work test task 1 - Fail false - Delay 100ms",
fields: fields{task: &testTask{ID: 1, Fail: false, Delay: "100ms"}},
want: "completed",
wantSuccess: true,
},
{
name: "Fail - work test task 2 - Delay 200ms - Fail true",
fields: fields{task: &testTask{ID: 2, Delay: "200ms", Fail: true}},
want: "failed",
wantSuccess: false,
},
{
name: "wantSuccess - work test task 3 - Fail false - Delay 300ms",
fields: fields{task: &testTask{ID: 3, Fail: false, Delay: "300ms"}},
want: "completed",
wantSuccess: true,
},
{
name: "Fail - work test task 4 - Fail true - Delay 400ms",
fields: fields{task: &testTask{ID: 4, Fail: true, Delay: "400ms"}},
want: "failed",
wantSuccess: false,
},
{
name: "wantSuccess - work test task 5 - Fail false - Delay 500ms",
fields: fields{task: &testTask{ID: 5, Fail: false, Delay: "500ms"}},
want: "completed",
wantSuccess: true,
},
{
name: "Fail - work test task 6 - Delay 600ms - Fail true",
fields: fields{task: &testTask{ID: 6, Delay: "600ms", Fail: true}},
want: "failed",
wantSuccess: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d.Start(0)
done := d.Do(tt.fields.task)
if success := <-done; success != tt.wantSuccess {
t.Errorf("worker <- task failed wanted: %v got %v", tt.wantSuccess, success)
}
if tt.fields.task.Status != tt.want {
t.Errorf("worker <- task failed wanted: %s got %s", tt.want, tt.fields.task.Status)
}
})
}
}