Files
golib/env/duration_test.go

43 lines
1.3 KiB
Go

package env
import (
"os"
"testing"
"time"
)
func TestDuration(t *testing.T) {
tests := []struct {
name string
key string
value string
defaultValue time.Duration
expected time.Duration
shouldSet bool
}{
{"valid positive duration", "TEST_DURATION", "100", 0, 100 * time.Nanosecond, true},
{"valid zero", "TEST_DURATION", "0", 10 * time.Second, 0, true},
{"large value", "TEST_DURATION", "1000000000", 0, 1 * time.Second, true},
{"valid negative duration", "TEST_DURATION", "-100", 0, -100 * time.Nanosecond, true},
{"not set", "TEST_DURATION_NOTSET", "", 5 * time.Minute, 5 * time.Minute, false},
{"invalid value", "TEST_DURATION", "not_a_number", 30 * time.Second, 30 * time.Second, true},
{"empty string", "TEST_DURATION", "", 1 * time.Hour, 1 * time.Hour, true},
{"float value", "TEST_DURATION", "10.5", 2 * time.Second, 2 * time.Second, true},
{"very large value", "TEST_DURATION", "9223372036854775807", 0, 9223372036854775807 * time.Nanosecond, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.shouldSet {
os.Setenv(tt.key, tt.value)
defer os.Unsetenv(tt.key)
}
result := Duration(tt.key, tt.defaultValue)
if result != tt.expected {
t.Errorf("Duration() = %v, want %v", result, tt.expected)
}
})
}
}