From 7afed1b7cb49ec44342d6d91616e7216dbd68741 Mon Sep 17 00:00:00 2001 From: Elena Kolevska Date: Fri, 30 Aug 2024 14:23:26 +0200 Subject: [PATCH] Update to using time duration instead of int for seconds Signed-off-by: Elena Kolevska --- utils/env.go | 16 ++++++------- utils/env_test.go | 57 +++++++++++++++++++++-------------------------- 2 files changed, 33 insertions(+), 40 deletions(-) diff --git a/utils/env.go b/utils/env.go index 4b5bce4..b99f891 100644 --- a/utils/env.go +++ b/utils/env.go @@ -3,26 +3,26 @@ package utils import ( "fmt" "os" - "strconv" + "time" ) -// GetEnvIntWithRange returns the integer value of the environment variable specified by `envVar`. +// GetEnvDurationWithRange returns the time.Duration value of the environment variable specified by `envVar`. // If the environment variable is not set, it returns `defaultValue`. -// If the value is set but is not valid (not a valid integer or falls outside the specified range -// [minValue, maxValue]), it returns `defaultValue` and an error. -func GetEnvIntWithRange(envVar string, defaultValue int, min int, max int) (int, error) { +// If the value is set but is not valid (not a valid time.Duration or falls outside the specified range +// [minValue, maxValue] inclusively), it returns `defaultValue` and an error. +func GetEnvDurationWithRange(envVar string, defaultValue, min, max time.Duration) (time.Duration, error) { v := os.Getenv(envVar) if v == "" { return defaultValue, nil } - val, err := strconv.Atoi(v) + val, err := time.ParseDuration(v) if err != nil { - return defaultValue, fmt.Errorf("invalid integer value for the %s env variable: %w", envVar, err) + return defaultValue, fmt.Errorf("invalid time.Duration value %s for the %s env variable: %w", val, envVar, err) } if val < min || val > max { - return defaultValue, fmt.Errorf("invalid value for the %s env variable: value should be between %d and %d for best performance, got %d", envVar, min, max, val) + return defaultValue, fmt.Errorf("invalid value for the %s env variable: value should be between %s and %s, got %s", envVar, min, max, val) } return val, nil diff --git a/utils/env_test.go b/utils/env_test.go index b64aa17..a1c0f98 100644 --- a/utils/env_test.go +++ b/utils/env_test.go @@ -2,55 +2,48 @@ package utils import ( "testing" + "time" "github.com/stretchr/testify/require" ) func TestGetEnvIntWithRangeWrongValues(t *testing.T) { - defaultValue := 3 - testValues := []struct { name string envVarVal string - min int - max int + min time.Duration + max time.Duration error string }{ { - "should error if value is not integer number", + "should error if value is not a valid time.Duration", "0.5", - 1, - 2, - "invalid integer value for the MY_ENV env variable", - }, - { - "should error if value is not integer", - "abc", - 1, - 2, - "invalid integer value for the MY_ENV env variable", + time.Second, + 2 * time.Second, + "invalid time.Duration value 0s for the MY_ENV env variable", }, { - "should error if value is lower than 1", - "0", - 1, - 10, - "value should be between 1 and 10", + "should error if value is lower than 1s", + "0s", + time.Second, + 10 * time.Second, + "value should be between 1s and 10s", }, { - "should error if value is higher than 10", - "11", - 1, - 10, - "value should be between 1 and 10", + "should error if value is higher than 10s", + "2m", + time.Second, + 10 * time.Second, + "value should be between 1s and 10s", }, } + defaultValue := 3 * time.Second for _, tt := range testValues { t.Run(tt.name, func(t *testing.T) { t.Setenv("MY_ENV", tt.envVarVal) - val, err := GetEnvIntWithRange("MY_ENV", defaultValue, tt.min, tt.max) + val, err := GetEnvDurationWithRange("MY_ENV", defaultValue, tt.min, tt.max) require.Error(t, err) require.Contains(t, err.Error(), tt.error) require.Equal(t, defaultValue, val) @@ -58,21 +51,21 @@ func TestGetEnvIntWithRangeWrongValues(t *testing.T) { } } -func TestGetEnvIntWithRangeValidValues(t *testing.T) { +func TestGetEnvDurationWithRangeValidValues(t *testing.T) { testValues := []struct { name string envVarVal string - result int + result time.Duration }{ { "should return default value if env variable is not set", "", - 3, + 3 * time.Second, }, { "should return result is env variable value is valid", - "4", - 4, + "4s", + 4 * time.Second, }, } @@ -82,7 +75,7 @@ func TestGetEnvIntWithRangeValidValues(t *testing.T) { t.Setenv("MY_ENV", tt.envVarVal) } - val, err := GetEnvIntWithRange("MY_ENV", 3, 1, 5) + val, err := GetEnvDurationWithRange("MY_ENV", 3*time.Second, time.Second, 5*time.Second) require.NoError(t, err) require.Equal(t, tt.result, val) })