Skip to content

Commit

Permalink
refactor: enhance code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Tochemey committed Sep 16, 2024
1 parent 821f33c commit 899522d
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 4 deletions.
6 changes: 2 additions & 4 deletions cluster/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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").
Expand Down
204 changes: 204 additions & 0 deletions cluster/config_test.go
Original file line number Diff line number Diff line change
@@ -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")
})
}

0 comments on commit 899522d

Please sign in to comment.