//go:build mysql package mysql import ( "context" "database/sql/driver" "errors" "testing" ms "github.com/go-sql-driver/mysql" ) func TestRetryInvalidConnectionRetriesOnce(t *testing.T) { attempts := 0 err := retryInvalidConnection("test", func() error { attempts++ if attempts == 1 { return ms.ErrInvalidConn } return nil }) if err != nil || attempts != 2 { t.Fatalf("err=%v attempts=%d", err, attempts) } } func TestRetryInvalidConnectionDoesNotRetryOtherErrors(t *testing.T) { want := errors.New("write failed") attempts := 0 err := retryInvalidConnection("test", func() error { attempts++ return want }) if !errors.Is(err, want) || attempts != 1 { t.Fatalf("err=%v attempts=%d", err, attempts) } } func TestInstrumentedQueryDiscardsInvalidConnection(t *testing.T) { conn := instrumentedConn{conn: invalidQueryConn{}} _, err := conn.QueryContext(context.Background(), "SELECT 1", nil) if !errors.Is(err, driver.ErrBadConn) { t.Fatalf("err=%v want=%v", err, driver.ErrBadConn) } } type invalidQueryConn struct{} func (invalidQueryConn) Prepare(string) (driver.Stmt, error) { return nil, driver.ErrSkip } func (invalidQueryConn) Close() error { return nil } func (invalidQueryConn) Begin() (driver.Tx, error) { return nil, driver.ErrSkip } func (invalidQueryConn) QueryContext(context.Context, string, []driver.NamedValue) (driver.Rows, error) { return nil, ms.ErrInvalidConn }