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.
13 # $Id: exclusive2.test,v 1.10 2008/11/27 02:22:11 drh Exp $
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
18 # Do not use a codec for tests in this file, as the database file is
19 # manipulated directly using tcl scripts (using the [hexio_write] command).
23 ifcapable {!pager_pragmas} {
28 # Tests in this file verify that locking_mode=exclusive causes SQLite to
29 # use cached pages even if the database is changed on disk. This doesn't
31 if {[permutation]=="mmap"} {
36 # This module does not work right if the cache spills at unexpected
37 # moments. So disable the soft-heap-limit.
39 sqlite3_soft_heap_limit 0
41 proc pagerChangeCounter {filename new {fd ""}} {
43 set fd [open $filename RDWR]
44 fconfigure $fd -translation binary -encoding binary
51 set a [expr {($new&0xFF000000)>>24}]
52 set b [expr {($new&0x00FF0000)>>16}]
53 set c [expr {($new&0x0000FF00)>>8}]
54 set d [expr {($new&0x000000FF)}]
55 puts -nonewline $fd [binary format cccc $a $b $c $d]
60 foreach {a b c d} [list 0 0 0 0] {}
61 binary scan [read $fd 4] cccc a b c d
62 set ret [expr ($a&0x000000FF)<<24]
63 incr ret [expr ($b&0x000000FF)<<16]
64 incr ret [expr ($c&0x000000FF)<<8]
65 incr ret [expr ($d&0x000000FF)<<0]
67 if {$needClose} {close $fd}
71 proc readPagerChangeCounter {filename} {
72 set fd [open $filename RDONLY]
73 fconfigure $fd -translation binary -encoding binary
76 foreach {a b c d} [list 0 0 0 0] {}
77 binary scan [read $fd 4] cccc a b c d
78 set ret [expr ($a&0x000000FF)<<24]
79 incr ret [expr ($b&0x000000FF)<<16]
80 incr ret [expr ($c&0x000000FF)<<8]
81 incr ret [expr ($d&0x000000FF)<<0]
88 proc t1sig {{db db}} {
89 execsql {SELECT count(*), md5sum(a) FROM t1} $db
91 do_test exclusive2-1.0 {
92 readPagerChangeCounter test.db
95 #-----------------------------------------------------------------------
96 # The following tests - exclusive2-1.X - check that:
98 # 1-3: Build a database with connection 1, calculate a signature.
99 # 4-7: Modify the database using a second connection in a way that
100 # does not modify the freelist, then reset the pager change-counter
101 # to the value it had before the modifications.
102 # 8: Check that using the first connection, the database signature
103 # is still the same. This is because it uses the in-memory cache.
104 # It can't tell the db has changed because we reset the change-counter.
105 # 9: Increment the change-counter.
106 # 10: Ensure that the first connection now sees the updated database. It
107 # sees the change-counter has been incremented and discards the
108 # invalid in-memory cache.
110 # This will only work if the database cache is large enough to hold
111 # the entire database. In the case of 1024 byte pages, this means
112 # the cache size must be at least 17. Otherwise, some pages will be
113 # loaded from the database file in step 8.
115 # For similar reasons, this test does not work with the memsubsys1 permutation.
116 # Permutation memsubsys1 configures the pcache subsystem to use a static
117 # allocation of 24 pages (shared between all pagers). This is not enough for
120 do_test exclusive2-1.1 {
123 CREATE TABLE t1(a, b);
124 INSERT INTO t1(a) VALUES(randstr(10, 400));
125 INSERT INTO t1(a) VALUES(randstr(10, 400));
126 INSERT INTO t1(a) SELECT randstr(10, 400) FROM t1;
127 INSERT INTO t1(a) SELECT randstr(10, 400) FROM t1;
128 INSERT INTO t1(a) SELECT randstr(10, 400) FROM t1;
129 INSERT INTO t1(a) SELECT randstr(10, 400) FROM t1;
130 INSERT INTO t1(a) SELECT randstr(10, 400) FROM t1;
132 SELECT count(*) FROM t1;
135 do_test exclusive2-1.2.1 {
136 # Make sure the pager cache is large enough to store the
138 set nPage [expr [file size test.db]/1024]
139 if {$::SQLITE_DEFAULT_CACHE_SIZE < $nPage} {
140 execsql "PRAGMA cache_size = $nPage"
142 expr {[execsql {PRAGMA cache_size}] >= $nPage}
144 do_test exclusive2-1.2 {
146 readPagerChangeCounter test.db
148 do_test exclusive2-1.3 {
151 do_test exclusive2-1.4 {
155 do_test exclusive2-1.5 {
157 UPDATE t1 SET b=a, a=NULL;
159 expr {[t1sig db2] eq $::sig}
161 do_test exclusive2-1.6 {
162 readPagerChangeCounter test.db
164 do_test exclusive2-1.7 {
165 pagerChangeCounter test.db 1
167 if {[permutation] != "memsubsys1"} {
168 do_test exclusive2-1.9 {
170 expr {[t1sig] eq $::sig}
173 do_test exclusive2-1.10 {
174 pagerChangeCounter test.db 2
176 do_test exclusive2-1.11 {
177 expr {[t1sig] eq $::sig}
181 #--------------------------------------------------------------------
182 # These tests - exclusive2-2.X - are similar to exclusive2-1.X,
183 # except that they are run with locking_mode=EXCLUSIVE.
185 # 1-3: Build a database with exclusive-access connection 1,
186 # calculate a signature.
187 # 4: Corrupt the database by writing 10000 bytes of garbage
188 # starting at the beginning of page 2. Check that connection 1
189 # still works. It should be accessing the in-memory cache.
190 # 5-6: Modify the dataase change-counter. Connection 1 still works
191 # entirely from in-memory cache, because it doesn't check the
193 # 7-8 Set the locking-mode back to normal. After the db is unlocked,
194 # SQLite detects the modified change-counter and discards the
195 # in-memory cache. Then it finds the corruption caused in step 4....
197 # As above, this test is only applicable if the pager cache is
198 # large enough to hold the entire database. With 1024 byte pages,
199 # this means 19 pages. We also need to disable the soft-heap-limit
200 # to prevent memory-induced cache spills.
202 do_test exclusive2-2.1 {
203 execsql {PRAGMA cache_size=1000;}
204 execsql {PRAGMA locking_mode = exclusive;}
208 INSERT INTO t1(a) VALUES(randstr(10, 400));
209 INSERT INTO t1(a) VALUES(randstr(10, 400));
210 INSERT INTO t1(a) SELECT randstr(10, 400) FROM t1;
211 INSERT INTO t1(a) SELECT randstr(10, 400) FROM t1;
212 INSERT INTO t1(a) SELECT randstr(10, 400) FROM t1;
213 INSERT INTO t1(a) SELECT randstr(10, 400) FROM t1;
214 INSERT INTO t1(a) SELECT randstr(10, 400) FROM t1;
216 SELECT count(*) FROM t1;
219 do_test exclusive2-2.2.1 {
220 # Make sure the pager cache is large enough to store the
222 set nPage [expr [file size test.db]/1024]
223 if {$::SQLITE_DEFAULT_CACHE_SIZE < $nPage} {
224 execsql "PRAGMA cache_size = $nPage"
226 expr {[execsql {PRAGMA cache_size}] >= $nPage}
228 do_test exclusive2-2.2 {
230 readPagerChangeCounter test.db
232 do_test exclusive2-2.3 {
236 do_test exclusive2-2.4 {
237 set ::fd [open test.db RDWR]
238 fconfigure $::fd -translation binary
240 puts -nonewline $::fd [string repeat [binary format c 0] 10000]
245 do_test exclusive2-2.5 {
246 pagerChangeCounter test.db 5 $::fd
248 do_test exclusive2-2.6 {
251 do_test exclusive2-2.7 {
252 execsql {PRAGMA locking_mode = normal}
256 do_test exclusive2-2.8 {
257 set rc [catch {t1sig} msg]
259 } {1 {database disk image is malformed}}
261 #--------------------------------------------------------------------
262 # These tests - exclusive2-3.X - verify that the pager change-counter
263 # is only incremented by the first change when in exclusive access
264 # mode. In normal mode, the change-counter is incremented once
265 # per write-transaction.
271 forcedelete test.db-journal
273 do_test exclusive2-3.0 {
277 CREATE TABLE t1(a UNIQUE);
278 INSERT INTO t1 VALUES(randstr(200, 200));
279 INSERT INTO t1 VALUES(randstr(200, 200));
282 readPagerChangeCounter test.db
284 do_test exclusive2-3.1 {
286 INSERT INTO t1 VALUES(randstr(200, 200));
288 readPagerChangeCounter test.db
290 do_test exclusive2-3.2 {
292 INSERT INTO t1 VALUES(randstr(200, 200));
294 readPagerChangeCounter test.db
296 do_test exclusive2-3.3 {
298 PRAGMA locking_mode = exclusive;
299 INSERT INTO t1 VALUES(randstr(200, 200));
301 readPagerChangeCounter test.db
303 do_test exclusive2-3.4 {
305 INSERT INTO t1 VALUES(randstr(200, 200));
307 readPagerChangeCounter test.db
309 do_test exclusive2-3.5 {
311 PRAGMA locking_mode = normal;
312 INSERT INTO t1 VALUES(randstr(200, 200));
314 readPagerChangeCounter test.db
316 do_test exclusive2-3.6 {
318 INSERT INTO t1 VALUES(randstr(200, 200));
320 readPagerChangeCounter test.db
322 sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)