-
-
Notifications
You must be signed in to change notification settings - Fork 955
Expand file tree
/
Copy pathissues_test.go
More file actions
135 lines (113 loc) · 2.94 KB
/
issues_test.go
File metadata and controls
135 lines (113 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package pq
import (
"context"
"database/sql"
"testing"
"time"
"github.com/lib/pq/internal/pqtest"
"github.com/lib/pq/pqerror"
)
// #1046: stmt.QueryRowContext doesn't respect canceled context
func TestQueryRowContext(t *testing.T) {
t.Parallel()
db := pqtest.MustDB(t)
ctxTimeout := time.Millisecond * 50
ctx, cancel := context.WithTimeout(context.Background(), ctxTimeout)
defer cancel()
stmt, err := db.PrepareContext(ctx, `SELECT pg_sleep(1) AS id`)
if err != nil {
t.Fatal(err)
}
defer stmt.Close()
var d []uint8
err = stmt.QueryRowContext(ctx).Scan(&d)
dl, _ := ctx.Deadline()
since := time.Since(dl)
if since > ctxTimeout {
t.Logf("FAIL %s: query returned after context deadline: %v\n", t.Name(), since)
t.Fail()
}
mustAs(t, err, pqerror.QueryCanceled)
}
// #1062: drivers.ErrBadConn returned for DB.QueryRowContext.Scan when context is cancelled
func TestQueryRowContextBad(t *testing.T) {
if !pqtest.Pgpool() {
t.Parallel()
}
db := pqtest.MustDB(t)
// Ensure that cancelling a QueryRowContext does not result in an ErrBadConn.
for range 100 {
ctx, cancel := context.WithCancel(context.Background())
go cancel()
row := db.QueryRowContext(ctx, "select 1")
var v int
err := row.Scan(&v)
// nil, context Canceled, and QueryCancelled are all fine.
if err != nil && err != context.Canceled {
mustAs(t, err, pqerror.QueryCanceled)
}
}
}
func connIsValid(t *testing.T, db *sql.DB) {
t.Helper()
ctx := context.Background()
conn, err := db.Conn(ctx)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
// the connection must be valid
err = conn.PingContext(ctx)
if err != nil {
t.Errorf("PingContext err=%#v", err)
}
// close must not return an error
err = conn.Close()
if err != nil {
t.Errorf("Close err=%#v", err)
}
}
func TestQueryCancelRace(t *testing.T) {
db := pqtest.MustDB(t)
// cancel a query while executing on Postgres: must return the cancelled error code
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(10 * time.Millisecond)
cancel()
}()
row := db.QueryRowContext(ctx, "select pg_sleep(0.5)")
var pgSleepVoid string
err := row.Scan(&pgSleepVoid)
mustAs(t, err, pqerror.QueryCanceled)
// get a connection: it must be a valid
connIsValid(t, db)
}
// Test cancelling a scan after it is started. This broke with 1.10.4.
func TestQueryCancelledReused(t *testing.T) {
t.Parallel()
db := pqtest.MustDB(t)
ctx, cancel := context.WithCancel(context.Background())
// run a query that returns a lot of data
rows, err := db.QueryContext(ctx, "select generate_series(1, 10000)")
if err != nil {
t.Fatal(err)
}
defer rows.Close()
// scan the first value
if !rows.Next() {
t.Error("expected rows.Next() to return true")
}
var i int
err = rows.Scan(&i)
if err != nil {
t.Fatal(err)
}
if i != 1 {
t.Error(i)
}
// cancel the context and close rows, ignoring errors
cancel()
rows.Close()
// get a connection: it must be valid
connIsValid(t, db)
}