52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// T20-B4 回归:池 profile 生效性。
|
|
func TestDefaultProfile_StatementTimeout30s(t *testing.T) {
|
|
p := DefaultProfile()
|
|
if p.StatementTimeout != 30*time.Second {
|
|
t.Fatalf("default statement_timeout expect 30s, got %s", p.StatementTimeout)
|
|
}
|
|
if p.MaxConns <= 0 {
|
|
t.Fatalf("default MaxConns expect >0, got %d", p.MaxConns)
|
|
}
|
|
}
|
|
|
|
func TestMigrateProfile_NoStatementTimeout(t *testing.T) {
|
|
p := MigrateProfile()
|
|
if p.StatementTimeout != 0 {
|
|
t.Fatalf("migrate profile must not set statement_timeout, got %s", p.StatementTimeout)
|
|
}
|
|
}
|
|
|
|
func TestOpenAppPool_SetsRuntimeParams(t *testing.T) {
|
|
// 直接验证 ParseConfig + profile 注入(不起真实 PG)。
|
|
dsn := "postgres://u:p@127.0.0.1:5432/x?sslmode=disable"
|
|
poolCfg, err := parseWithProfile(dsn, DefaultProfile())
|
|
if err != nil {
|
|
t.Fatalf("parse dsn: %v", err)
|
|
}
|
|
if poolCfg.ConnConfig.RuntimeParams["statement_timeout"] != "30000" {
|
|
t.Fatalf("statement_timeout runtime param expect 30000, got %q",
|
|
poolCfg.ConnConfig.RuntimeParams["statement_timeout"])
|
|
}
|
|
}
|
|
|
|
func TestLongStatementTimeout_Default15m(t *testing.T) {
|
|
if d := LongStatementTimeout(); d != 15*time.Minute {
|
|
t.Fatalf("long timeout default expect 15m, got %s", d)
|
|
}
|
|
}
|
|
|
|
func TestOpenAppPool_InvalidDSN(t *testing.T) {
|
|
_, err := OpenAppPool(context.Background(), "://bad dsn", DefaultProfile())
|
|
if err == nil {
|
|
t.Fatal("invalid dsn must error")
|
|
}
|
|
}
|