@@ -2,6 +2,7 @@ package supervisor
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"fmt"
6
7
"path/filepath"
7
8
"testing"
@@ -38,8 +39,8 @@ func TestSupervisor(t *testing.T) {
38
39
cmd , err := subprocess .New (ctx , logger , "starting" , "success" , "failed" , "sed" , "-i" , `$a test123` , testFile )
39
40
require .NoError (t , err )
40
41
41
- runner := NewSupervisor (func (ctx context.Context ) * subprocess.Subprocess {
42
- return cmd
42
+ runner := NewSupervisor (func (ctx context.Context ) ( * subprocess.Subprocess , error ) {
43
+ return cmd , nil
43
44
})
44
45
45
46
require .False (t , filesystem .Exists (testFile ))
@@ -55,6 +56,30 @@ func TestSupervisor(t *testing.T) {
55
56
assert .Contains (t , string (written ), "test\n test123\n test123" )
56
57
})
57
58
59
+ t .Run ("with command error" , func (t * testing.T ) {
60
+ ctx , cancel := context .WithTimeout (context .Background (), 50 * time .Millisecond )
61
+ defer cancel ()
62
+
63
+ runner := NewSupervisor (func (ctx context.Context ) (* subprocess.Subprocess , error ) {
64
+ return nil , errors .New ("something happened" )
65
+ })
66
+
67
+ err := runner .Run (ctx )
68
+ errortest .AssertError (t , err , commonerrors .ErrUnexpected )
69
+ })
70
+
71
+ t .Run ("with nil command" , func (t * testing.T ) {
72
+ ctx , cancel := context .WithTimeout (context .Background (), 50 * time .Millisecond )
73
+ defer cancel ()
74
+
75
+ runner := NewSupervisor (func (ctx context.Context ) (* subprocess.Subprocess , error ) {
76
+ return nil , nil
77
+ })
78
+
79
+ err := runner .Run (ctx )
80
+ errortest .AssertError (t , err , commonerrors .ErrUndefined )
81
+ })
82
+
58
83
t .Run ("with pre run" , func (t * testing.T ) {
59
84
if platform .IsWindows () {
60
85
t .SkipNow ()
@@ -72,8 +97,8 @@ func TestSupervisor(t *testing.T) {
72
97
cmd , err := subprocess .New (ctx , logger , "starting" , "success" , "failed" , "echo" , "123" )
73
98
require .NoError (t , err )
74
99
75
- runner := NewSupervisor (func (ctx context.Context ) * subprocess.Subprocess {
76
- return cmd
100
+ runner := NewSupervisor (func (ctx context.Context ) ( * subprocess.Subprocess , error ) {
101
+ return cmd , nil
77
102
}, WithPreStart (func (_ context.Context ) error {
78
103
_ = counter .Inc ()
79
104
return nil
@@ -102,8 +127,8 @@ func TestSupervisor(t *testing.T) {
102
127
cmd , err := subprocess .New (ctx , logger , "starting" , "success" , "failed" , "echo" , "123" )
103
128
require .NoError (t , err )
104
129
105
- runner := NewSupervisor (func (ctx context.Context ) * subprocess.Subprocess {
106
- return cmd
130
+ runner := NewSupervisor (func (ctx context.Context ) ( * subprocess.Subprocess , error ) {
131
+ return cmd , nil
107
132
}, WithPostStart (func (_ context.Context ) error {
108
133
_ = counter .Inc ()
109
134
return nil
@@ -132,8 +157,8 @@ func TestSupervisor(t *testing.T) {
132
157
cmd , err := subprocess .New (ctx , logger , "starting" , "success" , "failed" , "echo" , "123" )
133
158
require .NoError (t , err )
134
159
135
- runner := NewSupervisor (func (ctx context.Context ) * subprocess.Subprocess {
136
- return cmd
160
+ runner := NewSupervisor (func (ctx context.Context ) ( * subprocess.Subprocess , error ) {
161
+ return cmd , nil
137
162
}, WithPostStop (func (_ context.Context , _ error ) error {
138
163
_ = counter .Inc ()
139
164
return nil
@@ -164,8 +189,8 @@ func TestSupervisor(t *testing.T) {
164
189
cmd , err := subprocess .New (ctx , logger , "starting" , "success" , "failed" , "echo" , "123" )
165
190
require .NoError (t , err )
166
191
167
- runner := NewSupervisor (func (ctx context.Context ) * subprocess.Subprocess {
168
- return cmd
192
+ runner := NewSupervisor (func (ctx context.Context ) ( * subprocess.Subprocess , error ) {
193
+ return cmd , nil
169
194
}, WithPreStart (func (_ context.Context ) error {
170
195
_ = counter1 .Inc ()
171
196
return nil
@@ -182,6 +207,81 @@ func TestSupervisor(t *testing.T) {
182
207
assert .Equal (t , counter1 .Load (), counter2 .Load ())
183
208
})
184
209
210
+ t .Run ("with pre run (timeout)" , func (t * testing.T ) {
211
+ if platform .IsWindows () {
212
+ t .SkipNow ()
213
+ }
214
+
215
+ ctx , cancel := context .WithTimeout (context .Background (), 50 * time .Millisecond )
216
+ defer cancel ()
217
+
218
+ logger , err := logs .NewLogrLogger (logstest .NewTestLogger (t ), "Test" )
219
+ require .NoError (t , err )
220
+
221
+ cmd , err := subprocess .New (ctx , logger , "starting" , "success" , "failed" , "echo" , "123" )
222
+ require .NoError (t , err )
223
+
224
+ runner := NewSupervisor (func (ctx context.Context ) (* subprocess.Subprocess , error ) {
225
+ return cmd , nil
226
+ }, WithPreStart (func (_ context.Context ) error {
227
+ return commonerrors .ErrTimeout
228
+ }))
229
+
230
+ err = runner .Run (ctx )
231
+ errortest .AssertError (t , err , commonerrors .ErrTimeout )
232
+ assert .NotContains (t , err .Error (), "error running pre-start hook" )
233
+ })
234
+
235
+ t .Run ("with post run (timeout)" , func (t * testing.T ) {
236
+ if platform .IsWindows () {
237
+ t .SkipNow ()
238
+ }
239
+
240
+ ctx , cancel := context .WithTimeout (context .Background (), 50 * time .Millisecond )
241
+ defer cancel ()
242
+
243
+ logger , err := logs .NewLogrLogger (logstest .NewTestLogger (t ), "Test" )
244
+ require .NoError (t , err )
245
+
246
+ cmd , err := subprocess .New (ctx , logger , "starting" , "success" , "failed" , "echo" , "123" )
247
+ require .NoError (t , err )
248
+
249
+ runner := NewSupervisor (func (ctx context.Context ) (* subprocess.Subprocess , error ) {
250
+ return cmd , nil
251
+ }, WithPostStart (func (_ context.Context ) error {
252
+ return commonerrors .ErrTimeout
253
+ }))
254
+
255
+ err = runner .Run (ctx )
256
+ errortest .AssertError (t , err , commonerrors .ErrTimeout )
257
+ assert .NotContains (t , err .Error (), "error running post-start hook" )
258
+ })
259
+
260
+ t .Run ("with post stop (timeout)" , func (t * testing.T ) {
261
+ if platform .IsWindows () {
262
+ t .SkipNow ()
263
+ }
264
+
265
+ ctx , cancel := context .WithTimeout (context .Background (), 50 * time .Millisecond )
266
+ defer cancel ()
267
+
268
+ logger , err := logs .NewLogrLogger (logstest .NewTestLogger (t ), "Test" )
269
+ require .NoError (t , err )
270
+
271
+ cmd , err := subprocess .New (ctx , logger , "starting" , "success" , "failed" , "echo" , "123" )
272
+ require .NoError (t , err )
273
+
274
+ runner := NewSupervisor (func (ctx context.Context ) (* subprocess.Subprocess , error ) {
275
+ return cmd , nil
276
+ }, WithPostStop (func (_ context.Context , _ error ) error {
277
+ return commonerrors .ErrTimeout
278
+ }))
279
+
280
+ err = runner .Run (ctx )
281
+ errortest .AssertError (t , err , commonerrors .ErrTimeout )
282
+ assert .NotContains (t , err .Error (), "error running post-stop hook" )
283
+ })
284
+
185
285
t .Run ("with cancel" , func (t * testing.T ) {
186
286
ctx , cancel := context .WithCancel (context .Background ())
187
287
defer cancel ()
@@ -195,8 +295,8 @@ func TestSupervisor(t *testing.T) {
195
295
cmd , err := subprocess .New (ctx , logger , "starting" , "success" , "failed" , "sed" , "-i" , `$a test123` , testFile )
196
296
require .NoError (t , err )
197
297
198
- runner := NewSupervisor (func (ctx context.Context ) * subprocess.Subprocess {
199
- return cmd
298
+ runner := NewSupervisor (func (ctx context.Context ) ( * subprocess.Subprocess , error ) {
299
+ return cmd , nil
200
300
})
201
301
202
302
cancel ()
@@ -223,8 +323,8 @@ func TestSupervisor(t *testing.T) {
223
323
cmd , err := subprocess .New (ctx , logger , "starting" , "success" , failMessage , "sed" , "-i" , `$a test123` , testFile )
224
324
require .NoError (t , err )
225
325
226
- runner := NewSupervisor (func (ctx context.Context ) * subprocess.Subprocess {
227
- return cmd
326
+ runner := NewSupervisor (func (ctx context.Context ) ( * subprocess.Subprocess , error ) {
327
+ return cmd , nil
228
328
}, WithHaltingErrors (fmt .Errorf ("%v %v" , failMessage , commonerrors .ErrCancelled )))
229
329
230
330
require .False (t , filesystem .Exists (testFile ))
@@ -262,8 +362,8 @@ func TestSupervisor(t *testing.T) {
262
362
cmd , err := subprocess .New (ctx , logger , "starting" , "success" , "failed" , "sed" , "-i" , `$a test123` , testFile )
263
363
require .NoError (t , err )
264
364
265
- runner := NewSupervisor (func (ctx context.Context ) * subprocess.Subprocess {
266
- return cmd
365
+ runner := NewSupervisor (func (ctx context.Context ) ( * subprocess.Subprocess , error ) {
366
+ return cmd , nil
267
367
}, WithRestartDelay (time .Hour )) // won't have time to restart
268
368
269
369
require .False (t , filesystem .Exists (testFile ))
0 commit comments