-
Notifications
You must be signed in to change notification settings - Fork 10
/
executor_test.go
196 lines (162 loc) · 5.06 KB
/
executor_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
190
191
192
193
194
195
196
package taskrunner_test
import (
"context"
"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()
}
}