From 899522da903a99622c1637909693cb06ebbb31a8 Mon Sep 17 00:00:00 2001 From: Tochemey Date: Mon, 16 Sep 2024 20:42:20 +0100 Subject: [PATCH] refactor: enhance code coverage --- cluster/config.go | 6 +- cluster/config_test.go | 204 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 206 insertions(+), 4 deletions(-) create mode 100644 cluster/config_test.go diff --git a/cluster/config.go b/cluster/config.go index 25d8be0..b504f53 100644 --- a/cluster/config.go +++ b/cluster/config.go @@ -73,7 +73,7 @@ var _ validation.Validator = (*Config)(nil) func NewConfig() *Config { return &Config{ host: "0.0.0.0", - maxJoinAttempts: 10, + maxJoinAttempts: 5, joinRetryInterval: time.Second, shutdownTimeout: 3 * time.Second, stateSyncInterval: time.Minute, @@ -146,10 +146,8 @@ func (config *Config) WithReadTimeout(timeout time.Duration) *Config { // Validate implements validation.Validator. func (config *Config) Validate() error { return validation. - New(validation.AllErrors()). + New(validation.FailFast()). AddAssertion(config.provider != nil, "discovery provider is not set"). - AddAssertion(config.discoveryPort > 0, "gossip port is invalid"). - AddAssertion(config.port > 0, "client port is invalid"). AddAssertion(config.joinRetryInterval > 0, "join retry interval is invalid"). AddAssertion(config.shutdownTimeout > 0, "shutdown timeout is invalid"). AddAssertion(config.maxJoinAttempts > 0, "max join attempts is invalid"). diff --git a/cluster/config_test.go b/cluster/config_test.go new file mode 100644 index 0000000..40a9752 --- /dev/null +++ b/cluster/config_test.go @@ -0,0 +1,204 @@ +/* + * MIT License + * + * Copyright (c) 2024 Tochemey + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package cluster + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" + + "github.com/tochemey/gokv/log" + mocks "github.com/tochemey/gokv/mocks/discovery" +) + +func TestConfig(t *testing.T) { + t.Run("With valid config", func(t *testing.T) { + discovery := new(mocks.Provider) + config := NewConfig().WithPort(1234). + WithDiscoveryPort(1235). + WithDiscoveryProvider(discovery). + WithHost("127.0.0.1"). + WithStateSyncInterval(time.Second). + WithLogger(log.DiscardLogger). + WithJoinRetryInterval(time.Second). + WithShutdownTimeout(time.Second). + WithReadTimeout(time.Second) + assert.NoError(t, config.Validate()) + }) + t.Run("With invalid host", func(t *testing.T) { + discovery := new(mocks.Provider) + config := NewConfig(). + WithPort(1234). + WithDiscoveryPort(1235). + WithDiscoveryProvider(discovery). + WithHost(""). // host not provided will return an error + WithStateSyncInterval(time.Second). + WithLogger(log.DiscardLogger). + WithJoinRetryInterval(time.Second). + WithShutdownTimeout(time.Second). + WithReadTimeout(time.Second) + assert.Error(t, config.Validate()) + }) + t.Run("With invalid host", func(t *testing.T) { + discovery := new(mocks.Provider) + config := NewConfig(). + WithPort(1234). + WithDiscoveryPort(1235). + WithDiscoveryProvider(discovery). + WithHost(""). // host not provided will return an error + WithStateSyncInterval(time.Second). + WithLogger(log.DiscardLogger). + WithJoinRetryInterval(time.Second). + WithShutdownTimeout(time.Second). + WithReadTimeout(time.Second) + assert.Error(t, config.Validate()) + }) + t.Run("With invalid host", func(t *testing.T) { + discovery := new(mocks.Provider) + config := NewConfig(). + WithPort(1234). + WithDiscoveryPort(1235). + WithDiscoveryProvider(discovery). + WithHost(""). // host not provided will return an error + WithStateSyncInterval(time.Second). + WithLogger(log.DiscardLogger). + WithJoinRetryInterval(time.Second). + WithShutdownTimeout(time.Second). + WithReadTimeout(time.Second) + assert.Error(t, config.Validate()) + }) + t.Run("With invalid host", func(t *testing.T) { + discovery := new(mocks.Provider) + config := NewConfig(). + WithPort(1234). + WithDiscoveryPort(1235). + WithDiscoveryProvider(discovery). + WithHost(""). // host not provided will return an error + WithStateSyncInterval(time.Second). + WithLogger(log.DiscardLogger). + WithJoinRetryInterval(time.Second). + WithShutdownTimeout(time.Second). + WithReadTimeout(time.Second) + assert.Error(t, config.Validate()) + }) + t.Run("With empty host", func(t *testing.T) { + discovery := new(mocks.Provider) + config := NewConfig(). + WithPort(1234). + WithDiscoveryPort(1235). + WithDiscoveryProvider(discovery). + WithHost(""). + WithStateSyncInterval(time.Second). + WithLogger(log.DiscardLogger). + WithJoinRetryInterval(time.Second). + WithShutdownTimeout(time.Second). + WithReadTimeout(time.Second) + + err := config.Validate() + assert.Error(t, err) + assert.EqualError(t, err, "the [host] is required") + }) + t.Run("With provider not set", func(t *testing.T) { + config := NewConfig(). + WithPort(1234). + WithDiscoveryPort(1235). + WithDiscoveryProvider(nil). + WithHost("127.0.0.1"). + WithStateSyncInterval(time.Second). + WithLogger(log.DiscardLogger). + WithJoinRetryInterval(time.Second). + WithShutdownTimeout(time.Second). + WithReadTimeout(time.Second) + err := config.Validate() + assert.Error(t, err) + assert.EqualError(t, err, "discovery provider is not set") + }) + t.Run("With invalid join retry interval", func(t *testing.T) { + discovery := new(mocks.Provider) + config := NewConfig(). + WithPort(1234). + WithDiscoveryPort(1235). + WithDiscoveryProvider(discovery). + WithHost("127.0.0.1"). + WithLogger(log.DiscardLogger). + WithStateSyncInterval(time.Second). + WithJoinRetryInterval(-1). + WithShutdownTimeout(time.Second). + WithReadTimeout(time.Second) + err := config.Validate() + assert.Error(t, err) + assert.EqualError(t, err, "join retry interval is invalid") + }) + t.Run("With invalid sync retry interval", func(t *testing.T) { + discovery := new(mocks.Provider) + config := NewConfig(). + WithPort(1234). + WithDiscoveryPort(1235). + WithDiscoveryProvider(discovery). + WithHost("127.0.0.1"). + WithLogger(log.DiscardLogger). + WithStateSyncInterval(-1). + WithJoinRetryInterval(time.Second). + WithShutdownTimeout(time.Second). + WithReadTimeout(time.Second) + err := config.Validate() + assert.Error(t, err) + assert.EqualError(t, err, "stateSync interval is invalid") + }) + t.Run("With invalid shutdown timeout", func(t *testing.T) { + discovery := new(mocks.Provider) + config := NewConfig(). + WithPort(1234). + WithDiscoveryPort(1235). + WithDiscoveryProvider(discovery). + WithHost("127.0.0.1"). + WithLogger(log.DiscardLogger). + WithStateSyncInterval(time.Second). + WithJoinRetryInterval(time.Second). + WithShutdownTimeout(-1). + WithReadTimeout(time.Second) + err := config.Validate() + assert.Error(t, err) + assert.EqualError(t, err, "shutdown timeout is invalid") + }) + t.Run("With invalid max join attempts", func(t *testing.T) { + discovery := new(mocks.Provider) + config := NewConfig(). + WithPort(1234). + WithDiscoveryPort(1235). + WithDiscoveryProvider(discovery). + WithHost("127.0.0.1"). + WithLogger(log.DiscardLogger). + WithStateSyncInterval(time.Second). + WithJoinRetryInterval(time.Second). + WithShutdownTimeout(time.Second). + WithMaxJoinAttempts(-1). + WithReadTimeout(time.Second) + err := config.Validate() + assert.Error(t, err) + assert.EqualError(t, err, "max join attempts is invalid") + }) +}