fix: use custom timeout error if the runner times out

This commit is contained in:
Juan Pablo Villafáñez
2024-04-22 12:51:25 +02:00
parent 0d5756bc53
commit 08c47632f0
3 changed files with 37 additions and 3 deletions

View File

@@ -2,7 +2,6 @@ package runner
import (
"context"
"fmt"
"sync/atomic"
"time"
)
@@ -188,7 +187,7 @@ func (r *Runner) doTask(ch chan<- *Result, closeChan bool) {
case d := <-r.interruptedCh:
result = &Result{
RunnerID: r.ID,
RunnerError: fmt.Errorf("runner %s timed out after waiting for %s", r.ID, d.String()),
RunnerError: NewTimeoutError(r.ID, d),
}
case result = <-tmpCh:
// Just assign the received value, nothing else to do

View File

@@ -181,7 +181,11 @@ var _ = Describe("Runner", func() {
// context is done), so test should finish in 4 seconds
Eventually(ctx, ch2).Should(Receive(&expectedResult))
Expect(expectedResult.RunnerID).To(Equal("run003"))
Expect(expectedResult.RunnerError.Error()).To(ContainSubstring("timed out"))
var timeoutError *runner.TimeoutError
Expect(errors.As(expectedResult.RunnerError, &timeoutError)).To(BeTrue())
Expect(timeoutError.RunnerID).To(Equal("run003"))
Expect(timeoutError.Duration).To(Equal(3 * time.Second))
}, SpecTimeout(5*time.Second))
It("Run mutiple times panics", func(ctx SpecContext) {

View File

@@ -1,5 +1,10 @@
package runner
import (
"strings"
"time"
)
// Runable represent a task that can be executed by the Runner.
// It expected to be a long running task with an indefinite execution time,
// so it's suitable for servers or services.
@@ -33,3 +38,29 @@ type Result struct {
RunnerID string
RunnerError error
}
// TimeoutError is an error that should be used for timeouts.
// It implements the `error` interface
type TimeoutError struct {
RunnerID string
Duration time.Duration
}
// NewTimeoutError creates a new timeout error. Both runnerID and duration
// will be used in the error message
func NewTimeoutError(runnerID string, duration time.Duration) *TimeoutError {
return &TimeoutError{
RunnerID: runnerID,
Duration: duration,
}
}
// Error generates the message for this particular error.
func (te *TimeoutError) Error() string {
var sb strings.Builder
sb.WriteString("Runner ")
sb.WriteString(te.RunnerID)
sb.WriteString(" timed out after waiting for ")
sb.WriteString(te.Duration.String())
return sb.String()
}