This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
forked from mattn/go-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite3_opt_unlock_notify_test.go
222 lines (191 loc) · 4.73 KB
/
sqlite3_opt_unlock_notify_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
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
222
// Copyright (C) 2019 Yasuhiro Matsumoto <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
// +build sqlite_unlock_notify
package sqlite3
import (
"database/sql"
"fmt"
"os"
"sync"
"testing"
"time"
)
func TestUnlockNotify(t *testing.T) {
tempFilename := TempFilename(t)
defer os.Remove(tempFilename)
dsn := fmt.Sprintf("file:%s?cache=shared&mode=rwc&_busy_timeout=%d", tempFilename, 500)
db, err := sql.Open("sqlite3", dsn)
if err != nil {
t.Fatal("Failed to open database:", err)
}
defer db.Close()
_, err = db.Exec("CREATE TABLE foo(id INTEGER, status INTEGER)")
if err != nil {
t.Fatal("Failed to create table:", err)
}
tx, err := db.Begin()
if err != nil {
t.Fatal("Failed to begin transaction:", err)
}
_, err = tx.Exec("INSERT INTO foo(id, status) VALUES(1, 100)")
if err != nil {
t.Fatal("Failed to insert null:", err)
}
_, err = tx.Exec("UPDATE foo SET status = 200 WHERE id = 1")
if err != nil {
t.Fatal("Failed to update table:", err)
}
wg := sync.WaitGroup{}
wg.Add(1)
timer := time.NewTimer(500 * time.Millisecond)
go func() {
<-timer.C
err := tx.Commit()
if err != nil {
t.Fatal("Failed to commit transaction:", err)
}
wg.Done()
}()
rows, err := db.Query("SELECT count(*) from foo")
if err != nil {
t.Fatal("Unable to query foo table:", err)
}
if rows.Next() {
var count int
if err := rows.Scan(&count); err != nil {
t.Fatal("Failed to Scan rows", err)
}
}
if err := rows.Err(); err != nil {
t.Fatal("Failed at the call to Next:", err)
}
wg.Wait()
}
func TestUnlockNotifyMany(t *testing.T) {
tempFilename := TempFilename(t)
defer os.Remove(tempFilename)
dsn := fmt.Sprintf("file:%s?cache=shared&mode=rwc&_busy_timeout=%d", tempFilename, 500)
db, err := sql.Open("sqlite3", dsn)
if err != nil {
t.Fatal("Failed to open database:", err)
}
defer db.Close()
_, err = db.Exec("CREATE TABLE foo(id INTEGER, status INTEGER)")
if err != nil {
t.Fatal("Failed to create table:", err)
}
tx, err := db.Begin()
if err != nil {
t.Fatal("Failed to begin transaction:", err)
}
_, err = tx.Exec("INSERT INTO foo(id, status) VALUES(1, 100)")
if err != nil {
t.Fatal("Failed to insert null:", err)
}
_, err = tx.Exec("UPDATE foo SET status = 200 WHERE id = 1")
if err != nil {
t.Fatal("Failed to update table:", err)
}
wg := sync.WaitGroup{}
wg.Add(1)
timer := time.NewTimer(500 * time.Millisecond)
go func() {
<-timer.C
err := tx.Commit()
if err != nil {
t.Fatal("Failed to commit transaction:", err)
}
wg.Done()
}()
const concurrentQueries = 1000
wg.Add(concurrentQueries)
for i := 0; i < concurrentQueries; i++ {
go func() {
rows, err := db.Query("SELECT count(*) from foo")
if err != nil {
t.Fatal("Unable to query foo table:", err)
}
if rows.Next() {
var count int
if err := rows.Scan(&count); err != nil {
t.Fatal("Failed to Scan rows", err)
}
}
if err := rows.Err(); err != nil {
t.Fatal("Failed at the call to Next:", err)
}
wg.Done()
}()
}
wg.Wait()
}
func TestUnlockNotifyDeadlock(t *testing.T) {
tempFilename := TempFilename(t)
defer os.Remove(tempFilename)
dsn := fmt.Sprintf("file:%s?cache=shared&mode=rwc&_busy_timeout=%d", tempFilename, 500)
db, err := sql.Open("sqlite3", dsn)
if err != nil {
t.Fatal("Failed to open database:", err)
}
defer db.Close()
_, err = db.Exec("CREATE TABLE foo(id INTEGER, status INTEGER)")
if err != nil {
t.Fatal("Failed to create table:", err)
}
tx, err := db.Begin()
if err != nil {
t.Fatal("Failed to begin transaction:", err)
}
_, err = tx.Exec("INSERT INTO foo(id, status) VALUES(1, 100)")
if err != nil {
t.Fatal("Failed to insert null:", err)
}
_, err = tx.Exec("UPDATE foo SET status = 200 WHERE id = 1")
if err != nil {
t.Fatal("Failed to update table:", err)
}
wg := sync.WaitGroup{}
wg.Add(1)
timer := time.NewTimer(500 * time.Millisecond)
go func() {
<-timer.C
err := tx.Commit()
if err != nil {
t.Fatal("Failed to commit transaction:", err)
}
wg.Done()
}()
wg.Add(1)
go func() {
tx2, err := db.Begin()
if err != nil {
t.Fatal("Failed to begin transaction:", err)
}
defer tx2.Rollback()
_, err = tx2.Exec("DELETE FROM foo")
if err != nil {
t.Fatal("Failed to delete table:", err)
}
err = tx2.Commit()
if err != nil {
t.Fatal("Failed to commit transaction:", err)
}
wg.Done()
}()
rows, err := tx.Query("SELECT count(*) from foo")
if err != nil {
t.Fatal("Unable to query foo table:", err)
}
if rows.Next() {
var count int
if err := rows.Scan(&count); err != nil {
t.Fatal("Failed to Scan rows", err)
}
}
if err := rows.Err(); err != nil {
t.Fatal("Failed at the call to Next:", err)
}
wg.Wait()
}