-
Notifications
You must be signed in to change notification settings - Fork 6
/
tx_test.go
150 lines (139 loc) · 3.33 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
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
package pgsql_test
import (
"database/sql"
"fmt"
"log"
"math/rand"
"sync"
"testing"
"github.com/acoshift/pgsql"
)
func TestTx(t *testing.T) {
db := open(t)
defer db.Close()
_, err := db.Exec(`
drop table if exists test_pgsql_tx;
create table test_pgsql_tx (
id int primary key,
value int
);
insert into test_pgsql_tx (
id, value
) values
(0, 0),
(1, 0),
(2, 0);
`)
if err != nil {
t.Fatalf("prepare table error; %v", err)
}
defer db.Exec(`drop table test_pgsql_tx`)
db.SetMaxOpenConns(30)
opts := &pgsql.TxOptions{MaxAttempts: 10}
deposit := func(balance int) error {
return pgsql.RunInTx(db, opts, func(tx *sql.Tx) error {
var err error
// log.Println("deposit", balance)
var acc0, acc1 int
err = tx.QueryRow(`select value from test_pgsql_tx where id = 0`).Scan(&acc0)
if err != nil {
return err
}
err = tx.QueryRow(`select value from test_pgsql_tx where id = 1`).Scan(&acc1)
if err != nil {
return err
}
_, err = tx.Exec(`update test_pgsql_tx set value = $1 where id = 0`, acc0-balance)
if err != nil {
return err
}
_, err = tx.Exec(`update test_pgsql_tx set value = $1 where id = 1`, acc1+balance)
if err != nil {
return err
}
return nil
})
}
withdraw := func(balance int) error {
return pgsql.RunInTx(db, opts, func(tx *sql.Tx) error {
var err error
// log.Println("withdraw", balance)
var acc0, acc1 int
err = tx.QueryRow(`select value from test_pgsql_tx where id = 1`).Scan(&acc1)
if err != nil {
return err
}
if acc1 < balance {
return fmt.Errorf("not enough balance to withdraw")
}
err = tx.QueryRow(`select value from test_pgsql_tx where id = 0`).Scan(&acc0)
if err != nil {
return err
}
_, err = tx.Exec(`update test_pgsql_tx set value = $1 where id = 0`, acc0+balance)
if err != nil {
return err
}
_, err = tx.Exec(`update test_pgsql_tx set value = $1 where id = 1`, acc1-balance)
if err != nil {
return err
}
return nil
})
}
transfer := func(balance int) error {
return pgsql.RunInTx(db, opts, func(tx *sql.Tx) error {
var err error
// log.Println("transfer", balance)
var acc1, acc2 int
err = tx.QueryRow(`select value from test_pgsql_tx where id = 1`).Scan(&acc1)
if err != nil {
return err
}
if acc1 < balance {
return fmt.Errorf("not enough balance to transfer")
}
err = tx.QueryRow(`select value from test_pgsql_tx where id = 2`).Scan(&acc2)
if err != nil {
return err
}
_, err = tx.Exec(`update test_pgsql_tx set value = $1 where id = 1`, acc1-balance)
if err != nil {
return err
}
_, err = tx.Exec(`update test_pgsql_tx set value = $1 where id = 2`, acc2+balance)
if err != nil {
return err
}
return nil
})
}
wg := sync.WaitGroup{}
for i := 0; i < 1000; i++ {
wg.Add(1)
go func() {
var err error
k := rand.Intn(3)
if k == 0 {
err = deposit(rand.Intn(100000))
} else if k == 1 {
err = withdraw(rand.Intn(100000))
} else {
err = transfer(rand.Intn(100000))
}
if err != nil {
log.Println(err)
}
wg.Done()
}()
}
wg.Wait()
var result int
err = db.QueryRow(`select sum(value) from test_pgsql_tx`).Scan(&result)
if err != nil {
t.Fatalf("query result error; %v", err)
}
if result != 0 {
t.Fatalf("expected sum all value to be 0; got %d", result)
}
}