Skip to content

Stop repeated queries #617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1999,3 +1999,33 @@ func TestPing(t *testing.T) {
}
})
}

func TestReadTimeout(t *testing.T) {
runTests(t, dsn+"&readTimeout=500ms", func(dbt *DBTest) {
dbt.mustExec("CREATE TABLE test (v INTEGER)")
startTime := time.Now()

// This query will read-timeout.
if _, err := dbt.db.Exec("INSERT INTO test VALUES (SLEEP(1))"); err == nil {
dbt.Error("expected error")
} else if err, ok := err.(net.Error); !ok || !err.Timeout() {
dbt.Error("expected timeout error")
}

if d := time.Since(startTime); d > time.Second {
dbt.Errorf("too long execution time: %s", d)
}

// Wait for the query has done.
time.Sleep(time.Second)

// Check how many times the query is executed.
var v int
if err := dbt.db.QueryRow("SELECT COUNT(*) FROM test").Scan(&v); err != nil {
dbt.Fatalf("%s", err.Error())
}
if v != 1 {
dbt.Errorf("expected val to be 1, got %d", v)
}
})
}
6 changes: 3 additions & 3 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
}
errLog.Print(err)
mc.Close()
return nil, driver.ErrBadConn
return nil, err
}

// packet length [24 bit]
Expand All @@ -57,7 +57,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
if prevData == nil {
errLog.Print(ErrMalformPkt)
mc.Close()
return nil, driver.ErrBadConn
return nil, ErrMalformPkt
}

return prevData, nil
Expand All @@ -71,7 +71,7 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
}
errLog.Print(err)
mc.Close()
return nil, driver.ErrBadConn
return nil, err
}

// return data if this was the last packet
Expand Down
13 changes: 6 additions & 7 deletions packets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
package mysql

import (
"database/sql/driver"
"errors"
"net"
"testing"
Expand Down Expand Up @@ -252,8 +251,8 @@ func TestReadPacketFail(t *testing.T) {
conn.data = []byte{0x00, 0x00, 0x00, 0x00}
conn.maxReads = 1
_, err := mc.readPacket()
if err != driver.ErrBadConn {
t.Errorf("expected ErrBadConn, got %v", err)
if err != ErrMalformPkt {
t.Errorf("expected %v, got %v", ErrMalformPkt, err)
}

// reset
Expand All @@ -264,8 +263,8 @@ func TestReadPacketFail(t *testing.T) {
// fail to read header
conn.closed = true
_, err = mc.readPacket()
if err != driver.ErrBadConn {
t.Errorf("expected ErrBadConn, got %v", err)
if err != errConnClosed {
t.Errorf("expected %v, got %v", errConnClosed, err)
}

// reset
Expand All @@ -277,7 +276,7 @@ func TestReadPacketFail(t *testing.T) {
// fail to read body
conn.maxReads = 1
_, err = mc.readPacket()
if err != driver.ErrBadConn {
t.Errorf("expected ErrBadConn, got %v", err)
if err != errConnTooManyReads {
t.Errorf("expected %v, got %v", errConnTooManyReads, err)
}
}