interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / walsetlk.test

350 lines · 7212 bytes Raw Blame History
1# 2020 May 06
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
6# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
9#
10#***********************************************************************
11#
12# TESTRUNNER: slow
13#
14
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17source $testdir/lock_common.tcl
18set testprefix walsetlk
19
20ifcapable !wal {finish_test ; return }
21db timeout 1000
22
23#-------------------------------------------------------------------------
24# 1.*: Test that nothing goes wrong if recovery is forced while opening
25# a write transaction or performing a checkpoint with blocking locks.
26#
27
28do_execsql_test 1.0 {
29 CREATE TABLE t1(x, y);
30 PRAGMA journal_mode = wal;
31 INSERT INTO t1 VALUES(1, 2);
32 INSERT INTO t1 VALUES(3, 4);
33 INSERT INTO t1 VALUES(5, 6);
34 INSERT INTO t1 VALUES(7, 8);
35} {wal}
36
37sqlite3 db2 test.db
38db2 timeout 1000
39
40do_execsql_test -db db2 1.1 {
41 SELECT * FROM t1
42} {1 2 3 4 5 6 7 8}
43
44set fd [open test.db-shm r+]
45puts $fd "blahblahblahblah"
46flush $fd
47
48do_execsql_test 1.2 {
49 BEGIN;
50 INSERT INTO t1 VALUES(9, 10);
51}
52
53do_execsql_test -db db2 1.3 {
54 SELECT * FROM t1
55} {1 2 3 4 5 6 7 8}
56
57do_test 1.4 {
58 list [catch {db2 eval { BEGIN EXCLUSIVE }} msg] $msg
59} {1 {database is locked}}
60
61do_execsql_test 1.5 { COMMIT }
62do_execsql_test -db db2 1.6 {
63 SELECT * FROM t1
64} {1 2 3 4 5 6 7 8 9 10}
65
66puts $fd "blahblahblahblah"
67flush $fd
68
69do_execsql_test -db db2 1.7 {
70 PRAGMA wal_checkpoint = TRUNCATE
71} {0 0 0}
72
73do_test 1.8 {
74 file size test.db-wal
75} 0
76
77close $fd
78db close
79db2 close
80#-------------------------------------------------------------------------
81
82do_multiclient_test tn {
83
84 testvfs tvfs -fullshm 1
85 db close
86 sqlite3 db test.db -vfs tvfs
87 tvfs script xSleep_callback
88 tvfs filter xSleep
89
90 set ::sleep_count 0
91 proc xSleep_callback {xSleep nMs} {
92 after [expr $nMs / 1000]
93 incr ::sleep_count
94 }
95
96 do_test 2.$tn.1 {
97 sql1 {
98 PRAGMA journal_mode = wal;
99 CREATE TABLE t1(s, v);
100 INSERT INTO t1 VALUES(1, 2);
101 INSERT INTO t1 VALUES(3, 4);
102 INSERT INTO t1 VALUES(5, 6);
103 }
104 code1 { db timeout 1100 }
105 } {}
106
107 do_test 2.$tn.2 {
108 sql2 {
109 BEGIN;
110 INSERT INTO t1 VALUES(7, 8);
111 }
112 } {}
113
114 do_test 2.$tn.3 {
115 set us [lindex [time { catch {db eval "BEGIN EXCLUSIVE"} }] 0]
116 expr $us>1000000 && $us<4000000
117 } {1}
118
119 do_test 2.$tn.4 {
120 sql2 { COMMIT }
121 sql1 { SELECT * FROM t1 }
122 } {1 2 3 4 5 6 7 8}
123
124 do_test 2.$tn.5 {
125 sql2 {
126 BEGIN;
127 INSERT INTO t1 VALUES(9, 10);
128 }
129 } {}
130
131 do_test 2.$tn.6 {
132 set us [lindex [time { catch {db eval "PRAGMA wal_checkpoint=RESTART"} }] 0]
133 expr $us>1000000 && $us<4000000
134 } {1}
135
136 do_test 2.$tn.7 {
137 sql2 {
138 COMMIT;
139 BEGIN;
140 SELECT * FROM t1;
141 }
142 } {1 2 3 4 5 6 7 8 9 10}
143
144 do_test 2.$tn.8 {
145 set us [lindex [time { catch {db eval "PRAGMA wal_checkpoint=RESTART"} }] 0]
146 expr $us>1000000 && $us<4000000
147 } {1}
148 do_test 2.$tn.9 {
149 sql3 {
150 INSERT INTO t1 VALUES(11, 12);
151 }
152 sql2 {
153 COMMIT;
154 BEGIN;
155 SELECT * FROM t1;
156 }
157 sql3 {
158 INSERT INTO t1 VALUES(13, 14);
159 }
160 } {}
161
162 do_test 2.$tn.10 {
163 set us [lindex [time { catch {db eval "PRAGMA wal_checkpoint=RESTART"} }] 0]
164 expr $us>1000000 && $us<4000000
165 } {1}
166
167 do_test 2.$tn.11 {
168 sql3 {
169 BEGIN;
170 SELECT * FROM t1;
171 }
172 sql1 { INSERT INTO t1 VALUES(15, 16); }
173 } {}
174
175 do_test 2.$tn.12 {
176 set us [lindex [time { catch {db eval "PRAGMA wal_checkpoint=RESTART"} }] 0]
177 expr $us>1000000 && $us<4000000
178 } {1}
179
180 do_test 2.$tn.13 {
181 sql2 {
182 COMMIT;
183 BEGIN;
184 SELECT * FROM t1;
185 }
186 sql1 { INSERT INTO t1 VALUES(17, 18); }
187 } {}
188
189 do_test 2.$tn.14 {
190 set us [lindex [time { catch {db eval "PRAGMA wal_checkpoint=RESTART"} }] 0]
191 expr $us>1000000 && $us<4000000
192 } {1}
193
194 db close
195 tvfs delete
196
197 # Set bSleep to true if it is expected that the above used xSleep() to
198 # wait for locks. bSleep is true unless SQLITE_ENABLE_SETLK_TIMEOUT is
199 # set to 1 and either:
200 #
201 # * the OS is windows, or
202 # * the OS is unix and the tests were run with each connection
203 # in a separate process.
204 #
205 set bSleep 1
206 if {$::sqlite_options(setlk_timeout)==1} {
207 if {$::tcl_platform(platform) eq "windows"} {
208 set bSleep 0
209 }
210 if {$::tcl_platform(platform) eq "unix"} {
211 set bSleep [expr $tn==2]
212 }
213 }
214
215 do_test 2.$tn.15.$bSleep {
216 expr $::sleep_count > 0
217 } $bSleep
218}
219
220#-------------------------------------------------------------------------
221reset_db
222
223testvfs tvfs -fullshm 1
224tvfs script xSleep_callback
225tvfs filter xSleep
226
227set ::sleep_count 0
228proc xSleep_callback {xSleep nMs} {
229 after [expr $nMs / 1000]
230 incr ::sleep_count
231 breakpoint
232}
233
234sqlite3 db2 test.db -vfs tvfs
235db2 timeout 1000
236
237do_execsql_test 3.0 {
238 PRAGMA journal_mode = wal;
239 CREATE TABLE x1(x, y);
240 BEGIN;
241 INSERT INTO x1 VALUES(1, 2);
242} {wal}
243
244do_execsql_test -db db2 3.1a {
245 SELECT * FROM x1
246} {}
247
248do_test 3.1b {
249 list [catch { db2 eval {BEGIN EXCLUSIVE} } msg] $msg
250} {1 {database is locked}}
251
252# Set bExpect to true if calls to xSleep() are expected. Such calls are
253# expected unless this is an SQLITE_ENABLE_SETLK_TIMEOUT=1 build.
254set bExpect 1
255if {$tcl_platform(platform) eq "windows" && $::sqlite_options(setlk_timeout)==1} {
256 set bExpect 0
257}
258do_test 3.2 {
259 expr {$::sleep_count > 0}
260} $bExpect
261set ::sleep_count 0
262
263do_execsql_test 3.3 {
264 COMMIT;
265}
266
267# Launch a non-blocking testfixture process to write-lock the
268# database for 2000 ms.
269testfixture_nb done {
270 sqlite3 db test.db
271 db eval {
272 BEGIN EXCLUSIVE;
273 INSERT INTO x1 VALUES(3, 4);
274 }
275 after 2000
276 db eval {
277 COMMIT
278 }
279}
280
281after 500 {set ok 1}
282vwait ok
283
284db2 timeout 5000
285do_test 3.4 {
286 set t [lindex [time { db2 eval { BEGIN EXCLUSIVE } }] 0]
287 expr ($t>1000000)
288} {1}
289
290# Set bExpect to true if calls to xSleep() are expected. Such calls are
291# expected unless this is an SQLITE_ENABLE_SETLK_TIMEOUT=1 build.
292set bExpect 1
293if {$::sqlite_options(setlk_timeout)==1} {
294 set bExpect 0
295}
296do_test 3.5 {
297 expr {$::sleep_count > 0}
298} $bExpect
299
300do_execsql_test -db db2 3.6 {
301 INSERT INTO x1 VALUES(5, 6);
302 COMMIT;
303 SELECT * FROM x1;
304} {1 2 3 4 5 6}
305
306# Launch a non-blocking testfixture process to write-lock the
307# database for 2000 ms.
308testfixture_nb done {
309 sqlite3 db test.db
310 db eval {
311 BEGIN EXCLUSIVE;
312 INSERT INTO x1 VALUES(7, 8);
313 }
314 after 2000
315 db eval {
316 COMMIT
317 }
318 db close
319}
320
321after 500 {set ok 1}
322vwait ok
323
324db2 timeout 0x7FFFFFFF
325do_test 3.7 {
326 set t [lindex [time { db2 eval { BEGIN EXCLUSIVE } }] 0]
327 expr ($t>1000000)
328} {1}
329
330# Set bExpect to true if calls to xSleep() are expected. Such calls are
331# expected unless this is an SQLITE_ENABLE_SETLK_TIMEOUT=1 build.
332set bExpect 1
333if {$::sqlite_options(setlk_timeout)==1} {
334 set bExpect 0
335}
336do_test 3.8 {
337 expr {$::sleep_count > 0}
338} $bExpect
339
340do_execsql_test -db db2 3.9 {
341 INSERT INTO x1 VALUES(9, 10);
342 COMMIT;
343 SELECT * FROM x1;
344} {1 2 3 4 5 6 7 8 9 10}
345
346db2 close
347tvfs delete
348
349finish_test
350