Repository navigation
Expand file tree
/
Copy pathtx_cancel_test.go
More file actions
221 lines (185 loc) · 6.01 KB
/
Copy pathtx_cancel_test.go
File metadata and controls
221 lines (185 loc) · 6.01 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package sqlpro
import (
"context"
"database/sql"
"errors"
"fmt"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const pgTestDSN = "host=localhost port=5432 dbname=apitest password=egal sslmode=disable"
// openPG opens the local postgres used by the COPY tests, or skips.
func openPG(t *testing.T) DB {
t.Helper()
db, err := Open(POSTGRES, pgTestDSN)
if err != nil {
t.Skipf("no local postgres: %s", err)
}
var one int
if err := db.Query(&one, `SELECT 1`); err != nil {
db.Close()
t.Skipf("no local postgres: %s", err)
}
return db
}
// A cancelled caller context must not end the transaction underneath the
// running job. Against the fake driver (fakedb_test.go), so it runs without a
// database: database/sql's own ctx rollback would show up as a ROLLBACK
// recorded while the job is still running.
func TestFakeExecTXCancelRollsBackOnlyAfterJob(t *testing.T) {
db, backend := newFakeSqlPro(t, POSTGRES)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
jobErr := errors.New("job gave up")
var duringJob []string
err := db.ExecTX(ctx, func(ctx context.Context) error {
cancel()
time.Sleep(200 * time.Millisecond)
duringJob = backend.recorded("rollback")
return jobErr
}, nil)
assert.Empty(t, duringJob, "the TX must still be open while the job runs")
assert.Equal(t, []string{"ROLLBACK"}, backend.recorded("rollback"),
"exactly one rollback, issued by ExecTX after the job returned")
assert.ErrorIs(t, err, jobErr)
assert.NotErrorIs(t, err, sql.ErrTxDone)
}
// Same invariant against a real postgres and the pgx driver, which is where
// the concurrent rollback panics.
func TestExecTXCancelKeepsTXUntilJobReturns(t *testing.T) {
db := openPG(t)
defer db.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
jobErr := errors.New("job gave up")
var afterCancelErr error
err := db.ExecTX(ctx, func(ctx context.Context) error {
tx := CtxTX(ctx)
if err := tx.ExecContext(ctx, `CREATE TEMP TABLE cancel_example (id int)`); err != nil {
return err
}
if err := tx.ExecContext(ctx, `INSERT INTO cancel_example (id) VALUES (1)`); err != nil {
return err
}
// The caller goes away while the job is between statements.
cancel()
time.Sleep(200 * time.Millisecond)
// The TX is still ours: a statement on a live context must still run.
// With database/sql's own ctx rollback this fails with sql.ErrTxDone.
var n int
afterCancelErr = tx.QueryContext(context.WithoutCancel(ctx), &n,
`SELECT count(*) FROM cancel_example`)
if afterCancelErr == nil {
assert.Equal(t, 1, n)
}
return jobErr
}, nil)
assert.NoError(t, afterCancelErr, "TX must still be usable after the caller cancelled")
assert.ErrorIs(t, err, jobErr)
assert.NotErrorIs(t, err, sql.ErrTxDone,
"rollback must be ExecTX's own, not a leftover of database/sql's ctx rollback")
}
// A cancelled context closes the pgx connection of the statement it
// interrupts. ExecTX must drop that connection, not pool it: database/sql
// cannot tell it is dead and would hand it to the next transaction.
func TestExecTXCancelDropsClosedConn(t *testing.T) {
db := openPG(t)
defer db.Close()
db.DB().SetMaxOpenConns(1)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
time.AfterFunc(100*time.Millisecond, cancel)
err := db.ExecTX(ctx, func(ctx context.Context) error {
var n int
return CtxTX(ctx).QueryContext(ctx, &n, `SELECT 1 FROM pg_sleep(5)`)
}, nil)
assert.ErrorIs(t, err, context.Canceled)
assert.Equal(t, 0, db.DB().Stats().OpenConnections, "the closed connection must not stay in the pool")
var n int
err = db.ExecTX(context.Background(), func(ctx context.Context) error {
return CtxTX(ctx).QueryContext(ctx, &n, `SELECT 1`)
}, nil)
assert.NoError(t, err)
assert.Equal(t, 1, n)
}
// Cancelling in-flight statements must abort the job and roll back, without a
// concurrent rollback racing the driver connection. Run with -race.
func TestExecTXCancelInFlightRace(t *testing.T) {
db := openPG(t)
defer db.Close()
const rounds = 60
var wg sync.WaitGroup
for i := range rounds {
wg.Add(1)
go func(i int) {
defer wg.Done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// stagger the cancel across the statement's runtime
time.AfterFunc(time.Duration(i%20)*time.Millisecond, cancel)
err := db.ExecTX(ctx, func(ctx context.Context) error {
tx := CtxTX(ctx)
var n int
return tx.QueryContext(ctx, &n, `SELECT pg_sleep(0.02), 1`)
}, nil)
// cancelled or completed, both fine — the point is no panic and
// no data race
if err != nil && !errors.Is(err, context.Canceled) {
t.Errorf("round %d: unexpected error: %s", i, err)
}
}(i)
}
wg.Wait()
}
// The COPY path bypasses database/sql's per-connection lock, so a bulk insert
// must serialize against statements an adopted goroutine runs on the same TX.
// Run with -race.
func TestCopyFromConcurrentWithAdoptedStatement(t *testing.T) {
db := openPG(t)
defer db.Close()
ctx := context.Background()
err := db.ExecTX(ctx, func(ctx context.Context) error {
tx := CtxTX(ctx)
if err := tx.ExecContext(ctx, `CREATE TEMP TABLE copy_race (id SERIAL PRIMARY KEY, name TEXT, value INTEGER)`); err != nil {
return err
}
id, stop := tx.Lease()
defer stop()
var wg sync.WaitGroup
errs := make([]error, 2)
wg.Add(1)
go func() {
defer wg.Done()
ctx2, release, err := AdoptTX(ctx, id)
if err != nil {
errs[0] = err
return
}
defer release()
errs[0] = db.ExecTX(ctx2, func(ctx context.Context) error {
for i := range 20 {
var n int
if err := CtxTX(ctx).QueryContext(ctx, &n,
fmt.Sprintf(`SELECT %d`, i)); err != nil {
return err
}
}
return nil
}, nil)
}()
wg.Add(1)
go func() {
defer wg.Done()
rows := make([]row, 0, 200)
for i := range 200 {
rows = append(rows, row{Name: fmt.Sprintf("n%d", i), Value: i})
}
errs[1] = tx.InsertBulkContext(ctx, "copy_race", rows)
}()
wg.Wait()
return errors.Join(errs...)
}, nil)
assert.NoError(t, err)
}