From ec10128f8568d0c20e74e652a6e4079257f8c824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 16 Jun 2022 15:46:03 +0200 Subject: [PATCH] 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. --- internal/worker/command_misc.go | 7 ++++++- internal/worker/command_misc_test.go | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/internal/worker/command_misc.go b/internal/worker/command_misc.go index b40d0465..b41720d4 100644 --- a/internal/worker/command_misc.go +++ b/internal/worker/command_misc.go @@ -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(): diff --git a/internal/worker/command_misc_test.go b/internal/worker/command_misc_test.go index f713f888..6c3ad63e 100644 --- a/internal/worker/command_misc_test.go +++ b/internal/worker/command_misc_test.go @@ -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) + } +}