Repository navigation
Expand file tree
/
Copy pathrows_scan_retry_test.go
More file actions
129 lines (117 loc) · 4.58 KB
/
Copy pathrows_scan_retry_test.go
File metadata and controls
129 lines (117 loc) · 4.58 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
package fbx_test
import (
"context"
"errors"
"fmt"
"os"
"testing"
"time"
"github.com/firebirdsql/fbx"
"github.com/firebirdsql/fbx/fbconn"
"github.com/firebirdsql/fbx/fbxtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type retryableRowsScanError struct{ error }
func (retryableRowsScanError) SafeToRetry() bool { return true }
type failingRowsScanner struct{ err error }
func (s failingRowsScanner) ScanRow(fbx.Rows) error { return s.err }
type failingValueScanner struct{ err error }
func (s *failingValueScanner) Scan(any) error { return s.err }
type canceledBlobRowScanner struct{ cancel context.CancelFunc }
func (s canceledBlobRowScanner) ScanRow(rows fbx.Rows) error {
s.cancel()
var value []byte
return rows.Scan(&value)
}
func TestRowsScanFailureDoesNotAuthorizeRetry(t *testing.T) {
if os.Getenv("FBX_TEST_DATABASE") == "" {
t.Skip("FBX_TEST_DATABASE is not configured")
}
for _, failure := range []string{"blob", "row scanner", "value scanner"} {
t.Run(failure, func(t *testing.T) {
for _, queryRow := range []bool{false, true} {
t.Run(fmt.Sprintf("query_row=%t", queryRow), func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
ctr := defaultConnTestRunner
ctr.CreateConfig = func(ctx context.Context, t testing.TB) *fbx.ConnConfig {
config := defaultConnTestRunner.CreateConfig(ctx, t)
config.Autocommit = true
config.MaxInlineBlobSize = 0
return config
}
fbxtest.RunWithQueryExecModes(ctx, t, ctr, nil, func(ctx context.Context, t testing.TB, conn *fbx.Conn) {
table := fmt.Sprintf("FBX_SCAN_%x", time.Now().UnixNano())
_, err := conn.Exec(ctx, "create table "+table+" (id integer, payload blob sub_type 0)")
require.NoError(t, err)
defer func() {
cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
defer cancel()
// Cached verification queries keep the table in use on some servers.
if !assert.NoError(t, conn.DeallocateAll(cleanupCtx)) {
return
}
_, err := conn.Exec(cleanupCtx, "drop table "+table, fbx.QueryExecModeExec)
assert.NoError(t, err)
}()
queryCtx, stop := context.WithCancel(ctx)
defer stop()
cause := error(&retryableRowsScanError{error: errors.New("scanner requested retry")})
require.True(t, fbconn.SafeToRetry(cause))
column := "ID"
var destination any
switch failure {
case "blob":
column, cause = "PAYLOAD", context.Canceled
if queryRow {
destination = canceledBlobRowScanner{cancel: stop}
} else {
destination = new([]byte)
}
case "row scanner":
destination = failingRowsScanner{err: cause}
case "value scanner":
destination = &failingValueScanner{err: cause}
}
sql := "insert into " + table + " values (1, cast('scan payload' as blob sub_type 0)) returning " + column
if queryRow {
err = conn.QueryRow(queryCtx, sql).Scan(destination)
} else {
rows, queryErr := conn.Query(queryCtx, sql)
require.NoError(t, queryErr)
defer rows.Close()
require.True(t, rows.Next())
if failure == "blob" {
stop()
}
err = rows.Scan(destination)
require.ErrorIs(t, rows.Err(), cause)
assert.False(t, fbconn.SafeToRetry(rows.Err()))
assert.Equal(t, rows.Err(), err, "Scan must return the error after cleanup")
assert.False(t, rows.Next())
}
require.ErrorIs(t, err, cause)
assert.False(t, fbconn.SafeToRetry(err), "a scan failure must not authorize another INSERT")
if failure != "row scanner" {
var scanErr fbx.ScanArgError
require.ErrorAs(t, err, &scanErr)
assert.Equal(t, 0, scanErr.ColumnIndex)
assert.Equal(t, column, scanErr.FieldName)
}
require.False(t, conn.IsClosed())
require.False(t, conn.FbConn().IsBusy())
require.False(t, conn.FbConn().ActiveTx())
_, err = conn.Exec(ctx, "insert into "+table+" (id) values (2)")
require.NoError(t, err)
var failedRows, nextRows int
require.NoError(t, conn.QueryRow(ctx, "select cast(count(*) as integer) from "+table+" where id=1").Scan(&failedRows))
require.NoError(t, conn.QueryRow(ctx, "select cast(count(*) as integer) from "+table+" where id=2").Scan(&nextRows))
assert.Zero(t, failedRows, "the failed scan must roll back the pending INSERT")
assert.Equal(t, 1, nextRows, "the next request must not commit changes from the failed scan")
})
})
}
})
}
}