From 7141efb7c79d18dbbaef4f4a87f6e4cb6dba42bc Mon Sep 17 00:00:00 2001 From: Bouke van der Bijl Date: Fri, 25 Oct 2019 09:37:54 +0000 Subject: [PATCH] connector: don't return ErrBadConn when failing to connect ErrBadConn should only be returned for an already established connection, not when creating a new one. --- AUTHORS | 1 + connector.go | 4 ---- connector_test.go | 30 ++++++++++++++++++++++++++++++ driver_test.go | 2 +- 4 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 connector_test.go diff --git a/AUTHORS b/AUTHORS index d87414e82..9765b5348 100644 --- a/AUTHORS +++ b/AUTHORS @@ -91,6 +91,7 @@ Zhenye Xie Barracuda Networks, Inc. Counting Ltd. +DigitalOcean Inc. Facebook Inc. GitHub Inc. Google Inc. diff --git a/connector.go b/connector.go index 5aaaba43e..fd7e76924 100644 --- a/connector.go +++ b/connector.go @@ -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 } diff --git a/connector_test.go b/connector_test.go new file mode 100644 index 000000000..976903c5b --- /dev/null +++ b/connector_test.go @@ -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) + } +} diff --git a/driver_test.go b/driver_test.go index 3dee1bab2..df7353dbb 100644 --- a/driver_test.go +++ b/driver_test.go @@ -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