Worker: Sleep command, return error when sleep time is negative
I need a way to reliably generate task errors, and having a more thorough check on the sleep duration parameter seemed a nice way to create those.
This commit is contained in:
parent
d5d0893b05
commit
ec10128f85
@ -49,7 +49,12 @@ func (ce *CommandExecutor) cmdSleep(ctx context.Context, logger zerolog.Logger,
|
||||
return NewParameterInvalidError("duration_in_seconds", cmd, "bad type %T, expecting int or float", sleepTime)
|
||||
}
|
||||
|
||||
log.Info().Str("duration", duration.String()).Msg("sleep")
|
||||
logger = log.With().Str("duration", duration.String()).Logger()
|
||||
if duration < 0 {
|
||||
logger.Error().Msg("cannot sleep negative durations")
|
||||
return NewParameterInvalidError("duration_in_seconds", cmd, "cannot be negative")
|
||||
}
|
||||
logger.Info().Msg("sleep")
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
|
@ -74,3 +74,24 @@ loop:
|
||||
// Within the step size is precise enough. We're testing our implementation, not the precision of `time.After()`.
|
||||
assert.WithinDuration(t, timeBefore.Add(47*time.Second), timeAfter, timeStepSize)
|
||||
}
|
||||
|
||||
func TestCommandSleepNegative(t *testing.T) {
|
||||
mockCtrl := gomock.NewController(t)
|
||||
defer mockCtrl.Finish()
|
||||
ce, _ := testCommandExecutor(t, mockCtrl)
|
||||
|
||||
ctx := context.Background()
|
||||
taskID := "90e9d656-e201-4ef0-b6b0-c80684fafa27"
|
||||
cmd := api.Command{
|
||||
Name: "sleep",
|
||||
Parameters: map[string]interface{}{"duration_in_seconds": -47},
|
||||
}
|
||||
|
||||
err := ce.Run(ctx, taskID, cmd)
|
||||
var paramErr ParameterInvalidError
|
||||
if assert.ErrorAs(t, err, ¶mErr) {
|
||||
assert.Equal(t, "duration_in_seconds", paramErr.Parameter)
|
||||
assert.Equal(t, -47, paramErr.ParamValue())
|
||||
assert.Equal(t, "cannot be negative", paramErr.Message)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user