feat: scaffold MySQL services with lifecycle and readiness checks

This commit is contained in:
2026-09-20 23:12:47 +08:00
parent ff95692edb
commit affd02d5f7
28 changed files with 1153 additions and 63 deletions

View File

@@ -0,0 +1,39 @@
package database
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"sync/atomic"
"testing"
)
var failedPingClosed atomic.Int32
var errPingUnavailable = errors.New("database unavailable")
type failedPingDriver struct{}
type failedPingConnection struct{}
func (failedPingDriver) Open(string) (driver.Conn, error) { return failedPingConnection{}, nil }
func (failedPingConnection) Prepare(string) (driver.Stmt, error) {
return nil, errors.New("not implemented")
}
func (failedPingConnection) Begin() (driver.Tx, error) { return nil, errors.New("not implemented") }
func (failedPingConnection) Close() error { failedPingClosed.Add(1); return nil }
func (failedPingConnection) Ping(context.Context) error { return errPingUnavailable }
func init() { sql.Register("goOmniFailPing", failedPingDriver{}) }
func TestOpenClosesPoolWhenStartupPingFails(t *testing.T) {
failedPingClosed.Store(0)
cfg := DefaultConfig()
cfg.Driver, cfg.DSN = "goOmniFailPing", "test"
pool, err := Open(context.Background(), cfg)
if pool != nil || !errors.Is(err, errPingUnavailable) {
t.Fatalf("unexpected startup result: db=%v err=%v", pool, err)
}
if closed := failedPingClosed.Load(); closed != 1 {
t.Fatalf("startup failure closed %d connections, want 1", closed)
}
}