111 lines
2.4 KiB
Go
111 lines
2.4 KiB
Go
package initiatorpool
|
|
|
|
import (
|
|
"git.wisehodl.dev/jay/go-honeybee/transport"
|
|
"github.com/stretchr/testify/assert"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestNewPoolConfig(t *testing.T) {
|
|
conf, err := NewPoolConfig()
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, conf, &PoolConfig{
|
|
ConnectionConfig: nil,
|
|
WorkerConfig: nil,
|
|
WorkerFactory: nil,
|
|
})
|
|
}
|
|
|
|
func TestDefaultPoolConfig(t *testing.T) {
|
|
conf := GetDefaultPoolConfig()
|
|
|
|
assert.Equal(t, conf, &PoolConfig{
|
|
ConnectionConfig: nil,
|
|
WorkerConfig: nil,
|
|
WorkerFactory: nil,
|
|
})
|
|
}
|
|
|
|
func TestApplyPoolOptions(t *testing.T) {
|
|
conf := &PoolConfig{}
|
|
err := applyPoolOptions(
|
|
conf,
|
|
WithConnectionConfig(&transport.ConnectionConfig{}),
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 0*time.Second, conf.ConnectionConfig.WriteTimeout)
|
|
}
|
|
|
|
func TestWithConnectionConfig(t *testing.T) {
|
|
conf := &PoolConfig{}
|
|
opt := WithConnectionConfig(&transport.ConnectionConfig{WriteTimeout: 1 * time.Second})
|
|
err := applyPoolOptions(conf, opt)
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, conf.ConnectionConfig)
|
|
assert.Equal(t, 1*time.Second, conf.ConnectionConfig.WriteTimeout)
|
|
|
|
// invalid config is rejected
|
|
conf = &PoolConfig{}
|
|
opt = WithConnectionConfig(&transport.ConnectionConfig{WriteTimeout: -1 * time.Second})
|
|
err = applyPoolOptions(conf, opt)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestValidatePoolConfig(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
conf PoolConfig
|
|
wantErr error
|
|
wantErrText string
|
|
}{
|
|
{
|
|
name: "valid empty",
|
|
conf: *&PoolConfig{},
|
|
},
|
|
{
|
|
name: "valid defaults",
|
|
conf: *GetDefaultPoolConfig(),
|
|
},
|
|
{
|
|
name: "valid complete",
|
|
conf: PoolConfig{
|
|
ConnectionConfig: &transport.ConnectionConfig{},
|
|
},
|
|
},
|
|
{
|
|
name: "invalid connection config",
|
|
conf: PoolConfig{
|
|
ConnectionConfig: &transport.ConnectionConfig{
|
|
Retry: &transport.RetryConfig{
|
|
InitialDelay: 10 * time.Second,
|
|
MaxDelay: 1 * time.Second,
|
|
},
|
|
},
|
|
},
|
|
wantErrText: "initial delay may not exceed maximum delay",
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
err := ValidatePoolConfig(&tc.conf)
|
|
|
|
if tc.wantErr != nil || tc.wantErrText != "" {
|
|
if tc.wantErr != nil {
|
|
assert.ErrorIs(t, err, tc.wantErr)
|
|
}
|
|
|
|
if tc.wantErrText != "" {
|
|
assert.ErrorContains(t, err, tc.wantErrText)
|
|
}
|
|
return
|
|
}
|
|
|
|
assert.NoError(t, err)
|
|
})
|
|
}
|
|
}
|