diff --git a/watchdog/config_test.go b/watchdog/config_test.go index f0247425..1c22e0cc 100644 --- a/watchdog/config_test.go +++ b/watchdog/config_test.go @@ -162,16 +162,34 @@ func TestRead_ReadAndWriteTimeoutConfig(t *testing.T) { } } +func TestRead_ReadAndWriteTimeoutDurationConfig(t *testing.T) { + defaults := NewEnvBucket() + defaults.Setenv("read_timeout", "20s") + defaults.Setenv("write_timeout", "1m30s") + + readConfig := ReadConfig{} + config := readConfig.Read(defaults) + + if (config.readTimeout) != time.Duration(20)*time.Second { + t.Logf("readTimeout incorrect, got: %d\n", config.readTimeout) + t.Fail() + } + if (config.writeTimeout) != time.Duration(90)*time.Second { + t.Logf("writeTimeout incorrect, got: %d\n", config.writeTimeout) + t.Fail() + } +} + func TestRead_ExecTimeoutConfig(t *testing.T) { defaults := NewEnvBucket() - defaults.Setenv("exec_timeout", "3") + defaults.Setenv("exec_timeout", "3s") readConfig := ReadConfig{} config := readConfig.Read(defaults) want := time.Duration(3) * time.Second if (config.execTimeout) != want { - t.Logf("readTimeout incorrect, got: %d - want: %s\n", config.execTimeout, want) + t.Logf("execTimeout incorrect, got: %d - want: %s\n", config.execTimeout, want) t.Fail() } } diff --git a/watchdog/readconfig.go b/watchdog/readconfig.go index a4d0048f..af5d7c95 100644 --- a/watchdog/readconfig.go +++ b/watchdog/readconfig.go @@ -28,16 +28,19 @@ func parseBoolValue(val string) bool { return false } -func parseIntValue(val string) int { +func parseIntOrDurationValue(val string, fallback time.Duration) time.Duration { if len(val) > 0 { parsedVal, parseErr := strconv.Atoi(val) - if parseErr == nil && parsedVal >= 0 { - - return parsedVal + return time.Duration(parsedVal) * time.Second } } - return 0 + + duration, durationErr := time.ParseDuration(val) + if durationErr != nil { + return fallback + } + return duration } // Read fetches config from environmental variables. @@ -49,21 +52,10 @@ func (ReadConfig) Read(hasEnv HasEnv) WatchdogConfig { cfg.faasProcess = hasEnv.Getenv("fprocess") - readTimeout := parseIntValue(hasEnv.Getenv("read_timeout")) - writeTimeout := parseIntValue(hasEnv.Getenv("write_timeout")) + cfg.readTimeout = parseIntOrDurationValue(hasEnv.Getenv("read_timeout"), time.Second*5) + cfg.writeTimeout = parseIntOrDurationValue(hasEnv.Getenv("write_timeout"), time.Second*5) - cfg.execTimeout = time.Duration(parseIntValue(hasEnv.Getenv("exec_timeout"))) * time.Second - - if readTimeout == 0 { - readTimeout = 5 - } - - if writeTimeout == 0 { - writeTimeout = 5 - } - - cfg.readTimeout = time.Duration(readTimeout) * time.Second - cfg.writeTimeout = time.Duration(writeTimeout) * time.Second + cfg.execTimeout = parseIntOrDurationValue(hasEnv.Getenv("exec_timeout"), time.Second*0) writeDebugEnv := hasEnv.Getenv("write_debug") if isBoolValueSet(writeDebugEnv) {