Skip to content

Commit

Permalink
Update to using time duration instead of int for seconds
Browse files Browse the repository at this point in the history
Signed-off-by: Elena Kolevska <[email protected]>
  • Loading branch information
elena-kolevska committed Aug 30, 2024
1 parent 1c38b56 commit 7afed1b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 40 deletions.
16 changes: 8 additions & 8 deletions utils/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 25 additions & 32 deletions utils/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,70 @@ 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)
})
}
}

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,
},
}

Expand All @@ -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)
})
Expand Down

0 comments on commit 7afed1b

Please sign in to comment.