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 #***********************************************************************
12 # This file contains tests that verify that SQLite can correctly rollback
13 # databases after crashes when using the special IO modes triggered
14 # by device IOCAP flags.
16 # $Id: crash3.test,v 1.4 2008/07/12 14:52:20 drh Exp $
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
21 ifcapable !crashtest {
26 proc do_test2 {name tcl res1 res2} {
27 set script [subst -nocommands {
32 if {[set res] eq [set res1] || [set res] eq [set res2]} {
33 set res "{[set res1]} or {[set res2]}"
36 } {{$res1} or {$res2}}
41 # This block tests crash-recovery when the IOCAP_ATOMIC flags is set.
43 # Each iteration of the following loop sets up the database to contain
44 # the following schema and data:
46 # CREATE TABLE abc(a, b, c);
47 # INSERT INTO abc VALUES(1, 2, 3);
49 # Then execute the SQL statement, scheduling a crash for part-way through
50 # the first sync() of either the database file or the journal file (often
51 # the journal file is not required - meaning no crash occurs).
53 # After the crash (or absence of a crash), open the database and
56 # * The integrity check passes, and
57 # * The contents of table abc is either {1 2 3} or the value specified
58 # to the right of the SQL statement below.
60 # The procedure is repeated 10 times for each SQL statement. Five times
61 # with the crash scheduled for midway through the first journal sync (if
62 # any), and five times with the crash midway through the database sync.
65 foreach {sql res2} [list \
66 {INSERT INTO abc VALUES(4, 5, 6)} {1 2 3 4 5 6} \
67 {DELETE FROM abc} {} \
68 {INSERT INTO abc SELECT * FROM abc} {1 2 3 1 2 3} \
69 {UPDATE abc SET a = 2} {2 2 3} \
70 {INSERT INTO abc VALUES(4, 5, randstr(1000,1000))} {n/a} \
71 {CREATE TABLE def(d, e, f)} {n/a} \
73 for {set ii 0} {$ii < 10} {incr ii} {
76 forcedelete test.db test.db-journal
78 do_test crash3-1.$tn.1 {
80 PRAGMA page_size = 1024;
82 CREATE TABLE abc(a, b, c);
83 INSERT INTO abc VALUES(1, 2, 3);
90 if {($ii%2)==0} { append crashfile -journal }
91 set rand "SELECT randstr($tn,$tn);"
92 do_test crash3-1.$tn.2 [subst {
93 crashsql -file $crashfile -char atomic {$rand $sql}
95 execsql { PRAGMA integrity_check; }
98 do_test2 crash3-1.$tn.3 {
99 execsql { SELECT * FROM abc }
106 # This block tests both the IOCAP_SEQUENTIAL and IOCAP_SAFE_APPEND flags.
109 forcedelete test.db test.db-journal
114 CREATE TABLE abc(a PRIMARY KEY, b, c);
115 CREATE TABLE def(d PRIMARY KEY, e, f);
116 PRAGMA default_cache_size = 10;
117 INSERT INTO abc VALUES(randstr(10,1000),randstr(10,1000),randstr(10,1000));
119 SELECT randstr(10,1000),randstr(10,1000),randstr(10,1000) FROM abc;
121 SELECT randstr(10,1000),randstr(10,1000),randstr(10,1000) FROM abc;
123 SELECT randstr(10,1000),randstr(10,1000),randstr(10,1000) FROM abc;
125 SELECT randstr(10,1000),randstr(10,1000),randstr(10,1000) FROM abc;
127 SELECT randstr(10,1000),randstr(10,1000),randstr(10,1000) FROM abc;
129 SELECT randstr(10,1000),randstr(10,1000),randstr(10,1000) FROM abc;
135 foreach {::crashfile ::delay ::char} {
137 test.db 1 safe_append
138 test.db-journal 1 sequential
139 test.db-journal 1 safe_append
140 test.db-journal 2 safe_append
141 test.db-journal 2 sequential
142 test.db-journal 3 sequential
143 test.db-journal 3 safe_append
145 for {set ii 0} {$ii < 100} {incr ii} {
147 SELECT randstr($ii,$ii+10);
149 DELETE FROM abc WHERE random()%5;
151 SELECT randstr(10,1000),randstr(10,1000),randstr(10,1000)
153 WHERE (random()%5)==0;
154 DELETE FROM def WHERE random()%5;
156 SELECT randstr(10,1000),randstr(10,1000),randstr(10,1000)
158 WHERE (random()%5)==0;
162 do_test crash3-2.$tn.$ii {
163 crashsql -file $::crashfile -delay $::delay -char $::char $::SQL
166 execsql {PRAGMA integrity_check}
172 # The following block tests an interaction between IOCAP_ATOMIC and
173 # IOCAP_SEQUENTIAL. At one point, if both flags were set, small
174 # journal files that contained only a single page, but were required
175 # for some other reason (i.e. nTrunk) were not being written to
178 for {set ii 0} {$ii < 10} {incr ii} {
180 forcedelete test.db test.db-journal
181 crashsql -file test.db -char {sequential atomic} {
182 CREATE TABLE abc(a, b, c);
185 do_test crash3-3.$ii {
186 execsql {PRAGMA integrity_check}