Skip to content

Commit

Permalink
connector: don't return ErrBadConn when failing to connect (#1020)
Browse files Browse the repository at this point in the history
ErrBadConn should only be returned for an already established
connection, not when creating a new one.
  • Loading branch information
bouk authored and methane committed Nov 8, 2019
1 parent 296987f commit b57978c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Zhenye Xie <xiezhenye at gmail.com>

Barracuda Networks, Inc.
Counting Ltd.
DigitalOcean Inc.
Facebook Inc.
GitHub Inc.
Google Inc.
Expand Down
4 changes: 0 additions & 4 deletions connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
}

if err != nil {
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
errLog.Print("net.Error from Dial()': ", nerr.Error())
return nil, driver.ErrBadConn
}
return nil, err
}

Expand Down
30 changes: 30 additions & 0 deletions connector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package mysql

import (
"context"
"net"
"testing"
"time"
)

func TestConnectorReturnsTimeout(t *testing.T) {
connector := &connector{&Config{
Net: "tcp",
Addr: "1.1.1.1:1234",
Timeout: 10 * time.Millisecond,
}}

_, err := connector.Connect(context.Background())
if err == nil {
t.Fatal("error expected")
}

if nerr, ok := err.(*net.OpError); ok {
expected := "dial tcp 1.1.1.1:1234: i/o timeout"
if nerr.Error() != expected {
t.Fatalf("expected %q, got %q", expected, nerr.Error())
}
} else {
t.Fatalf("expected %T, got %T", nerr, err)
}
}
2 changes: 1 addition & 1 deletion driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1874,7 +1874,7 @@ func TestDialNonRetryableNetErr(t *testing.T) {

func TestDialTemporaryNetErr(t *testing.T) {
testErr := netErrorMock{temporary: true}
testDialError(t, testErr, driver.ErrBadConn)
testDialError(t, testErr, testErr)
}

// Tests custom dial functions
Expand Down

0 comments on commit b57978c

Please sign in to comment.