Files
threshold/internal/db/db_test.go
T
Timothy Messier fd5d15af27 feat(config): Add database config options for idle connection management (#2176)
This adds two new config options for the `database` stanza:

- `max_idle_connections`
- `max_idle_time`

These correspond to settings for the underlying `sql.DB` instance. They
can be used to have finer control over the controller's database
connection pool. For example this can be used to mitigate the high
connection overhead when connecting to postgres by setting a high
`max_idle_connections` and either setting `max_idle_time` to zero or to
a high duration.

Alternatively, if using a connection pool like pgpool-II or pgbouncer,
`max_idle_connections` can be set to 0.

See:
    https://pkg.go.dev/database/sql#DB.SetMaxIdleConns
    https://pkg.go.dev/database/sql#DB.SetConnMaxIdleTime
    https://www.pgpool.net/mediawiki/index.php/Main_Page
    https://www.pgbouncer.org/
2022-07-05 15:25:55 -04:00

79 lines
1.5 KiB
Go

package db
import (
"context"
"testing"
"github.com/hashicorp/boundary/testing/dbtest"
"github.com/stretchr/testify/require"
)
func TestOpen(t *testing.T) {
ctx := context.Background()
cleanup, url, _, err := dbtest.StartUsingTemplate(dbtest.Postgres)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := cleanup(); err != nil {
t.Error(err)
}
}()
type args struct {
dbType DbType
connectionUrl string
opt []Option
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "valid",
args: args{
dbType: Postgres,
connectionUrl: url,
},
wantErr: false,
},
{
name: "invalid",
args: args{
dbType: Postgres,
connectionUrl: "",
},
wantErr: true,
},
{
name: "invalid - max_open_connections set to 3",
args: args{
dbType: Postgres,
connectionUrl: "",
opt: []Option{WithMaxOpenConnections(3)},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Open(ctx, tt.args.dbType, tt.args.connectionUrl)
defer func() {
if err == nil {
sqlDB, err := got.SqlDB(ctx)
require.NoError(t, err)
err = sqlDB.Close()
require.NoError(t, err)
}
}()
if (err != nil) != tt.wantErr {
t.Errorf("Open() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr && got != nil {
t.Error("Open() wanted error and got != nil")
}
})
}
}