|
| 1 | +// Copyright 2022 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | +// implied. See the License for the specific language governing |
| 13 | +// permissions and limitations under the License. |
| 14 | + |
| 15 | +package crdbpgx |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "github.com/jackc/pgx/v5" |
| 21 | + "github.com/jackc/pgx/v5/pgxpool" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/cockroachdb/cockroach-go/v2/crdb" |
| 25 | + "github.com/cockroachdb/cockroach-go/v2/testserver" |
| 26 | +) |
| 27 | + |
| 28 | +// TestExecuteTx verifies transaction retry using the classic |
| 29 | +// example of write skew in bank account balance transfers. |
| 30 | +func TestExecuteTx(t *testing.T) { |
| 31 | + ts, err := testserver.NewTestServer() |
| 32 | + if err != nil { |
| 33 | + t.Fatal(err) |
| 34 | + } |
| 35 | + ctx := context.Background() |
| 36 | + |
| 37 | + pool, err := pgxpool.New(ctx, ts.PGURL().String()) |
| 38 | + if err != nil { |
| 39 | + t.Fatal(err) |
| 40 | + } |
| 41 | + |
| 42 | + if err := crdb.ExecuteTxGenericTest(ctx, pgxWriteSkewTest{pool: pool}); err != nil { |
| 43 | + t.Fatal(err) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +type pgxWriteSkewTest struct { |
| 48 | + pool *pgxpool.Pool |
| 49 | +} |
| 50 | + |
| 51 | +func (t pgxWriteSkewTest) Init(ctx context.Context) error { |
| 52 | + initStmt := ` |
| 53 | +CREATE DATABASE d; |
| 54 | +CREATE TABLE d.t (acct INT PRIMARY KEY, balance INT); |
| 55 | +INSERT INTO d.t (acct, balance) VALUES (1, 100), (2, 100); |
| 56 | +` |
| 57 | + _, err := t.pool.Exec(ctx, initStmt) |
| 58 | + return err |
| 59 | +} |
| 60 | + |
| 61 | +var _ crdb.WriteSkewTest = pgxWriteSkewTest{} |
| 62 | + |
| 63 | +// ExecuteTx is part of the crdb.WriteSkewTest interface. |
| 64 | +func (t pgxWriteSkewTest) ExecuteTx(ctx context.Context, fn func(tx interface{}) error) error { |
| 65 | + return ExecuteTx(ctx, t.pool, pgx.TxOptions{}, func(tx pgx.Tx) error { |
| 66 | + return fn(tx) |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +// GetBalances is part of the crdb.WriteSkewTest interface. |
| 71 | +func (t pgxWriteSkewTest) GetBalances(ctx context.Context, txi interface{}) (int, int, error) { |
| 72 | + tx := txi.(pgx.Tx) |
| 73 | + var rows pgx.Rows |
| 74 | + rows, err := tx.Query(ctx, `SELECT balance FROM d.t WHERE acct IN (1, 2);`) |
| 75 | + if err != nil { |
| 76 | + return 0, 0, err |
| 77 | + } |
| 78 | + defer rows.Close() |
| 79 | + var bal1, bal2 int |
| 80 | + balances := []*int{&bal1, &bal2} |
| 81 | + i := 0 |
| 82 | + for ; rows.Next(); i++ { |
| 83 | + if err = rows.Scan(balances[i]); err != nil { |
| 84 | + return 0, 0, err |
| 85 | + } |
| 86 | + } |
| 87 | + if i != 2 { |
| 88 | + return 0, 0, fmt.Errorf("expected two balances; got %d", i) |
| 89 | + } |
| 90 | + return bal1, bal2, nil |
| 91 | +} |
| 92 | + |
| 93 | +// UpdateBalance is part of the crdb.WriteSkewInterface. |
| 94 | +func (t pgxWriteSkewTest) UpdateBalance( |
| 95 | + ctx context.Context, txi interface{}, acct, delta int, |
| 96 | +) error { |
| 97 | + tx := txi.(pgx.Tx) |
| 98 | + _, err := tx.Exec(ctx, `UPDATE d.t SET balance=balance+$1 WHERE acct=$2;`, delta, acct) |
| 99 | + return err |
| 100 | +} |
0 commit comments