-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathexecutor_test.go
More file actions
257 lines (217 loc) · 6.73 KB
/
executor_test.go
File metadata and controls
257 lines (217 loc) · 6.73 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package taskrunner_test
import (
"context"
"errors"
"testing"
"time"
"github.com/samsarahq/taskrunner"
"github.com/samsarahq/taskrunner/config"
"github.com/samsarahq/taskrunner/shell"
"github.com/stretchr/testify/assert"
)
type mockFn struct {
Calls []time.Time
}
func (m *mockFn) Fn() {
m.Calls = append(m.Calls, time.Now())
}
func (m *mockFn) ExpectCalls(t *testing.T, n int) {
t.Helper()
assert.Equal(t, n, len(m.Calls), "expected %d calls but received %d", n, len(m.Calls))
}
func (m *mockFn) Reset() {
m.Calls = nil
}
func TestExecutorSimple(t *testing.T) {
config := &config.Config{}
mockA := &mockFn{}
mockB := &mockFn{}
taskA := &taskrunner.Task{
Name: "A",
Run: func(ctx context.Context, shellRun shell.ShellRun) error {
mockA.Fn()
return nil
},
}
taskB := &taskrunner.Task{
Name: "B",
Run: func(ctx context.Context, shellRun shell.ShellRun) error {
mockB.Fn()
return nil
},
Dependencies: []*taskrunner.Task{taskA},
}
tasks := []*taskrunner.Task{taskA, taskB}
ctx := context.Background()
executor := taskrunner.NewExecutor(config, tasks)
for _, testcase := range []struct {
Name string
Test func(t *testing.T)
}{
{
"single task",
func(t *testing.T) {
executor.Run(ctx, []string{"A"}, &taskrunner.Runtime{})
mockA.ExpectCalls(t, 1)
mockB.ExpectCalls(t, 0)
},
},
{
"dependent task",
func(t *testing.T) {
ctx = context.Background()
executor.Run(ctx, []string{"B"}, &taskrunner.Runtime{})
mockA.ExpectCalls(t, 1)
mockB.ExpectCalls(t, 1)
assert.True(t, mockA.Calls[0].UnixNano() < mockB.Calls[0].UnixNano(), "expected B to be called after A")
},
},
} {
t.Run(testcase.Name, testcase.Test)
mockA.Reset()
mockB.Reset()
}
}
type TestInvalidationEvent struct{}
func (f TestInvalidationEvent) Reason() taskrunner.InvalidationReason {
return taskrunner.InvalidationReason_Invalid
}
func (f TestInvalidationEvent) Description() string {
return "the test case decided to invalidate the task"
}
// consumeUntil consumes the events channel until an event matching
// the specified kind appears.
func consumeUntil(t *testing.T, events <-chan taskrunner.ExecutorEvent, kind taskrunner.ExecutorEventKind) taskrunner.ExecutorEvent {
for event := range events {
if event.Kind() == kind {
return event
}
}
t.Fatalf("channel was closed before event was observed")
return nil
}
func TestExecutorInvalidations(t *testing.T) {
config := &config.Config{}
mockA := &mockFn{}
mockB := &mockFn{}
taskA := &taskrunner.Task{
Name: "A",
Run: func(ctx context.Context, shellRun shell.ShellRun) error {
mockA.Fn()
return nil
},
}
taskB := &taskrunner.Task{
Name: "B",
Run: func(ctx context.Context, shellRun shell.ShellRun) error {
mockB.Fn()
return nil
},
Dependencies: []*taskrunner.Task{taskA},
}
tasks := []*taskrunner.Task{taskA, taskB}
for _, testcase := range []struct {
Name string
Test func(t *testing.T)
}{
{
"dependency invalidated",
func(t *testing.T) {
executor := taskrunner.NewExecutor(config, tasks, taskrunner.WithWatchMode(true))
ctx, cancel := context.WithCancel(context.Background())
events := executor.Subscribe()
go func() {
assert.Equal(t, taskA, consumeUntil(t, events, taskrunner.ExecutorEventKind_TaskCompleted).TaskHandler().Definition(), "expected first task to be taskA")
assert.Equal(t, taskB, consumeUntil(t, events, taskrunner.ExecutorEventKind_TaskCompleted).TaskHandler().Definition(), "expected first task to be taskB")
mockA.ExpectCalls(t, 1)
mockB.ExpectCalls(t, 1)
executor.Invalidate(taskA, TestInvalidationEvent{})
assert.Equal(t, taskA, consumeUntil(t, events, taskrunner.ExecutorEventKind_TaskCompleted).TaskHandler().Definition(), "expected first task to be taskA")
assert.Equal(t, taskB, consumeUntil(t, events, taskrunner.ExecutorEventKind_TaskCompleted).TaskHandler().Definition(), "expected first task to be taskB")
mockA.ExpectCalls(t, 2)
mockB.ExpectCalls(t, 2)
cancel()
}()
executor.Run(ctx, []string{"B"}, &taskrunner.Runtime{})
},
},
{
"leaf invalidated",
func(t *testing.T) {
executor := taskrunner.NewExecutor(config, tasks)
ctx, cancel := context.WithCancel(context.Background())
events := executor.Subscribe()
go func() {
assert.Equal(t, taskA, consumeUntil(t, events, taskrunner.ExecutorEventKind_TaskCompleted).TaskHandler().Definition(), "expected first task to be taskA")
assert.Equal(t, taskB, consumeUntil(t, events, taskrunner.ExecutorEventKind_TaskCompleted).TaskHandler().Definition(), "expected first task to be taskB")
mockA.ExpectCalls(t, 1)
mockB.ExpectCalls(t, 1)
executor.Invalidate(taskB, TestInvalidationEvent{})
assert.Equal(t, taskB, consumeUntil(t, events, taskrunner.ExecutorEventKind_TaskCompleted).TaskHandler().Definition(), "expected first task to be taskB")
mockA.ExpectCalls(t, 1)
mockB.ExpectCalls(t, 2)
cancel()
}()
executor.Run(ctx, []string{"B"}, &taskrunner.Runtime{})
},
},
} {
t.Run(testcase.Name, testcase.Test)
mockA.Reset()
mockB.Reset()
}
}
func TestExecutorErrorHandling(t *testing.T) {
config := &config.Config{}
for _, testcase := range []struct {
Name string
Test func(t *testing.T)
}{
{
"shell command error",
func(t *testing.T) {
executor := taskrunner.NewExecutor(config, []*taskrunner.Task{
{
Name: "shell-error",
Run: func(ctx context.Context, shellRun shell.ShellRun) error {
return shellRun(ctx, "invalid_command_that_will_fail")
},
},
})
events := executor.Subscribe()
go func() {
event := consumeUntil(t, events, taskrunner.ExecutorEventKind_TaskFailed)
assert.Contains(t, event.(*taskrunner.TaskFailedEvent).Error.Error(),
"Executor failed to run shell command")
}()
err := executor.Run(context.Background(), []string{"shell-error"}, &taskrunner.Runtime{})
assert.Error(t, err)
assert.Contains(t, err.Error(), "Executor failed to run task")
},
},
{
"task execution error",
func(t *testing.T) {
executor := taskrunner.NewExecutor(config, []*taskrunner.Task{
{
Name: "failing-task",
Run: func(ctx context.Context, shellRun shell.ShellRun) error {
return errors.New("task failed")
},
},
})
events := executor.Subscribe()
go func() {
event := consumeUntil(t, events, taskrunner.ExecutorEventKind_TaskFailed)
assert.Contains(t, event.(*taskrunner.TaskFailedEvent).Error.Error(),
"task failed")
}()
err := executor.Run(context.Background(), []string{"failing-task"}, &taskrunner.Runtime{})
assert.Error(t, err)
assert.Contains(t, err.Error(), "Executor failed to run task")
},
},
} {
t.Run(testcase.Name, testcase.Test)
}
}