-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker_test.go
159 lines (151 loc) · 3.54 KB
/
worker_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
package engine
import (
"context"
"testing"
)
func TestWorker_work(t *testing.T) {
type fields struct {
task *testTask
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "wantSuccess - work test task - Fail false",
fields: fields{task: &testTask{ID: 0, Fail: false, Delay: "100ms"}},
want: "completed",
},
{
name: "Fail - work test task - Fail true",
fields: fields{task: &testTask{ID: 0, Fail: true, Delay: "100ms"}},
want: "failed",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &worker{
id: "0",
ctx: context.TODO(),
stop: make(chan struct{}),
input: make(chan work),
pool: make(chan chan work),
}
// start worker
w.Start()
// work task and wait for it to complete
done := make(chan bool)
w.input <- work{
Executable: tt.fields.task,
success: done,
}
<-done
if tt.fields.task.Status != tt.want {
t.Errorf("worker <- task failed wanted: %s got %s", tt.want, tt.fields.task.Status)
}
})
}
}
func TestWorker_workParallel(t *testing.T) {
w := &worker{
id: "0",
ctx: context.TODO(),
stop: make(chan struct{}),
input: make(chan work),
pool: make(chan chan work),
}
// start worker
w.Start()
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) {
// work task and wait for it to complete
done := make(chan bool)
w.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 TestWorker_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) {
w := &worker{
id: "0",
ctx: context.TODO(),
stop: tt.fields.stop,
input: make(chan work),
pool: make(chan chan work),
}
// start worker...
w.Start()
w.Stop()
})
}
}