-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorchestration_test.go
284 lines (255 loc) · 6.5 KB
/
orchestration_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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package plumber_test
import (
"context"
"errors"
"fmt"
"log"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/getoutreach/plumber"
"gotest.tools/v3/assert"
)
func reportingRunner(name string, fce ...func()) plumber.Runner {
return plumber.GracefulRunner(func(ctx context.Context) error {
fmt.Println("runner", name, "started")
for _, f := range fce {
f()
}
return nil
}, func(ctx context.Context) error {
fmt.Println("runner", name, "closed")
return nil
})
}
// nolint: unparam //Why: not yet
func reportingBlockingRunner(name string, fce ...func()) plumber.Runner {
return plumber.GracefulRunner(func(ctx context.Context) error {
fmt.Println("runner", name, "started")
for _, f := range fce {
f()
}
<-ctx.Done()
return nil
}, func(ctx context.Context) error {
fmt.Println("runner", name, "closed")
return nil
})
}
func erroringRunner(name string) plumber.Runner {
return plumber.NewRunner(func(ctx context.Context) error {
return errors.New("runner " + name + " failed")
})
}
// nolint: unused //Why: not yet
func erroringCloser(name string) plumber.Runner {
return plumber.Closer(func(ctx context.Context) error {
return errors.New("runner " + name + " failed")
})
}
func TestCloseTimeout(t *testing.T) {
ctx := context.Background()
err := plumber.Start(ctx,
plumber.Pipeline(
reportingBlockingRunner("runner 1"),
reportingBlockingRunner("runner 2"),
reportingBlockingRunner("runner 3"),
),
plumber.TTL(10*time.Millisecond),
plumber.CloseTimeout(100*time.Millisecond),
)
if !errors.Is(err, context.DeadlineExceeded) {
fmt.Printf("err nil or: %v", err)
}
}
func TestPipelineErrors(t *testing.T) {
ctx := context.Background()
err := plumber.Start(ctx,
plumber.Pipeline(
erroringRunner("runner 1"),
erroringRunner("runner 2"),
erroringRunner("runner 3"),
),
plumber.TTL(100*time.Millisecond),
plumber.CloseTimeout(100*time.Millisecond),
func(ctx context.Context, o *plumber.Options) context.Context {
o.Closer(func(ctx context.Context) error {
return errors.New("Closer error")
})
return ctx
},
)
assert.ErrorContains(t, err, "runner runner 1 failed")
assert.ErrorContains(t, err, "Closer error")
}
func TestPipelineSignalerClosing(t *testing.T) {
ctx := context.Background()
err := plumber.Start(ctx,
plumber.Pipeline(
reportingRunner("runner 1", func() {
// Let other job to start as well
time.Sleep(10 * time.Millisecond)
}),
plumber.NewRunner(func(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(25 * time.Second):
return errors.New("runner failed")
}
}),
),
plumber.TTL(50*time.Millisecond),
)
// Context should be canceled since:
// - first runner immediately closes
// - close sequence is initiated
// - closing is immediately done
// - start context is canceled
fmt.Println(err)
assert.Assert(t, errors.Is(err, context.Canceled))
}
func TestPipelineCloseOnError(t *testing.T) {
ctx := context.Background()
closed := make(chan time.Time, 3)
started := time.Now()
reportingClose := func(ctx context.Context) error {
closed <- time.Now()
return nil
}
err := plumber.Start(ctx,
plumber.Pipeline(
plumber.NewRunner(func(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(25 * time.Second):
return errors.New("runner 1 timeout")
}
}, plumber.WithClose(reportingClose)),
plumber.Pipeline(
plumber.NewRunner(func(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(25 * time.Second):
return errors.New("runner 3 timeout")
}
}, plumber.WithClose(reportingClose)),
plumber.NewRunner(func(ctx context.Context) error {
return errors.New("runner 2 failed")
}, plumber.WithClose(reportingClose)),
),
),
plumber.TTL(3*time.Second),
)
close(closed)
// Context should be canceled since:
// - first runner immediately closes
// - close sequence is initiated
// - closing is immediately done
// - start context is canceled
fmt.Println(err)
n := 0
for tm := range closed {
n++
//time.Now().
diff := tm.Sub(started)
fmt.Println(diff)
assert.Assert(t, diff < 100*time.Millisecond)
}
assert.Equal(t, n, 3)
}
func TestPipelineRunnerContextCanceled(t *testing.T) {
ctx := context.Background()
err := plumber.Start(ctx,
plumber.Pipeline(
reportingBlockingRunner("runner 1"),
plumber.ReadyRunner(func(ctx context.Context, ready plumber.ReadyFunc) error {
ready()
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(2 * time.Second):
return errors.New("runner failed")
}
}),
),
plumber.TTL(10*time.Millisecond),
plumber.CloseTimeout(10*time.Millisecond),
)
assert.ErrorContains(t, err, "context deadline exceeded")
}
func TestParallelPipeline(t *testing.T) {
ctx := context.Background()
runner := plumber.ReadyRunner(func(ctx context.Context, ready plumber.ReadyFunc) error {
ready()
time.Sleep(10 * time.Millisecond)
return nil
})
start := time.Now()
err := plumber.Start(ctx,
plumber.Parallel(
runner,
runner,
runner,
runner,
runner,
runner,
),
plumber.TTL(1*time.Second),
)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", time.Since(start))
assert.Assert(t, time.Since(start) <= 15*time.Millisecond)
}
func TestPipelineDetachedContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
var (
logger = log.New(log.Writer(), "test: ", log.LstdFlags|log.Lmicroseconds)
brutallyCanceled int32
)
runner := plumber.PipelineRunner(
plumber.Pipeline(
plumber.Looper(func(ctx context.Context, loop *plumber.Loop) error {
loop.Ready()
for {
select {
case closed := <-loop.Closing():
logger.Print("closing looper")
closed.Success()
return nil
case <-ctx.Done():
atomic.StoreInt32(&brutallyCanceled, 1)
logger.Print("looper context done")
return fmt.Errorf("context brutally canceled: %w", ctx.Err())
default:
logger.Print("looper working")
time.Sleep(30 * time.Millisecond)
}
}
}),
),
plumber.TTL(1*time.Second),
plumber.CloseTimeout(2*time.Second),
plumber.DetachContext(),
)
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
err := runner.Run(ctx)
assert.NilError(t, err)
}()
go func() {
defer wg.Done()
time.Sleep(100 * time.Millisecond)
logger.Print("canceling parent context")
cancel()
}()
wg.Wait()
assert.Assert(t, brutallyCanceled == 0)
}