perf(db): configure database connection pools
Add configurable PostgreSQL and database/sql pool limits, validate invalid environment values, clamp idle connections, and cover the parsing/application logic with unit tests.
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
package container
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNonNegativeEnvInt(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
raw string
|
||||
fallback int
|
||||
want int
|
||||
}{
|
||||
{name: "uses fallback when unset", raw: "", fallback: 10, want: 10},
|
||||
{name: "accepts positive value", raw: "120", fallback: 10, want: 120},
|
||||
{name: "accepts zero", raw: "0", fallback: 10, want: 0},
|
||||
{name: "rejects negative value", raw: "-1", fallback: 10, want: 10},
|
||||
{name: "rejects invalid value", raw: "many", fallback: 10, want: 10},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Setenv("TEST_DB_POOL_INT", tt.raw)
|
||||
if got := nonNegativeEnvInt("TEST_DB_POOL_INT", tt.fallback); got != tt.want {
|
||||
t.Fatalf("nonNegativeEnvInt() = %d, want %d", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNonNegativeEnvDuration(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
raw string
|
||||
fallback time.Duration
|
||||
want time.Duration
|
||||
}{
|
||||
{name: "uses fallback when unset", raw: "", fallback: 10 * time.Minute, want: 10 * time.Minute},
|
||||
{name: "accepts duration", raw: "5m", fallback: 10 * time.Minute, want: 5 * time.Minute},
|
||||
{name: "accepts zero", raw: "0", fallback: 10 * time.Minute, want: 0},
|
||||
{name: "rejects negative duration", raw: "-1m", fallback: 10 * time.Minute, want: 10 * time.Minute},
|
||||
{name: "rejects invalid duration", raw: "later", fallback: 10 * time.Minute, want: 10 * time.Minute},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Setenv("TEST_DB_POOL_DURATION", tt.raw)
|
||||
if got := nonNegativeEnvDuration("TEST_DB_POOL_DURATION", tt.fallback); got != tt.want {
|
||||
t.Fatalf("nonNegativeEnvDuration() = %s, want %s", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDatabasePoolConfigFromEnv(t *testing.T) {
|
||||
t.Setenv("DB_MAX_OPEN_CONNS", "120")
|
||||
t.Setenv("DB_MAX_IDLE_CONNS", "20")
|
||||
t.Setenv("DB_CONN_MAX_IDLE_TIME", "5m")
|
||||
t.Setenv("DB_CONN_MAX_LIFETIME", "10m")
|
||||
|
||||
got := databasePoolConfigFromEnv()
|
||||
if got.maxOpen != 120 {
|
||||
t.Fatalf("maxOpen = %d, want 120", got.maxOpen)
|
||||
}
|
||||
if got.maxIdle != 20 {
|
||||
t.Fatalf("maxIdle = %d, want 20", got.maxIdle)
|
||||
}
|
||||
if got.maxIdleTime != 5*time.Minute {
|
||||
t.Fatalf("maxIdleTime = %s, want 5m", got.maxIdleTime)
|
||||
}
|
||||
if got.maxLifetime != 10*time.Minute {
|
||||
t.Fatalf("maxLifetime = %s, want 10m", got.maxLifetime)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDatabasePoolConfigClampsIdleToOpen(t *testing.T) {
|
||||
t.Setenv("DB_MAX_OPEN_CONNS", "5")
|
||||
t.Setenv("DB_MAX_IDLE_CONNS", "20")
|
||||
|
||||
got := databasePoolConfigFromEnv()
|
||||
if got.maxIdle != 5 {
|
||||
t.Fatalf("maxIdle = %d, want 5", got.maxIdle)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyDatabasePoolConfig(t *testing.T) {
|
||||
db, err := sql.Open("sqlite3", ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("sql.Open() error = %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
applyDatabasePoolConfig(db, databasePoolConfig{
|
||||
maxOpen: 120,
|
||||
maxIdle: 20,
|
||||
maxIdleTime: 5 * time.Minute,
|
||||
maxLifetime: 10 * time.Minute,
|
||||
})
|
||||
|
||||
if got := db.Stats().MaxOpenConnections; got != 120 {
|
||||
t.Fatalf("MaxOpenConnections = %d, want 120", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user