Add dbx library: PostgreSQL cluster with master/replica routing, retry, health checking

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-23 00:01:15 +03:00
parent 164c6a5723
commit 62df3a2eb3
21 changed files with 1607 additions and 0 deletions

72
config_test.go Normal file
View File

@@ -0,0 +1,72 @@
package dbx
import (
"testing"
"time"
)
func TestConfigDefaults(t *testing.T) {
cfg := Config{}
cfg.defaults()
if cfg.Logger == nil {
t.Error("Logger should not be nil after defaults")
}
if cfg.Retry.MaxAttempts != 3 {
t.Errorf("MaxAttempts = %d, want 3", cfg.Retry.MaxAttempts)
}
if cfg.Retry.BaseDelay != 50*time.Millisecond {
t.Errorf("BaseDelay = %v, want 50ms", cfg.Retry.BaseDelay)
}
if cfg.Retry.MaxDelay != time.Second {
t.Errorf("MaxDelay = %v, want 1s", cfg.Retry.MaxDelay)
}
if cfg.HealthCheck.Interval != 5*time.Second {
t.Errorf("HealthCheck.Interval = %v, want 5s", cfg.HealthCheck.Interval)
}
if cfg.HealthCheck.Timeout != 2*time.Second {
t.Errorf("HealthCheck.Timeout = %v, want 2s", cfg.HealthCheck.Timeout)
}
if cfg.Master.Name != "master" {
t.Errorf("Master.Name = %q, want %q", cfg.Master.Name, "master")
}
}
func TestConfigDefaultsReplicaNames(t *testing.T) {
cfg := Config{
Replicas: []NodeConfig{
{DSN: "a"},
{Name: "custom", DSN: "b"},
{DSN: "c"},
},
}
cfg.defaults()
if cfg.Replicas[0].Name != "replica-1" {
t.Errorf("Replica[0].Name = %q, want %q", cfg.Replicas[0].Name, "replica-1")
}
if cfg.Replicas[1].Name != "custom" {
t.Errorf("Replica[1].Name = %q, want %q", cfg.Replicas[1].Name, "custom")
}
if cfg.Replicas[2].Name != "replica-3" {
t.Errorf("Replica[2].Name = %q, want %q", cfg.Replicas[2].Name, "replica-3")
}
}
func TestConfigDefaultsNoOverwrite(t *testing.T) {
cfg := Config{
Retry: RetryConfig{
MaxAttempts: 5,
BaseDelay: 100 * time.Millisecond,
MaxDelay: 2 * time.Second,
},
}
cfg.defaults()
if cfg.Retry.MaxAttempts != 5 {
t.Errorf("MaxAttempts = %d, want 5", cfg.Retry.MaxAttempts)
}
if cfg.Retry.BaseDelay != 100*time.Millisecond {
t.Errorf("BaseDelay = %v, want 100ms", cfg.Retry.BaseDelay)
}
}