-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Created mock delay bechavior * Created added graceful shutdown support * Disable maintidx * Added tests for decode hook * Added tests for context cancelation
- Loading branch information
Evgeny Abramovich
committed
Mar 19, 2023
1 parent
a87b8f5
commit 1cdcd43
Showing
9 changed files
with
265 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package hooks | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
"time" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
func StringToTimeDurationHookFunc() mapstructure.DecodeHookFunc { //nolint: ireturn | ||
return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) { | ||
if f.Kind() != reflect.String { | ||
return data, nil | ||
} | ||
|
||
if t != reflect.TypeOf(time.Second) { | ||
return data, nil | ||
} | ||
|
||
trimmed := strings.ReplaceAll(data.(string), " ", "") //nolint: forcetypeassert | ||
|
||
return time.ParseDuration(trimmed) //nolint:wrapcheck | ||
} | ||
} |
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,103 @@ | ||
package hooks_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/evg4b/uncors/internal/configuration/hooks" | ||
"github.com/evg4b/uncors/testing/testutils" | ||
"github.com/mitchellh/mapstructure" | ||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStringToTimeDurationHookFunc(t *testing.T) { | ||
const key = "duration" | ||
viperInstance := viper.New() | ||
configOption := viper.DecodeHook(mapstructure.ComposeDecodeHookFunc( | ||
hooks.StringToTimeDurationHookFunc(), | ||
mapstructure.OrComposeDecodeHookFunc( | ||
mapstructure.StringToSliceHookFunc(","), | ||
mapstructure.StringToSliceHookFunc(", "), | ||
), | ||
)) | ||
|
||
t.Run("correct parse different formats", func(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
value string | ||
expected time.Duration | ||
}{ | ||
{ | ||
name: "duration with spaces", | ||
value: "1m 4s", | ||
expected: 1*time.Minute + 4*time.Second, | ||
}, | ||
{ | ||
name: "duration without spaces", | ||
value: "3h6m13s", | ||
expected: 3*time.Hour + 6*time.Minute + 13*time.Second, | ||
}, | ||
{ | ||
name: "duration with mixed spaces", | ||
value: "1h 3m59s 40ms", | ||
expected: 1*time.Hour + 3*time.Minute + 59*time.Second + 40*time.Millisecond, | ||
}, | ||
} | ||
|
||
for _, testCase := range tests { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
viperInstance.Set(key, testCase.value) | ||
|
||
durationValue := time.Duration(0) | ||
err := viperInstance.UnmarshalKey(key, &durationValue, configOption) | ||
testutils.CheckNoError(t, err) | ||
|
||
assert.Equal(t, testCase.expected, durationValue) | ||
}) | ||
} | ||
}) | ||
|
||
t.Run("doesnt not affected other type parses", func(t *testing.T) { | ||
t.Run("string to string", func(t *testing.T) { | ||
viperInstance.Set(key, "value") | ||
|
||
stringValue := "" | ||
err := viperInstance.UnmarshalKey(key, &stringValue, configOption) | ||
testutils.CheckNoError(t, err) | ||
|
||
assert.Equal(t, "value", stringValue) | ||
}) | ||
|
||
t.Run("string to []string", func(t *testing.T) { | ||
viperInstance.Set(key, "value,value2") | ||
|
||
stringValue := []string{} | ||
err := viperInstance.UnmarshalKey(key, &stringValue, configOption) | ||
testutils.CheckNoError(t, err) | ||
|
||
assert.Equal(t, []string{"value", "value2"}, stringValue) | ||
}) | ||
|
||
t.Run("number to string", func(t *testing.T) { | ||
viperInstance.Set(key, 11) | ||
|
||
stringValue := "" | ||
err := viperInstance.UnmarshalKey(key, &stringValue, configOption) | ||
testutils.CheckNoError(t, err) | ||
|
||
assert.Equal(t, "11", stringValue) | ||
}) | ||
|
||
t.Run("number to duration", func(t *testing.T) { | ||
const expected = 14 * time.Minute | ||
viperInstance.Set(key, int(expected)) | ||
|
||
durationValue := time.Nanosecond | ||
err := viperInstance.UnmarshalKey(key, &durationValue, configOption) | ||
testutils.CheckNoError(t, err) | ||
|
||
assert.Equal(t, expected, durationValue) | ||
}) | ||
}) | ||
} |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
//nolint:maintidx | ||
package mock_test | ||
|
||
import ( | ||
|
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