blob: cfce5fed6d7cf14c10569b29e2983283a2305781 [file] [log] [blame]
dan7c246102010-04-12 19:00:29 +00001# 2010 February 8
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#***********************************************************************
dand3f8f942010-04-13 11:35:01 +000011# This file implements regression tests for SQLite library. The
12# focus of this file is testing the operation of the library when
13# recovering a database following a simulated system failure in
14# "PRAGMA journal_mode=WAL" mode.
dan7c246102010-04-12 19:00:29 +000015#
16
17#
18# These are 'warm-body' tests of database recovery used while developing
19# the WAL code. They serve to prove that a few really simple cases work:
20#
21# walcrash-1.*: Recover a database.
22# walcrash-2.*: Recover a database where the failed transaction spanned more
23# than one page.
24# walcrash-3.*: Recover multiple databases where the failed transaction
25# was a multi-file transaction.
26#
27
28set testdir [file dirname $argv0]
29source $testdir/tester.tcl
dan5cf53532010-05-01 16:40:20 +000030ifcapable !wal {finish_test ; return }
31
dan7c246102010-04-12 19:00:29 +000032db close
33
34set seed 0
35set REPEATS 100
36
dan7c246102010-04-12 19:00:29 +000037# walcrash-1.*
38#
39for {set i 1} {$i < $REPEATS} {incr i} {
mistachkinfda06be2011-08-02 00:57:34 +000040 forcedelete test.db test.db-wal
dan7c246102010-04-12 19:00:29 +000041 do_test walcrash-1.$i.1 {
42 crashsql -delay 4 -file test.db-wal -seed [incr seed] {
43 PRAGMA journal_mode = WAL;
44 CREATE TABLE t1(a, b);
45 INSERT INTO t1 VALUES(1, 1);
46 INSERT INTO t1 VALUES(2, 3);
47 INSERT INTO t1 VALUES(3, 6);
48 }
49 } {1 {child process exited abnormally}}
50 do_test walcrash-1.$i.2 {
dana470aeb2010-04-21 11:43:38 +000051 sqlite3 db test.db
dan7c246102010-04-12 19:00:29 +000052 execsql { SELECT sum(a)==max(b) FROM t1 }
53 } {1}
54 integrity_check walcrash-1.$i.3
55 db close
56
57 do_test walcrash-1.$i.4 {
58 crashsql -delay 2 -file test.db-wal -seed [incr seed] {
dan7c246102010-04-12 19:00:29 +000059 INSERT INTO t1 VALUES(4, (SELECT sum(a) FROM t1) + 4);
60 INSERT INTO t1 VALUES(5, (SELECT sum(a) FROM t1) + 5);
61 }
62 } {1 {child process exited abnormally}}
63 do_test walcrash-1.$i.5 {
dana470aeb2010-04-21 11:43:38 +000064 sqlite3 db test.db
dan7c246102010-04-12 19:00:29 +000065 execsql { SELECT sum(a)==max(b) FROM t1 }
66 } {1}
67 integrity_check walcrash-1.$i.6
shaneh504b2512010-07-08 16:22:05 +000068 do_test walcrash-1.$i.7 {
dana470aeb2010-04-21 11:43:38 +000069 execsql { PRAGMA main.journal_mode }
70 } {wal}
dan7c246102010-04-12 19:00:29 +000071 db close
72}
73
74# walcrash-2.*
75#
76for {set i 1} {$i < $REPEATS} {incr i} {
mistachkinfda06be2011-08-02 00:57:34 +000077 forcedelete test.db test.db-wal
dan7c246102010-04-12 19:00:29 +000078 do_test walcrash-2.$i.1 {
79 crashsql -delay 4 -file test.db-wal -seed [incr seed] {
80 PRAGMA journal_mode = WAL;
81 CREATE TABLE t1(a PRIMARY KEY, b);
82 INSERT INTO t1 VALUES(1, 2);
83 INSERT INTO t1 VALUES(3, 4);
84 INSERT INTO t1 VALUES(5, 9);
85 }
86 } {1 {child process exited abnormally}}
87 do_test walcrash-2.$i.2 {
dana470aeb2010-04-21 11:43:38 +000088 sqlite3 db test.db
dan7c246102010-04-12 19:00:29 +000089 execsql { SELECT sum(a)==max(b) FROM t1 }
90 } {1}
91 integrity_check walcrash-2.$i.3
92 db close
93
94 do_test walcrash-2.$i.4 {
95 crashsql -delay 2 -file test.db-wal -seed [incr seed] {
dan7c246102010-04-12 19:00:29 +000096 INSERT INTO t1 VALUES(6, (SELECT sum(a) FROM t1) + 6);
97 INSERT INTO t1 VALUES(7, (SELECT sum(a) FROM t1) + 7);
98 }
99 } {1 {child process exited abnormally}}
100 do_test walcrash-2.$i.5 {
dana470aeb2010-04-21 11:43:38 +0000101 sqlite3 db test.db
dan7c246102010-04-12 19:00:29 +0000102 execsql { SELECT sum(a)==max(b) FROM t1 }
103 } {1}
104 integrity_check walcrash-2.$i.6
dana470aeb2010-04-21 11:43:38 +0000105 do_test walcrash-2.$i.6 {
106 execsql { PRAGMA main.journal_mode }
107 } {wal}
dan7c246102010-04-12 19:00:29 +0000108 db close
109}
110
111# walcrash-3.*
112#
113# for {set i 1} {$i < $REPEATS} {incr i} {
mistachkinfda06be2011-08-02 00:57:34 +0000114# forcedelete test.db test.db-wal
115# forcedelete test2.db test2.db-wal
dan7c246102010-04-12 19:00:29 +0000116#
117# do_test walcrash-3.$i.1 {
118# crashsql -delay 2 -file test2.db-wal -seed [incr seed] {
119# PRAGMA journal_mode = WAL;
120# ATTACH 'test2.db' AS aux;
121# CREATE TABLE t1(a PRIMARY KEY, b);
122# CREATE TABLE aux.t2(a PRIMARY KEY, b);
123# BEGIN;
124# INSERT INTO t1 VALUES(1, 2);
125# INSERT INTO t2 VALUES(1, 2);
126# COMMIT;
127# }
128# } {1 {child process exited abnormally}}
129#
130# do_test walcrash-3.$i.2 {
131# sqlite3_wal db test.db
132# execsql {
133# ATTACH 'test2.db' AS aux;
134# SELECT * FROM t1 EXCEPT SELECT * FROM t2;
135# }
136# } {}
137# do_test walcrash-3.$i.3 { execsql { PRAGMA main.integrity_check } } {ok}
138# do_test walcrash-3.$i.4 { execsql { PRAGMA aux.integrity_check } } {ok}
139#
140# db close
141# }
142
143# walcrash-4.*
144#
145for {set i 1} {$i < $REPEATS} {incr i} {
mistachkinfda06be2011-08-02 00:57:34 +0000146 forcedelete test.db test.db-wal
147 forcedelete test2.db test2.db-wal
dan7c246102010-04-12 19:00:29 +0000148
149 do_test walcrash-4.$i.1 {
150 crashsql -delay 3 -file test.db-wal -seed [incr seed] -blocksize 4096 {
151 PRAGMA journal_mode = WAL;
152 PRAGMA page_size = 1024;
153 CREATE TABLE t1(a PRIMARY KEY, b);
154 INSERT INTO t1 VALUES(1, 2);
155 INSERT INTO t1 VALUES(3, 4);
156 }
157 } {1 {child process exited abnormally}}
158
159 do_test walcrash-4.$i.2 {
dana470aeb2010-04-21 11:43:38 +0000160 sqlite3 db test.db
dan7c246102010-04-12 19:00:29 +0000161 execsql {
162 SELECT * FROM t1 WHERE a = 1;
163 }
164 } {1 2}
165 do_test walcrash-4.$i.3 { execsql { PRAGMA main.integrity_check } } {ok}
dana470aeb2010-04-21 11:43:38 +0000166 do_test walcrash-4.$i.4 { execsql { PRAGMA main.journal_mode } } {wal}
dan7c246102010-04-12 19:00:29 +0000167
168 db close
169}
170
171# walcrash-5.*
172#
173for {set i 1} {$i < $REPEATS} {incr i} {
mistachkinfda06be2011-08-02 00:57:34 +0000174 forcedelete test.db test.db-wal
175 forcedelete test2.db test2.db-wal
dan7c246102010-04-12 19:00:29 +0000176
177 do_test walcrash-5.$i.1 {
178 crashsql -delay 11 -file test.db-wal -seed [incr seed] -blocksize 4096 {
179 PRAGMA journal_mode = WAL;
180 PRAGMA page_size = 1024;
181 BEGIN;
182 CREATE TABLE t1(x PRIMARY KEY);
183 INSERT INTO t1 VALUES(randomblob(900));
184 INSERT INTO t1 VALUES(randomblob(900));
185 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4 */
186 COMMIT;
187 INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 8 */
188 INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 12 */
189 INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 16 */
190 INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 20 */
191 INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 24 */
192 INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 28 */
193 INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 32 */
194
dan5a299f92010-05-03 11:05:08 +0000195 PRAGMA wal_checkpoint;
dan7c246102010-04-12 19:00:29 +0000196 INSERT INTO t1 VALUES(randomblob(900));
197 INSERT INTO t1 VALUES(randomblob(900));
198 INSERT INTO t1 VALUES(randomblob(900));
199 }
200 } {1 {child process exited abnormally}}
201
202 do_test walcrash-5.$i.2 {
dana470aeb2010-04-21 11:43:38 +0000203 sqlite3 db test.db
dan7c246102010-04-12 19:00:29 +0000204 execsql { SELECT count(*)==33 OR count(*)==34 FROM t1 WHERE x != 1 }
205 } {1}
206 do_test walcrash-5.$i.3 { execsql { PRAGMA main.integrity_check } } {ok}
dana470aeb2010-04-21 11:43:38 +0000207 do_test walcrash-5.$i.4 { execsql { PRAGMA main.journal_mode } } {wal}
dan7c246102010-04-12 19:00:29 +0000208
209 db close
210}
211
212# walcrash-6.*
213#
214for {set i 1} {$i < $REPEATS} {incr i} {
mistachkinfda06be2011-08-02 00:57:34 +0000215 forcedelete test.db test.db-wal
216 forcedelete test2.db test2.db-wal
dan7c246102010-04-12 19:00:29 +0000217
218 do_test walcrash-6.$i.1 {
219 crashsql -delay 12 -file test.db-wal -seed [incr seed] -blocksize 512 {
220 PRAGMA journal_mode = WAL;
221 PRAGMA page_size = 1024;
222 BEGIN;
223 CREATE TABLE t1(x PRIMARY KEY);
224 INSERT INTO t1 VALUES(randomblob(900));
225 INSERT INTO t1 VALUES(randomblob(900));
226 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4 */
227 COMMIT;
228 INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 8 */
229 INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 12 */
230 INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 16 */
231 INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 20 */
232 INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 24 */
233 INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 28 */
234 INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 32 */
235
dan5a299f92010-05-03 11:05:08 +0000236 PRAGMA wal_checkpoint;
dan7c246102010-04-12 19:00:29 +0000237 INSERT INTO t1 VALUES(randomblob(900));
238 INSERT INTO t1 VALUES(randomblob(900));
239 INSERT INTO t1 VALUES(randomblob(900));
240 }
241 } {1 {child process exited abnormally}}
242
243 do_test walcrash-6.$i.2 {
dana470aeb2010-04-21 11:43:38 +0000244 sqlite3 db test.db
dan7c246102010-04-12 19:00:29 +0000245 execsql { SELECT count(*)==34 OR count(*)==35 FROM t1 WHERE x != 1 }
246 } {1}
247 do_test walcrash-6.$i.3 { execsql { PRAGMA main.integrity_check } } {ok}
dana470aeb2010-04-21 11:43:38 +0000248 do_test walcrash-6.$i.4 { execsql { PRAGMA main.journal_mode } } {wal}
249
250 db close
251}
252
danb6e099a2010-05-04 14:47:39 +0000253#-------------------------------------------------------------------------
254# This test case simulates a crash while checkpointing the database. Page
255# 1 is one of the pages overwritten by the checkpoint. This is a special
256# case because it means the content of page 1 may be damaged. SQLite will
257# have to determine:
258#
259# (a) that the database is a WAL database, and
260# (b) the database page-size
261#
262# based on the log file.
263#
dana470aeb2010-04-21 11:43:38 +0000264for {set i 1} {$i < $REPEATS} {incr i} {
mistachkinfda06be2011-08-02 00:57:34 +0000265 forcedelete test.db test.db-wal
dana470aeb2010-04-21 11:43:38 +0000266
danb6e099a2010-05-04 14:47:39 +0000267 # Select a page-size for this test.
268 #
269 set pgsz [lindex {512 1024 2048 4096 8192 16384} [expr $i%6]]
270
dana470aeb2010-04-21 11:43:38 +0000271 do_test walcrash-7.$i.1 {
danb6e099a2010-05-04 14:47:39 +0000272 crashsql -delay 3 -file test.db -seed [incr seed] -blocksize 512 "
273 PRAGMA page_size = $pgsz;
dana470aeb2010-04-21 11:43:38 +0000274 PRAGMA journal_mode = wal;
275 BEGIN;
276 CREATE TABLE t1(a, b);
277 INSERT INTO t1 VALUES(1, 2);
278 COMMIT;
dan5a299f92010-05-03 11:05:08 +0000279 PRAGMA wal_checkpoint;
dana470aeb2010-04-21 11:43:38 +0000280 CREATE INDEX i1 ON t1(a);
dan5a299f92010-05-03 11:05:08 +0000281 PRAGMA wal_checkpoint;
danb6e099a2010-05-04 14:47:39 +0000282 "
dana470aeb2010-04-21 11:43:38 +0000283 } {1 {child process exited abnormally}}
284
285 do_test walcrash-7.$i.2 {
286 sqlite3 db test.db
287 execsql { SELECT b FROM t1 WHERE a = 1 }
288 } {2}
289 do_test walcrash-7.$i.3 { execsql { PRAGMA main.integrity_check } } {ok}
290 do_test walcrash-7.$i.4 { execsql { PRAGMA main.journal_mode } } {wal}
dan7c246102010-04-12 19:00:29 +0000291
292 db close
293}
294
295finish_test
296