Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/test/walsetlk2.test # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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 15 set testdir [file dirname $argv0] 16 source $testdir/tester.tcl 17 source $testdir/lock_common.tcl 18 set testprefix walsetlk2 19 20 ifcapable !wal {finish_test ; return } 21 ifcapable !setlk_timeout {finish_test ; return } 22 23 #------------------------------------------------------------------------- 24 # Check that xShmLock calls are as expected for write transactions in 25 # setlk mode. 26 # 27 reset_db 28 29 do_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} 34 db close 35 36 testvfs tvfs 37 tvfs script xShmLock_callback 38 tvfs filter xShmLock 39 40 set ::xshmlock [list] 41 proc xShmLock_callback {method path name detail} { 42 lappend ::xshmlock $detail 43 } 44 45 sqlite3 db test.db -vfs tvfs 46 db timeout 1000 47 48 do_execsql_test 1.1 { 49 SELECT * FROM t1 50 } {1 2 3} 51 52 do_execsql_test 1.2 { 53 INSERT INTO t1 VALUES(4, 5, 6); 54 } 55 56 set ::xshmlock [list] 57 do_execsql_test 1.3 { 58 INSERT INTO t1 VALUES(7, 8, 9); 59 } 60 61 do_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 71 do_execsql_test 1.5.1 { SELECT * FROM t1 } {1 2 3 4 5 6 7 8 9} 72 set ::xshmlock [list] 73 do_execsql_test 1.5.2 { 74 INSERT INTO t1 VALUES(10, 11, 12); 75 } 76 do_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 85 db close 86 tvfs 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 # 92 reset_db 93 db close 94 sqlite3 db test.db -fullmutex 1 95 96 do_execsql_test 2.0 { 97 CREATE TABLE t1(a, b); 98 INSERT INTO t1 VALUES(1, 2), (3, 4); 99 } 100 101 sqlite3_setlk_timeout db 2000 102 103 # Launch a non-blocking testfixture process to write-lock the 104 # database for 2000 ms. 105 testfixture_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 118 after 500 {set ok 1} 119 vwait ok 120 121 do_catchsql_test 2.1 { 122 INSERT INTO t1 VALUES(7, 8); 123 } {1 {database is locked}} 124 125 sqlite3_busy_timeout db 2000 126 127 do_catchsql_test 2.2 { 128 INSERT INTO t1 VALUES(7, 8); 129 } {0 {}} 130 131 do_execsql_test 2.3 { 132 SELECT * FROM t1 133 } {1 2 3 4 5 6 7 8} 134 135 do_execsql_test 2.4 { 136 PRAGMA journal_mode = wal; 137 } {wal} 138 139 db close 140 sqlite3 db test.db 141 142 do_execsql_test 2.5 { 143 INSERT INTO t1 VALUES(9, 10); 144 } 145 146 if {$::sqlite_options(setlk_timeout)==1} { 147 148 sqlite3_setlk_timeout db 2000 149 150 # Launch a non-blocking testfixture process to write-lock the 151 # database for 2000 ms. 152 testfixture_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 165 after 500 {set ok 1} 166 vwait ok 167 168 do_catchsql_test 2.6 { 169 INSERT INTO t1 VALUES(13, 14); 170 } {0 {}} 171 172 do_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 # 184 reset_db 185 186 if {$::sqlite_options(setlk_timeout)==1} { 187 do_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 193 sqlite3_setlk_timeout db -1 194 195 # Launch a non-blocking testfixture process to write-lock the 196 # database for 2000 ms. 197 testfixture_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 210 after 500 {set ok 1} 211 vwait ok 212 213 breakpoint 214 do_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. 220 testfixture_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 233 after 500 {set ok 1} 234 vwait ok 235 236 do_catchsql_test 3.2 { 237 INSERT INTO t1 VALUES(9, 'ten'); 238 } {1 {UNIQUE constraint failed: t1.a}} 239 240 do_execsql_test 3.3 { 241 SELECT * FROM t1 242 } {1 one 3 three 5 five 7 seven 9 nine} 243 244 db close 245 246 # Launch another non-blocking testfixture process to write-lock the 247 # database for 2000 ms. 248 testfixture_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 260 sqlite3 db test.db 261 sqlite3_setlk_timeout db -1 262 do_catchsql_test 3.4 { 263 INSERT INTO t1 VALUES(13, 'thirteen'); 264 } {0 {}} 265 266 } 267 268 finish_test 269