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 / walsetlk2.test

269 lines · 5281 bytes Raw Blame History
1# 2025 Jan 24
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 walsetlk2
19
20ifcapable !wal {finish_test ; return }
21ifcapable !setlk_timeout {finish_test ; return }
22
23#-------------------------------------------------------------------------
24# Check that xShmLock calls are as expected for write transactions in
25# setlk mode.
26#
27reset_db
28
29do_execsql_test 1.0 {
30 PRAGMA journal_mode = wal;
31 CREATE TABLE t1(a, b, c);
32 INSERT INTO t1 VALUES(1, 2, 3);
33} {wal}
34db close
35
36testvfs tvfs
37tvfs script xShmLock_callback
38tvfs filter xShmLock
39
40set ::xshmlock [list]
41proc xShmLock_callback {method path name detail} {
42 lappend ::xshmlock $detail
43}
44
45sqlite3 db test.db -vfs tvfs
46db timeout 1000
47
48do_execsql_test 1.1 {
49 SELECT * FROM t1
50} {1 2 3}
51
52do_execsql_test 1.2 {
53 INSERT INTO t1 VALUES(4, 5, 6);
54}
55
56set ::xshmlock [list]
57do_execsql_test 1.3 {
58 INSERT INTO t1 VALUES(7, 8, 9);
59}
60
61do_test 1.4 {
62 set ::xshmlock
63} [list \
64 {0 1 lock exclusive} \
65 {4 1 lock exclusive} {4 1 unlock exclusive} \
66 {4 1 lock shared} \
67 {0 1 unlock exclusive} \
68 {4 1 unlock shared}
69]
70
71do_execsql_test 1.5.1 { SELECT * FROM t1 } {1 2 3 4 5 6 7 8 9}
72set ::xshmlock [list]
73do_execsql_test 1.5.2 {
74 INSERT INTO t1 VALUES(10, 11, 12);
75}
76do_test 1.5.3 {
77 set ::xshmlock
78} [list \
79 {0 1 lock exclusive} \
80 {4 1 lock shared} \
81 {0 1 unlock exclusive} \
82 {4 1 unlock shared}
83]
84
85db close
86tvfs delete
87
88#-------------------------------------------------------------------------
89# Check that if sqlite3_setlk_timeout() is used, blocking locks timeout
90# but other operations do not use the retry mechanism.
91#
92reset_db
93db close
94sqlite3 db test.db -fullmutex 1
95
96do_execsql_test 2.0 {
97 CREATE TABLE t1(a, b);
98 INSERT INTO t1 VALUES(1, 2), (3, 4);
99}
100
101sqlite3_setlk_timeout db 2000
102
103# Launch a non-blocking testfixture process to write-lock the
104# database for 2000 ms.
105testfixture_nb done {
106 sqlite3 db test.db
107 db eval {
108 BEGIN EXCLUSIVE;
109 INSERT INTO t1 VALUES(5, 6);
110 }
111 after 2000
112 db eval {
113 COMMIT
114 }
115 db close
116}
117
118after 500 {set ok 1}
119vwait ok
120
121do_catchsql_test 2.1 {
122 INSERT INTO t1 VALUES(7, 8);
123} {1 {database is locked}}
124
125sqlite3_busy_timeout db 2000
126
127do_catchsql_test 2.2 {
128 INSERT INTO t1 VALUES(7, 8);
129} {0 {}}
130
131do_execsql_test 2.3 {
132 SELECT * FROM t1
133} {1 2 3 4 5 6 7 8}
134
135do_execsql_test 2.4 {
136 PRAGMA journal_mode = wal;
137} {wal}
138
139db close
140sqlite3 db test.db
141
142do_execsql_test 2.5 {
143 INSERT INTO t1 VALUES(9, 10);
144}
145
146if {$::sqlite_options(setlk_timeout)==1} {
147
148sqlite3_setlk_timeout db 2000
149
150# Launch a non-blocking testfixture process to write-lock the
151# database for 2000 ms.
152testfixture_nb done {
153 sqlite3 db test.db
154 db eval {
155 BEGIN EXCLUSIVE;
156 INSERT INTO t1 VALUES(11, 12);
157 }
158 after 2000
159 db eval {
160 COMMIT
161 }
162 db close
163}
164
165after 500 {set ok 1}
166vwait ok
167
168do_catchsql_test 2.6 {
169 INSERT INTO t1 VALUES(13, 14);
170} {0 {}}
171
172do_execsql_test 2.7 {
173 SELECT * FROM t1
174} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
175
176}
177
178
179#-------------------------------------------------------------------------
180# Check that if sqlite3_setlk_timeout(-1) is called, blocking locks are
181# enabled and last for a few seconds at least. Difficult to test that they
182# really do block indefinitely.
183#
184reset_db
185
186if {$::sqlite_options(setlk_timeout)==1} {
187do_execsql_test 3.0 {
188 PRAGMA journal_mode = wal;
189 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
190 INSERT INTO t1 VALUES(1, 'one'), (3, 'three');
191} {wal}
192
193sqlite3_setlk_timeout db -1
194
195# Launch a non-blocking testfixture process to write-lock the
196# database for 2000 ms.
197testfixture_nb done {
198 sqlite3 db test.db
199 db eval {
200 BEGIN EXCLUSIVE;
201 INSERT INTO t1 VALUES(5, 'five');
202 }
203 after 2000
204 db eval {
205 COMMIT
206 }
207 db close
208}
209
210after 500 {set ok 1}
211vwait ok
212
213breakpoint
214do_catchsql_test 3.1 {
215 INSERT INTO t1 VALUES(7, 'seven');
216} {0 {}}
217
218# Launch another non-blocking testfixture process to write-lock the
219# database for 2000 ms.
220testfixture_nb done {
221 sqlite3 db test.db
222 db eval {
223 BEGIN EXCLUSIVE;
224 INSERT INTO t1 VALUES(9, 'nine');
225 }
226 after 2000
227 db eval {
228 COMMIT
229 }
230 db close
231}
232
233after 500 {set ok 1}
234vwait ok
235
236do_catchsql_test 3.2 {
237 INSERT INTO t1 VALUES(9, 'ten');
238} {1 {UNIQUE constraint failed: t1.a}}
239
240do_execsql_test 3.3 {
241 SELECT * FROM t1
242} {1 one 3 three 5 five 7 seven 9 nine}
243
244db close
245
246# Launch another non-blocking testfixture process to write-lock the
247# database for 2000 ms.
248testfixture_nb done {
249 sqlite3 db test.db
250 db eval {
251 BEGIN EXCLUSIVE;
252 INSERT INTO t1 VALUES(11, 'eleven');
253 }
254 after 2000
255 db eval {
256 COMMIT
257 }
258}
259
260sqlite3 db test.db
261sqlite3_setlk_timeout db -1
262do_catchsql_test 3.4 {
263 INSERT INTO t1 VALUES(13, 'thirteen');
264} {0 {}}
265
266}
267
268finish_test
269