3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The focus
12 # of these tests is exclusive access mode (i.e. the thing activated by
13 # "PRAGMA locking_mode = EXCLUSIVE").
15 # $Id: exclusive.test,v 1.15 2009/06/26 12:30:40 danielk1977 Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 ifcapable {!pager_pragmas} {
25 forcedelete test2.db-journal
27 forcedelete test3.db-journal
29 forcedelete test4.db-journal
32 #----------------------------------------------------------------------
33 # Test cases exclusive-1.X test the PRAGMA logic.
35 do_test exclusive-1.0 {
38 pragma main.locking_mode;
39 pragma temp.locking_mode;
41 } [list normal normal exclusive]
42 do_test exclusive-1.1 {
44 pragma locking_mode = exclusive;
47 do_test exclusive-1.2 {
50 pragma main.locking_mode;
51 pragma temp.locking_mode;
53 } [list exclusive exclusive exclusive]
54 do_test exclusive-1.3 {
56 pragma locking_mode = normal;
59 do_test exclusive-1.4 {
62 pragma main.locking_mode;
63 pragma temp.locking_mode;
65 } [list normal normal exclusive]
66 do_test exclusive-1.5 {
68 pragma locking_mode = invalid;
71 do_test exclusive-1.6 {
74 pragma main.locking_mode;
75 pragma temp.locking_mode;
77 } [list normal normal exclusive]
79 do_test exclusive-1.7 {
81 pragma locking_mode = exclusive;
82 ATTACH 'test2.db' as aux;
85 pragma main.locking_mode;
86 pragma aux.locking_mode;
88 } {exclusive exclusive}
89 do_test exclusive-1.8 {
91 pragma main.locking_mode = normal;
94 pragma main.locking_mode;
95 pragma temp.locking_mode;
96 pragma aux.locking_mode;
98 } [list normal exclusive exclusive]
99 do_test exclusive-1.9 {
104 do_test exclusive-1.10 {
106 ATTACH 'test3.db' as aux2;
109 pragma main.locking_mode;
110 pragma aux.locking_mode;
111 pragma aux2.locking_mode;
113 } {normal exclusive exclusive}
114 do_test exclusive-1.11 {
116 pragma aux.locking_mode = normal;
119 pragma main.locking_mode;
120 pragma aux.locking_mode;
121 pragma aux2.locking_mode;
123 } {normal normal exclusive}
124 do_test exclusive-1.12 {
126 pragma locking_mode = normal;
129 pragma main.locking_mode;
130 pragma temp.locking_mode;
131 pragma aux.locking_mode;
132 pragma aux2.locking_mode;
134 } [list normal exclusive normal normal]
135 do_test exclusive-1.13 {
137 ATTACH 'test4.db' as aux3;
140 pragma main.locking_mode;
141 pragma temp.locking_mode;
142 pragma aux.locking_mode;
143 pragma aux2.locking_mode;
144 pragma aux3.locking_mode;
146 } [list normal exclusive normal normal normal]
148 do_test exclusive-1.99 {
157 #----------------------------------------------------------------------
158 # Test cases exclusive-2.X verify that connections in exclusive
159 # locking_mode do not relinquish locks.
161 do_test exclusive-2.0 {
163 CREATE TABLE abc(a, b, c);
164 INSERT INTO abc VALUES(1, 2, 3);
165 PRAGMA locking_mode = exclusive;
168 do_test exclusive-2.1 {
171 INSERT INTO abc VALUES(4, 5, 6);
175 do_test exclusive-2.2 {
176 # This causes connection 'db' (in exclusive mode) to establish
177 # a shared-lock on the db. The other connection should now be
178 # locked out as a writer.
183 do_test exclusive-2.4 {
188 do_test exclusive-2.5 {
190 INSERT INTO abc VALUES(7, 8, 9);
192 } {1 {database is locked}}
193 sqlite3_soft_heap_limit 0
194 do_test exclusive-2.6 {
195 # Because connection 'db' only has a shared-lock, the other connection
196 # will be able to get a RESERVED, but will fail to upgrade to EXCLUSIVE.
199 INSERT INTO abc VALUES(7, 8, 9);
204 } {1 {database is locked}}
205 do_test exclusive-2.7 {
209 } {1 {database is locked}}
210 do_test exclusive-2.8 {
215 sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
217 do_test exclusive-2.9 {
218 # Write the database to establish the exclusive lock with connection 'db.
220 INSERT INTO abc VALUES(7, 8, 9);
225 } {1 {database is locked}}
226 do_test exclusive-2.10 {
227 # Changing the locking-mode does not release any locks.
229 PRAGMA locking_mode = normal;
234 } {1 {database is locked}}
235 do_test exclusive-2.11 {
236 # After changing the locking mode, accessing the db releases locks.
243 } {1 2 3 4 5 6 7 8 9}
246 #----------------------------------------------------------------------
247 # Tests exclusive-3.X - test that a connection in exclusive mode
248 # truncates instead of deletes the journal file when committing
251 # These tests are not run on windows because the windows backend
252 # opens the journal file for exclusive access, preventing its contents
253 # from being inspected externally.
255 if {$tcl_platform(platform) != "windows"} {
257 # Return a list of two booleans (either 0 or 1). The first is true
258 # if the named file exists. The second is true only if the file
259 # exists and the first 28 bytes contain at least one non-zero byte.
261 proc filestate {fname} {
264 if {[file exists $fname]} {
266 set hdr [hexio_read $fname 0 28]
267 set content [expr {0==[string match $hdr [string repeat 0 56]]}]
269 list $exists $content
272 do_test exclusive-3.0 {
273 filestate test.db-journal
275 do_test exclusive-3.1 {
277 PRAGMA locking_mode = exclusive;
281 filestate test.db-journal
283 do_test exclusive-3.2 {
287 filestate test.db-journal
289 do_test exclusive-3.3 {
291 INSERT INTO abc VALUES('A', 'B', 'C');
295 do_test exclusive-3.4 {
298 UPDATE abc SET a = 1, b = 2, c = 3;
303 do_test exclusive-3.5 {
304 filestate test.db-journal
306 do_test exclusive-3.6 {
308 PRAGMA locking_mode = normal;
311 filestate test.db-journal
315 #----------------------------------------------------------------------
316 # Tests exclusive-4.X - test that rollback works correctly when
317 # in exclusive-access mode.
320 # The following procedure computes a "signature" for table "t3". If
321 # T3 changes in any way, the signature should change.
323 # This is used to test ROLLBACK. We gather a signature for t3, then
324 # make lots of changes to t3, then rollback and take another signature.
325 # The two signatures should be the same.
328 return [db eval {SELECT count(*), md5sum(x) FROM t3}]
331 do_test exclusive-4.0 {
332 execsql { PRAGMA locking_mode = exclusive; }
333 execsql { PRAGMA default_cache_size = 10; }
336 CREATE TABLE t3(x TEXT);
337 INSERT INTO t3 VALUES(randstr(10,400));
338 INSERT INTO t3 VALUES(randstr(10,400));
339 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
340 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
341 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
342 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
345 execsql {SELECT count(*) FROM t3;}
349 do_test exclusive-4.1 {
352 DELETE FROM t3 WHERE random()%10!=0;
353 INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
354 INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
355 SELECT count(*) FROM t3;
361 do_test exclusive-4.2 {
364 DELETE FROM t3 WHERE random()%10!=0;
365 INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
366 DELETE FROM t3 WHERE random()%10!=0;
367 INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
373 do_test exclusive-4.3 {
375 INSERT INTO t3 SELECT randstr(10,400) FROM t3 WHERE random()%10==0;
379 do_test exclusive-4.4 {
380 catch {set ::X [signature]}
382 do_test exclusive-4.5 {
384 PRAGMA locking_mode = NORMAL;
390 #----------------------------------------------------------------------
391 # Tests exclusive-5.X - test that statement journals are truncated
392 # instead of deleted when in exclusive access mode.
395 # Close and reopen the database so that the temp database is no
401 # if we're using proxy locks, we use 3 filedescriptors for a db
402 # that is open but NOT writing changes, normally
403 # sqlite uses 1 (proxy locking adds the conch and the local lock)
405 foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
406 set using_proxy $value
409 if {$using_proxy!=0} {
413 do_test exclusive-5.0 {
415 CREATE TABLE abc(a UNIQUE, b UNIQUE, c UNIQUE);
417 INSERT INTO abc VALUES(1, 2, 3);
418 INSERT INTO abc SELECT a+1, b+1, c+1 FROM abc;
421 do_test exclusive-5.1 {
422 # Three files are open: The db, journal and statement-journal.
423 set sqlite_open_file_count
424 expr $sqlite_open_file_count-$extrafds
425 } [expr 3 - ($TEMP_STORE>=2)]
426 do_test exclusive-5.2 {
430 # One file open: the db.
431 set sqlite_open_file_count
432 expr $sqlite_open_file_count-$extrafds
434 do_test exclusive-5.3 {
436 PRAGMA locking_mode = exclusive;
438 INSERT INTO abc VALUES(5, 6, 7);
440 # Two files open: the db and journal.
441 set sqlite_open_file_count
442 expr $sqlite_open_file_count-$extrafds
444 do_test exclusive-5.4 {
446 INSERT INTO abc SELECT a+10, b+10, c+10 FROM abc;
448 # Three files are open: The db, journal and statement-journal.
449 set sqlite_open_file_count
450 expr $sqlite_open_file_count-$extrafds
451 } [expr 3 - ($TEMP_STORE>=2)]
452 do_test exclusive-5.5 {
456 # Three files are still open: The db, journal and statement-journal.
457 set sqlite_open_file_count
458 expr $sqlite_open_file_count-$extrafds
459 } [expr 3 - ($TEMP_STORE>=2)]
460 do_test exclusive-5.6 {
462 PRAGMA locking_mode = normal;
465 } {normal 1 2 3 2 3 4 5 6 7 11 12 13 12 13 14 15 16 17}
466 do_test exclusive-5.7 {
468 set sqlite_open_file_count
469 expr $sqlite_open_file_count-$extrafds
472 #-------------------------------------------------------------------------
474 do_execsql_test exclusive-6.1 {
475 CREATE TABLE t4(a, b);
476 INSERT INTO t4 VALUES('Eden', 1955);
478 INSERT INTO t4 VALUES('Macmillan', 1957);
479 INSERT INTO t4 VALUES('Douglas-Home', 1963);
480 INSERT INTO t4 VALUES('Wilson', 1964);
482 do_test exclusive-6.2 {
483 forcedelete test2.db test2.db-journal
484 copy_file test.db test2.db
485 copy_file test.db-journal test2.db-journal
489 do_execsql_test exclusive-6.3 {
490 PRAGMA locking_mode = EXCLUSIVE;
492 } {exclusive Eden 1955}
494 do_test exclusive-6.4 {
496 forcedelete test.db test.db-journal
497 set fd [open test.db-journal w]
503 do_execsql_test exclusive-6.5 {
504 PRAGMA locking_mode = EXCLUSIVE;
505 SELECT * FROM sqlite_master;