@@ -2,6 +2,7 @@ package taskrunner_test
22
33import (
44 "context"
5+ "errors"
56 "testing"
67 "time"
78
@@ -194,3 +195,63 @@ func TestExecutorInvalidations(t *testing.T) {
194195 mockB .Reset ()
195196 }
196197}
198+
199+ func TestExecutorErrorHandling (t * testing.T ) {
200+ config := & config.Config {}
201+
202+ for _ , testcase := range []struct {
203+ Name string
204+ Test func (t * testing.T )
205+ }{
206+ {
207+ "shell command error" ,
208+ func (t * testing.T ) {
209+ executor := taskrunner .NewExecutor (config , []* taskrunner.Task {
210+ {
211+ Name : "shell-error" ,
212+ Run : func (ctx context.Context , shellRun shell.ShellRun ) error {
213+ return shellRun (ctx , "invalid_command_that_will_fail" )
214+ },
215+ },
216+ })
217+
218+ events := executor .Subscribe ()
219+ go func () {
220+ event := consumeUntil (t , events , taskrunner .ExecutorEventKind_TaskFailed )
221+ assert .Contains (t , event .(* taskrunner.TaskFailedEvent ).Error .Error (),
222+ "Executor failed to run shell command" )
223+ }()
224+
225+ err := executor .Run (context .Background (), []string {"shell-error" }, & taskrunner.Runtime {})
226+ assert .Error (t , err )
227+ assert .Contains (t , err .Error (), "Executor failed to run task" )
228+ },
229+ },
230+ {
231+ "task execution error" ,
232+ func (t * testing.T ) {
233+ executor := taskrunner .NewExecutor (config , []* taskrunner.Task {
234+ {
235+ Name : "failing-task" ,
236+ Run : func (ctx context.Context , shellRun shell.ShellRun ) error {
237+ return errors .New ("task failed" )
238+ },
239+ },
240+ })
241+
242+ events := executor .Subscribe ()
243+ go func () {
244+ event := consumeUntil (t , events , taskrunner .ExecutorEventKind_TaskFailed )
245+ assert .Contains (t , event .(* taskrunner.TaskFailedEvent ).Error .Error (),
246+ "task failed" )
247+ }()
248+
249+ err := executor .Run (context .Background (), []string {"failing-task" }, & taskrunner.Runtime {})
250+ assert .Error (t , err )
251+ assert .Contains (t , err .Error (), "Executor failed to run task" )
252+ },
253+ },
254+ } {
255+ t .Run (testcase .Name , testcase .Test )
256+ }
257+ }
0 commit comments