File tree Expand file tree Collapse file tree 3 files changed +49
-1
lines changed Expand file tree Collapse file tree 3 files changed +49
-1
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ Hanno Braun <mail at hannobraun.com>
3535Henri Yandell <flamefew at gmail.com>
3636Hirotaka Yamamoto <ymmt2005 at gmail.com>
3737ICHINOSE Shogo <shogo82148 at gmail.com>
38+ Ilia Cimpoes <ichimpoesh at gmail.com>
3839INADA Naoki <songofacandy at gmail.com>
3940Jacek Szwec <szwec.jacek at gmail.com>
4041James Harr <james.harr at gmail.com>
Original file line number Diff line number Diff 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 ()
Original file line number Diff line number Diff line change @@ -11,6 +11,8 @@ package mysql
1111import (
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+ }
You can’t perform that action at this time.
0 commit comments