mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 15:10:01 +08:00
fix: timeout on query should return context.DeadlineExceeded (#4060)
This commit is contained in:
@@ -296,7 +296,7 @@ func (db *commonSqlConn) queryRows(ctx context.Context, scanner func(*sql.Rows)
|
||||
|
||||
return query(ctx, conn, func(rows *sql.Rows) error {
|
||||
e := scanner(rows)
|
||||
if e != nil {
|
||||
if e != nil && !errors.Is(e, context.DeadlineExceeded) {
|
||||
scanFailed = true
|
||||
}
|
||||
return e
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -290,6 +291,24 @@ func TestStmtBreaker(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestQueryScanTimeout(t *testing.T) {
|
||||
dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
|
||||
defer cancel()
|
||||
row := sqlmock.NewRows([]string{"foo"})
|
||||
for i := 0; i < 10000; i++ {
|
||||
row = row.AddRow("bar" + strconv.Itoa(i))
|
||||
}
|
||||
var val []struct {
|
||||
Foo int
|
||||
Bar string
|
||||
}
|
||||
conn := NewSqlConnFromDB(db)
|
||||
err := conn.QueryRowsCtx(ctx, &val, "any")
|
||||
assert.ErrorIs(t, err, context.DeadlineExceeded)
|
||||
})
|
||||
}
|
||||
|
||||
type mockedSessionConn struct {
|
||||
lastInsertId int64
|
||||
rowsAffected int64
|
||||
|
||||
Reference in New Issue
Block a user