AryaWu/sqlite
0
1# 2008 June 282#3# The author disclaims copyright to this source code. In place of4# 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# This file implements regression tests for SQLite library. The12# focus of this script is database locks.13#14 15set testdir [file dirname $argv0]16source $testdir/tester.tcl17set testprefix lock518 19# This file is only run if using the unix backend compiled with the20# SQLITE_ENABLE_LOCKING_STYLE macro.21db close22if {[catch {sqlite3 db test.db -vfs unix-none} msg]} {23 finish_test24 return25}26db close27forcedelete test.db.lock28 29ifcapable lock_proxy_pragmas {30 set ::using_proxy 031 foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {32 set ::using_proxy $value33 }34 # Disable the proxy locking for these tests35 set env(SQLITE_FORCE_PROXY_LOCKING) "0"36}37 38 39do_test lock5-dotfile.1 {40 sqlite3 db test.db -vfs unix-dotfile41 execsql {42 BEGIN;43 CREATE TABLE t1(a, b);44 }45} {}46 47do_test lock5-dotfile.2 {48 file exists test.db.lock49} {1}50 51do_test lock5-dotfile.3 {52 execsql COMMIT53 file exists test.db.lock54} {0}55 56do_test lock5-dotfile.4 {57 sqlite3 db2 test.db -vfs unix-dotfile58 execsql {59 INSERT INTO t1 VALUES('a', 'b');60 SELECT * FROM t1;61 } db262} {a b}63 64do_test lock5-dotfile.5 {65 execsql {66 BEGIN;67 SELECT * FROM t1;68 } db269} {a b}70 71do_test lock5-dotfile.6 {72 file exists test.db.lock73} {1}74 75do_test lock5-dotfile.7 {76 catchsql { SELECT * FROM t1; }77} {1 {database is locked}}78 79do_test lock5-dotfile.8 {80 execsql {81 SELECT * FROM t1;82 ROLLBACK;83 } db284} {a b}85 86do_test lock5-dotfile.9 {87 catchsql { SELECT * FROM t1; }88} {0 {a b}}89 90do_test lock5-dotfile.10 {91 file exists test.db.lock92} {0}93 94do_test lock5-dotfile.X {95 db2 close96 execsql {BEGIN EXCLUSIVE}97 db close98 file exists test.db.lock99} {0}100 101#####################################################################102 103forcedelete test.db104if {0==[catch {sqlite3 db test.db -vfs unix-flock} msg]} {105 106do_test lock5-flock.1 {107 sqlite3 db test.db -vfs unix-flock108 execsql {109 CREATE TABLE t1(a, b);110 BEGIN;111 INSERT INTO t1 VALUES(1, 2);112 }113} {}114 115# Make sure we are not accidentally using the dotfile locking scheme.116do_test lock5-flock.2 {117 file exists test.db.lock118} {0}119 120do_test lock5-flock.3 {121 catch { sqlite3 db2 test.db -vfs unix-flock }122 catchsql { SELECT * FROM t1 } db2123} {1 {database is locked}}124 125do_test lock5-flock.4 {126 execsql COMMIT127 catchsql { SELECT * FROM t1 } db2128} {0 {1 2}}129 130do_test lock5-flock.5 {131 execsql BEGIN132 catchsql { SELECT * FROM t1 } db2133} {0 {1 2}}134 135do_test lock5-flock.6 {136 execsql {SELECT * FROM t1}137 catchsql { SELECT * FROM t1 } db2138} {1 {database is locked}}139 140do_test lock5-flock.7 {141 db close142 catchsql { SELECT * FROM t1 } db2143} {0 {1 2}}144 145do_test lock5-flock.8 {146 db2 close147} {}148 149do_test lock5-flock.9 {150 sqlite3 db test.db -vfs unix-flock151 execsql {152 SELECT * FROM t1153 }154} {1 2}155 156do_test lock5-flock.10 {157 sqlite3 db2 test.db -vfs unix-flock158 execsql {159 SELECT * FROM t1160 } db2161} {1 2}162 163do_test lock5-flock.10 {164 execsql {165 PRAGMA cache_size = 1;166 BEGIN;167 WITH s(i) AS (168 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<10000169 )170 INSERT INTO t1 SELECT i, i+1 FROM s;171 }172 173 catchsql {174 SELECT * FROM t1175 } db2176} {1 {database is locked}}177 178if {[permutation]!="inmemory_journal"} {179 do_test lock5-flock.11 {180 forcecopy test.db test.db2181 forcecopy test.db-journal test.db2-journal182 db2 close183 sqlite3 db2 test.db2 -vfs unix-flock184 catchsql {185 SELECT * FROM t1186 } db2187 } {0 {1 2}}188 189 do_test lock5-flock.12 {190 file exists test.db2-journal191 } 0192}193 194db close195db2 close196 197}198 199#####################################################################200 201reset_db202 203do_test lock5-none.1 {204 sqlite3 db test.db -vfs unix-none205 sqlite3 db2 test.db -vfs unix-none206 execsql { PRAGMA mmap_size = 0 } db2207 execsql {208 CREATE TABLE t1(a, b);209 INSERT INTO t1 VALUES(1, 2);210 BEGIN;211 INSERT INTO t1 VALUES(3, 4);212 }213} {}214do_test lock5-none.2 {215 execsql { SELECT * FROM t1 }216} {1 2 3 4}217do_test lock5-none.3 {218 execsql { SELECT * FROM t1; } db2219} {1 2}220do_test lock5-none.4 {221 execsql { 222 BEGIN;223 SELECT * FROM t1;224 } db2225} {1 2}226do_test lock5-none.5 {227 execsql COMMIT228 execsql {SELECT * FROM t1} db2229} {1 2}230 231ifcapable memorymanage {232 if {[permutation]!="memsubsys1" && [permutation]!="memsubsys2"} { 233 do_test lock5-none.6 {234 sqlite3_release_memory 1000000235 execsql {SELECT * FROM t1} db2236 } {1 2 3 4}237 }238}239 240do_test lock5-none.X {241 db close242 db2 close243} {}244 245ifcapable lock_proxy_pragmas {246 set env(SQLITE_FORCE_PROXY_LOCKING) $::using_proxy247}248 249#####################################################################250reset_db251if {[permutation]!="inmemory_journal"} {252 253 # 1. Create a large database using the unix-dotfile VFS254 # 2. Write a large transaction to the db, so that the cache spills, but do255 # not commit it.256 # 3. Make a copy of the database files on disk.257 # 4. Try to read from the copy using unix-dotfile VFS. This fails because258 # the dotfile still exists, so SQLite thinks the database is locked.259 # 5. Remove the dotfile.260 # 6. Try to read the db again. This time, the old transaction is rolled261 # back and the read permitted.262 #263 do_test 2.dotfile.1 {264 sqlite3 db test.db -vfs unix-dotfile265 execsql {266 PRAGMA cache_size = 10;267 CREATE TABLE t1(x, y, z);268 CREATE INDEX t1x ON t1(x);269 WITH s(i) AS (270 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000271 )272 INSERT INTO t1 SELECT hex(randomblob(20)), hex(randomblob(500)), i FROM s;273 }274 } {}275 276 do_execsql_test 2.dotfile.2 {277 BEGIN;278 UPDATE t1 SET z=z+1, x=hex(randomblob(20));279 }280 281 do_test 2.dotfile.3 {282 list \283 [file exists test.db] \284 [file exists test.db-journal] \285 [file exists test.db.lock]286 } {1 1 1}287 288 do_test 2.dotfile.4 {289 forcecopy test.db test.db2290 forcecopy test.db-journal test.db2-journal291 file mkdir test.db2.lock292 293 sqlite3 db2 test.db2 -vfs unix-dotfile294 catchsql {295 SELECT count(*) FROM t1;296 } db2297 } {1 {database is locked}}298 299 do_test 2.dotfile.5 {300 file delete test.db2.lock301 execsql {302 PRAGMA integrity_check303 } db2304 } {ok}305 306 db2 close307 308 do_test 2.dotfile.6 {309 forcecopy test.db test.db2310 forcecopy test.db-journal test.db2-journal311 312 sqlite3 db2 file:test.db2?nolock=1 -vfs unix-dotfile -uri 1313 catchsql {314 SELECT count(*) FROM t1;315 } db2316 } {0 1000}317}318 319finish_test320 