Use real default values for NewConfig.

This commit is contained in:
Jay
2026-02-03 15:48:12 -05:00
parent 2cb25a6ba2
commit 541ad9f96e
3 changed files with 32 additions and 20 deletions

View File

@@ -35,7 +35,12 @@ func NewConfig(options ...ConfigOption) (*Config, error) {
} }
func GetDefaultConfig() *Config { func GetDefaultConfig() *Config {
return &Config{} return &Config{
CloseHandler: nil,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
Retry: GetDefaultRetryConfig(),
}
} }
func GetDefaultRetryConfig() *RetryConfig { func GetDefaultRetryConfig() *RetryConfig {

View File

@@ -10,10 +10,13 @@ import (
// Config Tests // Config Tests
func TestNewConfig(t *testing.T) { func TestNewConfig(t *testing.T) {
conf, err := NewConfig(WithRetry()) conf, err := NewConfig()
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, conf, &Config{ assert.Equal(t, conf, &Config{
CloseHandler: nil,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
Retry: GetDefaultRetryConfig(), Retry: GetDefaultRetryConfig(),
}) })
@@ -30,8 +33,12 @@ func TestNewConfig(t *testing.T) {
func TestDefaultConfig(t *testing.T) { func TestDefaultConfig(t *testing.T) {
conf := GetDefaultConfig() conf := GetDefaultConfig()
assert.Nil(t, conf.CloseHandler) assert.Equal(t, conf, &Config{
assert.Nil(t, conf.Retry) CloseHandler: nil,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
Retry: GetDefaultRetryConfig(),
})
} }
func TestDefaultRetryConfig(t *testing.T) { func TestDefaultRetryConfig(t *testing.T) {
@@ -48,7 +55,7 @@ func TestDefaultRetryConfig(t *testing.T) {
// Config Builder Tests // Config Builder Tests
func TestSetConfig(t *testing.T) { func TestSetConfig(t *testing.T) {
conf := GetDefaultConfig() conf := &Config{}
err := SetConfig( err := SetConfig(
conf, conf,
WithRetryMaxRetries(0), WithRetryMaxRetries(0),
@@ -73,7 +80,7 @@ func TestSetConfig(t *testing.T) {
// Config Option Tests // Config Option Tests
func TestWithCloseHandler(t *testing.T) { func TestWithCloseHandler(t *testing.T) {
conf := GetDefaultConfig() conf := &Config{}
opt := WithCloseHandler(func(code int, text string) error { return nil }) opt := WithCloseHandler(func(code int, text string) error { return nil })
err := SetConfig(conf, opt) err := SetConfig(conf, opt)
assert.NoError(t, err) assert.NoError(t, err)
@@ -81,21 +88,21 @@ func TestWithCloseHandler(t *testing.T) {
} }
func TestWithReadTimeout(t *testing.T) { func TestWithReadTimeout(t *testing.T) {
conf := GetDefaultConfig() conf := &Config{}
opt := WithReadTimeout(30) opt := WithReadTimeout(30)
err := SetConfig(conf, opt) err := SetConfig(conf, opt)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, conf.ReadTimeout, time.Duration(30)) assert.Equal(t, conf.ReadTimeout, time.Duration(30))
// zero allowed // zero allowed
conf = GetDefaultConfig() conf = &Config{}
opt = WithReadTimeout(0) opt = WithReadTimeout(0)
err = SetConfig(conf, opt) err = SetConfig(conf, opt)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, conf.ReadTimeout, time.Duration(0)) assert.Equal(t, conf.ReadTimeout, time.Duration(0))
// negative disallowed // negative disallowed
conf = GetDefaultConfig() conf = &Config{}
opt = WithReadTimeout(-30) opt = WithReadTimeout(-30)
err = SetConfig(conf, opt) err = SetConfig(conf, opt)
assert.ErrorIs(t, err, errors.InvalidReadTimeout) assert.ErrorIs(t, err, errors.InvalidReadTimeout)
@@ -103,21 +110,21 @@ func TestWithReadTimeout(t *testing.T) {
} }
func TestWithWriteTimeout(t *testing.T) { func TestWithWriteTimeout(t *testing.T) {
conf := GetDefaultConfig() conf := &Config{}
opt := WithWriteTimeout(30) opt := WithWriteTimeout(30)
err := SetConfig(conf, opt) err := SetConfig(conf, opt)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, conf.WriteTimeout, time.Duration(30)) assert.Equal(t, conf.WriteTimeout, time.Duration(30))
// zero allowed // zero allowed
conf = GetDefaultConfig() conf = &Config{}
opt = WithWriteTimeout(0) opt = WithWriteTimeout(0)
err = SetConfig(conf, opt) err = SetConfig(conf, opt)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, conf.WriteTimeout, time.Duration(0)) assert.Equal(t, conf.WriteTimeout, time.Duration(0))
// negative disallowed // negative disallowed
conf = GetDefaultConfig() conf = &Config{}
opt = WithWriteTimeout(-30) opt = WithWriteTimeout(-30)
err = SetConfig(conf, opt) err = SetConfig(conf, opt)
assert.ErrorIs(t, err, errors.InvalidWriteTimeout) assert.ErrorIs(t, err, errors.InvalidWriteTimeout)
@@ -125,7 +132,7 @@ func TestWithWriteTimeout(t *testing.T) {
} }
func TestWithRetry(t *testing.T) { func TestWithRetry(t *testing.T) {
conf := GetDefaultConfig() conf := &Config{}
opt := WithRetry() opt := WithRetry()
err := SetConfig(conf, opt) err := SetConfig(conf, opt)
assert.NoError(t, err) assert.NoError(t, err)
@@ -134,7 +141,7 @@ func TestWithRetry(t *testing.T) {
} }
func TestWithRetryAttempts(t *testing.T) { func TestWithRetryAttempts(t *testing.T) {
conf := GetDefaultConfig() conf := &Config{}
opt := WithRetryMaxRetries(3) opt := WithRetryMaxRetries(3)
err := SetConfig(conf, opt) err := SetConfig(conf, opt)
assert.NoError(t, err) assert.NoError(t, err)
@@ -153,7 +160,7 @@ func TestWithRetryAttempts(t *testing.T) {
} }
func TestWithRetryInitialDelay(t *testing.T) { func TestWithRetryInitialDelay(t *testing.T) {
conf := GetDefaultConfig() conf := &Config{}
opt := WithRetryInitialDelay(10 * time.Second) opt := WithRetryInitialDelay(10 * time.Second)
err := SetConfig(conf, opt) err := SetConfig(conf, opt)
assert.NoError(t, err) assert.NoError(t, err)
@@ -172,7 +179,7 @@ func TestWithRetryInitialDelay(t *testing.T) {
} }
func TestWithRetryMaxDelay(t *testing.T) { func TestWithRetryMaxDelay(t *testing.T) {
conf := GetDefaultConfig() conf := &Config{}
opt := WithRetryMaxDelay(10 * time.Second) opt := WithRetryMaxDelay(10 * time.Second)
err := SetConfig(conf, opt) err := SetConfig(conf, opt)
assert.NoError(t, err) assert.NoError(t, err)
@@ -191,7 +198,7 @@ func TestWithRetryMaxDelay(t *testing.T) {
} }
func TestWithRetryJitterFactor(t *testing.T) { func TestWithRetryJitterFactor(t *testing.T) {
conf := GetDefaultConfig() conf := &Config{}
opt := WithRetryJitterFactor(0.2) opt := WithRetryJitterFactor(0.2)
err := SetConfig(conf, opt) err := SetConfig(conf, opt)
@@ -221,7 +228,7 @@ func TestValidateConfig(t *testing.T) {
}{ }{
{ {
name: "valid empty", name: "valid empty",
conf: *GetDefaultConfig(), conf: *&Config{},
}, },
{ {
name: "valid defaults", name: "valid defaults",

View File

@@ -213,7 +213,7 @@ func TestNewConnectionFromSocket(t *testing.T) {
assert.Equal(t, StateConnected, conn.state) assert.Equal(t, StateConnected, conn.state)
assert.False(t, conn.closed) assert.False(t, conn.closed)
// Verify config defaulting // Verify default config is used if nil is passed
if tc.config == nil { if tc.config == nil {
assert.Equal(t, GetDefaultConfig(), conn.config) assert.Equal(t, GetDefaultConfig(), conn.config)
} else { } else {