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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
# 2020 May 06
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
#
# TESTRUNNER: slow
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/lock_common.tcl
set testprefix walsetlk
ifcapable !wal {finish_test ; return }
db timeout 1000
#-------------------------------------------------------------------------
# 1.*: Test that nothing goes wrong if recovery is forced while opening
# a write transaction or performing a checkpoint with blocking locks.
#
do_execsql_test 1.0 {
CREATE TABLE t1(x, y);
PRAGMA journal_mode = wal;
INSERT INTO t1 VALUES(1, 2);
INSERT INTO t1 VALUES(3, 4);
INSERT INTO t1 VALUES(5, 6);
INSERT INTO t1 VALUES(7, 8);
} {wal}
sqlite3 db2 test.db
db2 timeout 1000
do_execsql_test -db db2 1.1 {
SELECT * FROM t1
} {1 2 3 4 5 6 7 8}
set fd [open test.db-shm r+]
puts $fd "blahblahblahblah"
flush $fd
do_execsql_test 1.2 {
BEGIN;
INSERT INTO t1 VALUES(9, 10);
}
do_execsql_test -db db2 1.3 {
SELECT * FROM t1
} {1 2 3 4 5 6 7 8}
do_test 1.4 {
list [catch {db2 eval { BEGIN EXCLUSIVE }} msg] $msg
} {1 {database is locked}}
do_execsql_test 1.5 { COMMIT }
do_execsql_test -db db2 1.6 {
SELECT * FROM t1
} {1 2 3 4 5 6 7 8 9 10}
puts $fd "blahblahblahblah"
flush $fd
do_execsql_test -db db2 1.7 {
PRAGMA wal_checkpoint = TRUNCATE
} {0 0 0}
do_test 1.8 {
file size test.db-wal
} 0
close $fd
db close
db2 close
#-------------------------------------------------------------------------
do_multiclient_test tn {
testvfs tvfs -fullshm 1
db close
sqlite3 db test.db -vfs tvfs
tvfs script xSleep_callback
tvfs filter xSleep
set ::sleep_count 0
proc xSleep_callback {xSleep nMs} {
after [expr $nMs / 1000]
incr ::sleep_count
}
do_test 2.$tn.1 {
sql1 {
PRAGMA journal_mode = wal;
CREATE TABLE t1(s, v);
INSERT INTO t1 VALUES(1, 2);
INSERT INTO t1 VALUES(3, 4);
INSERT INTO t1 VALUES(5, 6);
}
code1 { db timeout 1100 }
} {}
do_test 2.$tn.2 {
sql2 {
BEGIN;
INSERT INTO t1 VALUES(7, 8);
}
} {}
do_test 2.$tn.3 {
set us [lindex [time { catch {db eval "BEGIN EXCLUSIVE"} }] 0]
expr $us>1000000 && $us<4000000
} {1}
do_test 2.$tn.4 {
sql2 { COMMIT }
sql1 { SELECT * FROM t1 }
} {1 2 3 4 5 6 7 8}
do_test 2.$tn.5 {
sql2 {
BEGIN;
INSERT INTO t1 VALUES(9, 10);
}
} {}
do_test 2.$tn.6 {
set us [lindex [time { catch {db eval "PRAGMA wal_checkpoint=RESTART"} }] 0]
expr $us>1000000 && $us<4000000
} {1}
do_test 2.$tn.7 {
sql2 {
COMMIT;
BEGIN;
SELECT * FROM t1;
}
} {1 2 3 4 5 6 7 8 9 10}
do_test 2.$tn.8 {
set us [lindex [time { catch {db eval "PRAGMA wal_checkpoint=RESTART"} }] 0]
expr $us>1000000 && $us<4000000
} {1}
do_test 2.$tn.9 {
sql3 {
INSERT INTO t1 VALUES(11, 12);
}
sql2 {
COMMIT;
BEGIN;
SELECT * FROM t1;
}
sql3 {
INSERT INTO t1 VALUES(13, 14);
}
} {}
do_test 2.$tn.10 {
set us [lindex [time { catch {db eval "PRAGMA wal_checkpoint=RESTART"} }] 0]
expr $us>1000000 && $us<4000000
} {1}
do_test 2.$tn.11 {
sql3 {
BEGIN;
SELECT * FROM t1;
}
sql1 { INSERT INTO t1 VALUES(15, 16); }
} {}
do_test 2.$tn.12 {
set us [lindex [time { catch {db eval "PRAGMA wal_checkpoint=RESTART"} }] 0]
expr $us>1000000 && $us<4000000
} {1}
do_test 2.$tn.13 {
sql2 {
COMMIT;
BEGIN;
SELECT * FROM t1;
}
sql1 { INSERT INTO t1 VALUES(17, 18); }
} {}
do_test 2.$tn.14 {
set us [lindex [time { catch {db eval "PRAGMA wal_checkpoint=RESTART"} }] 0]
expr $us>1000000 && $us<4000000
} {1}
db close
tvfs delete
# Set bSleep to true if it is expected that the above used xSleep() to
# wait for locks. bSleep is true unless SQLITE_ENABLE_SETLK_TIMEOUT is
# set to 1 and either:
#
# * the OS is windows, or
# * the OS is unix and the tests were run with each connection
# in a separate process.
#
set bSleep 1
if {$::sqlite_options(setlk_timeout)==1} {
if {$::tcl_platform(platform) eq "windows"} {
set bSleep 0
}
if {$::tcl_platform(platform) eq "unix"} {
set bSleep [expr $tn==2]
}
}
do_test 2.$tn.15.$bSleep {
expr $::sleep_count > 0
} $bSleep
}
#-------------------------------------------------------------------------
reset_db
testvfs tvfs -fullshm 1
tvfs script xSleep_callback
tvfs filter xSleep
set ::sleep_count 0
proc xSleep_callback {xSleep nMs} {
after [expr $nMs / 1000]
incr ::sleep_count
breakpoint
}
sqlite3 db2 test.db -vfs tvfs
db2 timeout 1000
do_execsql_test 3.0 {
PRAGMA journal_mode = wal;
CREATE TABLE x1(x, y);
BEGIN;
INSERT INTO x1 VALUES(1, 2);
} {wal}
do_execsql_test -db db2 3.1a {
SELECT * FROM x1
} {}
do_test 3.1b {
list [catch { db2 eval {BEGIN EXCLUSIVE} } msg] $msg
} {1 {database is locked}}
# Set bExpect to true if calls to xSleep() are expected. Such calls are
# expected unless this is an SQLITE_ENABLE_SETLK_TIMEOUT=1 build.
set bExpect 1
if {$tcl_platform(platform) eq "windows" && $::sqlite_options(setlk_timeout)==1} {
set bExpect 0
}
do_test 3.2 {
expr {$::sleep_count > 0}
} $bExpect
set ::sleep_count 0
do_execsql_test 3.3 {
COMMIT;
}
# Launch a non-blocking testfixture process to write-lock the
# database for 2000 ms.
testfixture_nb done {
sqlite3 db test.db
db eval {
BEGIN EXCLUSIVE;
INSERT INTO x1 VALUES(3, 4);
}
after 2000
db eval {
COMMIT
}
}
after 500 {set ok 1}
vwait ok
db2 timeout 5000
do_test 3.4 {
set t [lindex [time { db2 eval { BEGIN EXCLUSIVE } }] 0]
expr ($t>1000000)
} {1}
# Set bExpect to true if calls to xSleep() are expected. Such calls are
# expected unless this is an SQLITE_ENABLE_SETLK_TIMEOUT=1 build.
set bExpect 1
if {$::sqlite_options(setlk_timeout)==1} {
set bExpect 0
}
do_test 3.5 {
expr {$::sleep_count > 0}
} $bExpect
do_execsql_test -db db2 3.6 {
INSERT INTO x1 VALUES(5, 6);
COMMIT;
SELECT * FROM x1;
} {1 2 3 4 5 6}
# Launch a non-blocking testfixture process to write-lock the
# database for 2000 ms.
testfixture_nb done {
sqlite3 db test.db
db eval {
BEGIN EXCLUSIVE;
INSERT INTO x1 VALUES(7, 8);
}
after 2000
db eval {
COMMIT
}
db close
}
after 500 {set ok 1}
vwait ok
db2 timeout 0x7FFFFFFF
do_test 3.7 {
set t [lindex [time { db2 eval { BEGIN EXCLUSIVE } }] 0]
expr ($t>1000000)
} {1}
# Set bExpect to true if calls to xSleep() are expected. Such calls are
# expected unless this is an SQLITE_ENABLE_SETLK_TIMEOUT=1 build.
set bExpect 1
if {$::sqlite_options(setlk_timeout)==1} {
set bExpect 0
}
do_test 3.8 {
expr {$::sleep_count > 0}
} $bExpect
do_execsql_test -db db2 3.9 {
INSERT INTO x1 VALUES(9, 10);
COMMIT;
SELECT * FROM x1;
} {1 2 3 4 5 6 7 8 9 10}
db2 close
tvfs delete
finish_test
|