This repository has been archived by the owner on Dec 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds configs for timeouts, updates year in license
Signed-off-by: Alex Ellis <[email protected]>
- Loading branch information
Showing
8 changed files
with
171 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright (c) Alex Ellis 2017. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
package test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/openfaas/faas-swarm/types" | ||
) | ||
|
||
type EnvBucket struct { | ||
Items map[string]string | ||
} | ||
|
||
func NewEnvBucket() EnvBucket { | ||
return EnvBucket{ | ||
Items: make(map[string]string), | ||
} | ||
} | ||
|
||
func (e EnvBucket) Getenv(key string) string { | ||
return e.Items[key] | ||
} | ||
|
||
func (e EnvBucket) Setenv(key string, value string) { | ||
e.Items[key] = value | ||
} | ||
|
||
func TestRead_EmptyTimeoutConfig(t *testing.T) { | ||
defaults := NewEnvBucket() | ||
readConfig := types.ReadConfig{} | ||
|
||
config := readConfig.Read(defaults) | ||
defaultVal := time.Duration(10) * time.Second | ||
|
||
if (config.ReadTimeout) != defaultVal { | ||
t.Logf("ReadTimeout want: %s, got %s", defaultVal.String(), config.ReadTimeout.String()) | ||
t.Fail() | ||
} | ||
if (config.WriteTimeout) != defaultVal { | ||
t.Logf("WriteTimeout want: %s, got %s", defaultVal.String(), config.ReadTimeout.String()) | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestRead_ReadAndWriteIntegerTimeoutConfig(t *testing.T) { | ||
defaults := NewEnvBucket() | ||
defaults.Setenv("read_timeout", "10") | ||
defaults.Setenv("write_timeout", "60") | ||
|
||
readConfig := types.ReadConfig{} | ||
config := readConfig.Read(defaults) | ||
|
||
if (config.ReadTimeout) != time.Duration(10)*time.Second { | ||
t.Logf("ReadTimeout incorrect, got: %d\n", config.ReadTimeout) | ||
t.Fail() | ||
} | ||
if (config.WriteTimeout) != time.Duration(60)*time.Second { | ||
t.Logf("WriteTimeout incorrect, got: %d\n", config.WriteTimeout) | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestRead_ReadAndWriteDurationTimeoutConfig(t *testing.T) { | ||
defaults := NewEnvBucket() | ||
defaults.Setenv("read_timeout", "10s") | ||
defaults.Setenv("write_timeout", "60s") | ||
|
||
readConfig := types.ReadConfig{} | ||
config := readConfig.Read(defaults) | ||
|
||
if (config.ReadTimeout) != time.Duration(10)*time.Second { | ||
t.Logf("ReadTimeout incorrect, got: %d\n", config.ReadTimeout) | ||
t.Fail() | ||
} | ||
if (config.WriteTimeout) != time.Duration(60)*time.Second { | ||
t.Logf("WriteTimeout incorrect, got: %d\n", config.WriteTimeout) | ||
t.Fail() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package tests | ||
package test | ||
|
||
import ( | ||
"encoding/json" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) Alex Ellis 2017. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
package types | ||
|
||
import ( | ||
"os" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
// OsEnv implements interface to wrap os.Getenv | ||
type OsEnv struct { | ||
} | ||
|
||
// Getenv wraps os.Getenv | ||
func (OsEnv) Getenv(key string) string { | ||
return os.Getenv(key) | ||
} | ||
|
||
// HasEnv provides interface for os.Getenv | ||
type HasEnv interface { | ||
Getenv(key string) string | ||
} | ||
|
||
// ReadConfig constitutes config from env variables | ||
type ReadConfig struct { | ||
} | ||
|
||
func parseIntOrDurationValue(val string, fallback time.Duration) time.Duration { | ||
if len(val) > 0 { | ||
parsedVal, parseErr := strconv.Atoi(val) | ||
if parseErr == nil && parsedVal >= 0 { | ||
return time.Duration(parsedVal) * time.Second | ||
} | ||
} | ||
|
||
duration, durationErr := time.ParseDuration(val) | ||
if durationErr != nil { | ||
return fallback | ||
} | ||
|
||
return duration | ||
} | ||
|
||
func parseBoolValue(val string, fallback bool) bool { | ||
if len(val) > 0 { | ||
return val == "true" | ||
} | ||
return fallback | ||
} | ||
|
||
// Read fetches config from environmental variables. | ||
func (ReadConfig) Read(hasEnv HasEnv) BootstrapConfig { | ||
cfg := BootstrapConfig{} | ||
|
||
readTimeout := parseIntOrDurationValue(hasEnv.Getenv("read_timeout"), time.Second*10) | ||
writeTimeout := parseIntOrDurationValue(hasEnv.Getenv("write_timeout"), time.Second*10) | ||
|
||
cfg.ReadTimeout = readTimeout | ||
cfg.WriteTimeout = writeTimeout | ||
|
||
return cfg | ||
} | ||
|
||
// BootstrapConfig for the process. | ||
type BootstrapConfig struct { | ||
ReadTimeout time.Duration | ||
WriteTimeout time.Duration | ||
} |