/
tx_test.go
101 lines (89 loc) · 2.78 KB
/
tx_test.go
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
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package crdb
import (
"context"
"database/sql"
"fmt"
"testing"
"github.com/cockroachdb/cockroach-go/v2/testserver"
)
// TestExecuteTx verifies transaction retry using the classic
// example of write skew in bank account balance transfers.
func TestExecuteTx(t *testing.T) {
db, stop := testserver.NewDBForTest(t)
defer stop()
ctx := context.Background()
if err := ExecuteTxGenericTest(ctx, stdlibWriteSkewTest{db: db}); err != nil {
t.Fatal(err)
}
}
// TestConfigureRetries verifies that the number of retries can be specified
// via context.
func TestConfigureRetries(t *testing.T) {
ctx := context.Background()
if numRetriesFromContext(ctx) != defaultRetries {
t.Fatal("expect default number of retries")
}
ctx = WithMaxRetries(context.Background(), 123 + defaultRetries)
if numRetriesFromContext(ctx) != defaultRetries + 123 {
t.Fatal("expected default+123 retires")
}
}
type stdlibWriteSkewTest struct {
db *sql.DB
}
var _ WriteSkewTest = stdlibWriteSkewTest{}
func (t stdlibWriteSkewTest) Init(ctx context.Context) error {
initStmt := `
CREATE DATABASE d;
CREATE TABLE d.t (acct INT PRIMARY KEY, balance INT);
INSERT INTO d.t (acct, balance) VALUES (1, 100), (2, 100);
`
_, err := t.db.ExecContext(ctx, initStmt)
return err
}
func (t stdlibWriteSkewTest) ExecuteTx(ctx context.Context, fn func(tx interface{}) error) error {
return ExecuteTx(ctx, t.db, nil /* opts */, func(tx *sql.Tx) error {
return fn(tx)
})
}
func (t stdlibWriteSkewTest) GetBalances(ctx context.Context, txi interface{}) (int, int, error) {
tx := txi.(*sql.Tx)
var rows *sql.Rows
rows, err := tx.QueryContext(ctx, `SELECT balance FROM d.t WHERE acct IN (1, 2);`)
if err != nil {
return 0, 0, err
}
defer rows.Close()
var bal1, bal2 int
balances := []*int{&bal1, &bal2}
i := 0
for ; rows.Next(); i++ {
if err = rows.Scan(balances[i]); err != nil {
return 0, 0, err
}
}
if i != 2 {
return 0, 0, fmt.Errorf("expected two balances; got %d", i)
}
return bal1, bal2, nil
}
func (t stdlibWriteSkewTest) UpdateBalance(
ctx context.Context, txi interface{}, acct, delta int,
) error {
tx := txi.(*sql.Tx)
_, err := tx.ExecContext(ctx, `UPDATE d.t SET balance=balance+$1 WHERE acct=$2;`, delta, acct)
return err
}