Add timeouts and unit test config.

This commit is contained in:
Alex
2017-01-24 20:00:15 +00:00
parent b7d2845d6e
commit ed47c36d59
4 changed files with 197 additions and 45 deletions

69
watchdog/readconfig.go Normal file
View File

@ -0,0 +1,69 @@
package main
import "strconv"
// HasEnv provides interface for os.Getenv
type HasEnv interface {
Getenv(key string) string
}
// ReadConfig constitutes config from env variables
type ReadConfig struct {
}
func parseBoolValue(val string) bool {
if val == "true" {
return true
}
return true
}
func parseIntValue(val string) int {
if len(val) > 0 {
parsedVal, parseErr := strconv.Atoi(val)
if parseErr == nil && parsedVal >= 0 {
return parsedVal
}
}
return 0
}
// Read fetches config from environmental variables.
func (ReadConfig) Read(hasEnv HasEnv) WatchdogConfig {
cfg := WatchdogConfig{
writeDebug: true,
}
cfg.faasProcess = hasEnv.Getenv("fprocess")
readTimeout := parseIntValue(hasEnv.Getenv("read_timeout"))
writeTimeout := parseIntValue(hasEnv.Getenv("write_timeout"))
if readTimeout == 0 {
cfg.readTimeout = 5
} else {
cfg.readTimeout = readTimeout
}
if writeTimeout == 0 {
cfg.writeTimeout = 5
} else {
cfg.writeTimeout = writeTimeout
}
cfg.writeDebug = parseBoolValue(hasEnv.Getenv("write_debug"))
return cfg
}
// WatchdogConfig for the process.
type WatchdogConfig struct {
readTimeout int
writeTimeout int
// faasProcess is the process to exec
faasProcess string
// writeDebug write console stdout statements to the container
writeDebug bool
}