Skip to content

Commit 0f257fc

Browse files
iliacimpoesjulienschmidt
authored andcommitted
Call markBadConn in Ping method (go-sql-driver#875)
* Call markBadConn in Ping method * Add myself to AUTHORS
1 parent 361f66e commit 0f257fc

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Hanno Braun <mail at hannobraun.com>
3535
Henri Yandell <flamefew at gmail.com>
3636
Hirotaka Yamamoto <ymmt2005 at gmail.com>
3737
ICHINOSE Shogo <shogo82148 at gmail.com>
38+
Ilia Cimpoes <ichimpoesh at gmail.com>
3839
INADA Naoki <songofacandy at gmail.com>
3940
Jacek Szwec <szwec.jacek at gmail.com>
4041
James Harr <james.harr at gmail.com>

connection.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ func (mc *mysqlConn) Ping(ctx context.Context) (err error) {
475475
defer mc.finish()
476476

477477
if err = mc.writeCommandPacket(comPing); err != nil {
478-
return
478+
return mc.markBadConn(err)
479479
}
480480

481481
return mc.readResultOK()

connection_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ package mysql
1111
import (
1212
"context"
1313
"database/sql/driver"
14+
"errors"
15+
"net"
1416
"testing"
1517
)
1618

@@ -108,3 +110,48 @@ func TestCleanCancel(t *testing.T) {
108110
}
109111
}
110112
}
113+
114+
func TestPingMarkBadConnection(t *testing.T) {
115+
nc := badConnection{err: errors.New("boom")}
116+
ms := &mysqlConn{
117+
netConn: nc,
118+
buf: newBuffer(nc),
119+
maxAllowedPacket: defaultMaxAllowedPacket,
120+
}
121+
122+
err := ms.Ping(context.Background())
123+
124+
if err != driver.ErrBadConn {
125+
t.Errorf("expected driver.ErrBadConn, got %#v", err)
126+
}
127+
}
128+
129+
func TestPingErrInvalidConn(t *testing.T) {
130+
nc := badConnection{err: errors.New("failed to write"), n: 10}
131+
ms := &mysqlConn{
132+
netConn: nc,
133+
buf: newBuffer(nc),
134+
maxAllowedPacket: defaultMaxAllowedPacket,
135+
closech: make(chan struct{}),
136+
}
137+
138+
err := ms.Ping(context.Background())
139+
140+
if err != ErrInvalidConn {
141+
t.Errorf("expected ErrInvalidConn, got %#v", err)
142+
}
143+
}
144+
145+
type badConnection struct {
146+
n int
147+
err error
148+
net.Conn
149+
}
150+
151+
func (bc badConnection) Write(b []byte) (n int, err error) {
152+
return bc.n, bc.err
153+
}
154+
155+
func (bc badConnection) Close() error {
156+
return nil
157+
}

0 commit comments

Comments
 (0)