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 #***********************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 source $testdir/lock_common.tcl
16 source $testdir/malloc_common.tcl
18 ifcapable !mergesort {
23 set testprefix indexfault
25 # Set up the custom fault-injector. This is further configured by using
26 # different values for $::custom_filter and different implementations
27 # of Tcl proc [xCustom] for each test case.
29 proc install_custom_faultsim {} {
30 set ::FAULTSIM(custom) [list \
31 -injectinstall custom_injectinstall \
32 -injectstart custom_injectstart \
33 -injectstop custom_injectstop \
34 -injecterrlist {{1 {disk I/O error}}} \
35 -injectuninstall custom_injectuninstall \
37 proc custom_injectinstall {} {
38 testvfs shmfault -default true
39 shmfault filter $::custom_filter
40 shmfault script xCustom
42 proc custom_injectuninstall {} {
49 proc custom_injectstart {iFail} {
50 set ::custom_ifail $iFail
53 proc custom_injectstop {} {
55 return $::custom_nfail
58 proc uninstall_custom_faultsim {} {
59 unset -nocomplain ::FAULTSIM(custom)
63 #-------------------------------------------------------------------------
64 # These tests - indexfault-1.* - Build an index on a smallish table with
65 # all different kinds of fault-injection. The CREATE INDEX is run once
66 # with default options and once with a 50KB soft-heap-limit.
71 INSERT INTO t1 VALUES(randomblob(202));
72 INSERT INTO t1 SELECT randomblob(202) FROM t1; -- 2
73 INSERT INTO t1 SELECT randomblob(202) FROM t1; -- 4
74 INSERT INTO t1 SELECT randomblob(202) FROM t1; -- 8
75 INSERT INTO t1 SELECT randomblob(202) FROM t1; -- 16
76 INSERT INTO t1 SELECT randomblob(202) FROM t1; -- 32
77 INSERT INTO t1 SELECT randomblob(202) FROM t1; -- 64
78 INSERT INTO t1 SELECT randomblob(202) FROM t1; -- 128
79 INSERT INTO t1 SELECT randomblob(202) FROM t1; -- 256
82 faultsim_save_and_close
84 do_faultsim_test 1.1 -prep {
85 faultsim_restore_and_reopen
87 execsql { CREATE INDEX i1 ON t1(x) }
88 faultsim_test_result {0 {}}
89 faultsim_integrity_check
91 ifcapable memorymanage {
92 set soft_limit [sqlite3_soft_heap_limit 50000]
93 do_faultsim_test 2.1 -prep {
94 faultsim_restore_and_reopen
96 execsql { CREATE INDEX i1 ON t1(x) }
97 faultsim_test_result {0 {}}
99 sqlite3_soft_heap_limit $soft_limit
102 #-------------------------------------------------------------------------
103 # These are similar to the indexfault-1.* tests, except they create an
104 # index with more than one column.
107 do_execsql_test 2.0 {
109 DROP TABLE IF EXISTS t1;
110 CREATE TABLE t1(t,u,v,w,x,y,z);
111 INSERT INTO t1 VALUES(
112 randomblob(30), randomblob(30), randomblob(30), randomblob(30),
113 randomblob(30), randomblob(30), randomblob(30)
115 INSERT INTO t1 SELECT
116 randomblob(30), randomblob(30), randomblob(30), randomblob(30),
117 randomblob(30), randomblob(30), randomblob(30) FROM t1; -- 2
118 INSERT INTO t1 SELECT
119 randomblob(30), randomblob(30), randomblob(30), randomblob(30),
120 randomblob(30), randomblob(30), randomblob(30) FROM t1; -- 4
121 INSERT INTO t1 SELECT
122 randomblob(30), randomblob(30), randomblob(30), randomblob(30),
123 randomblob(30), randomblob(30), randomblob(30) FROM t1; -- 8
124 INSERT INTO t1 SELECT
125 randomblob(30), randomblob(30), randomblob(30), randomblob(30),
126 randomblob(30), randomblob(30), randomblob(30) FROM t1; -- 16
127 INSERT INTO t1 SELECT
128 randomblob(30), randomblob(30), randomblob(30), randomblob(30),
129 randomblob(30), randomblob(30), randomblob(30) FROM t1; -- 32
130 INSERT INTO t1 SELECT
131 randomblob(30), randomblob(30), randomblob(30), randomblob(30),
132 randomblob(30), randomblob(30), randomblob(30) FROM t1; -- 64
133 INSERT INTO t1 SELECT
134 randomblob(30), randomblob(30), randomblob(30), randomblob(30),
135 randomblob(30), randomblob(30), randomblob(30) FROM t1; -- 128
138 faultsim_save_and_close
140 do_faultsim_test 2.1 -prep {
141 faultsim_restore_and_reopen
143 execsql { CREATE INDEX i1 ON t1(t,u,v,w,x,y,z) }
144 faultsim_test_result {0 {}}
145 faultsim_integrity_check
147 ifcapable memorymanage {
148 set soft_limit [sqlite3_soft_heap_limit 50000]
149 do_faultsim_test 2.2 -prep {
150 faultsim_restore_and_reopen
152 execsql { CREATE INDEX i1 ON t1(t,u,v,w,x,y,z) }
153 faultsim_test_result {0 {}}
155 sqlite3_soft_heap_limit $soft_limit
158 #-------------------------------------------------------------------------
159 # The following tests - indexfault-2.* - all attempt to build a index
160 # on table t1 in the main database with injected IO errors. Individual
161 # test cases work as follows:
163 # 3.1: IO errors injected into xOpen() calls.
164 # 3.2: As 7.1, but with a low (50KB) soft-heap-limit.
166 # 3.3: IO errors injected into the first 200 write() calls made on the
167 # second temporary file.
168 # 3.4: As 7.3, but with a low (50KB) soft-heap-limit.
170 # 3.5: After a certain amount of data has been read from the main database
171 # file (and written into the temporary b-tree), sqlite3_release_memory()
172 # is called to free as much memory as possible. This causes the temp
173 # b-tree to be flushed to disk. So that before its contents can be
174 # transfered to a PMA they must be read back from disk - creating extra
175 # opportunities for IO errors.
177 install_custom_faultsim
179 # Set up a table to build indexes on. Save the setup using the
180 # [faultsim_save_and_close] mechanism.
183 do_execsql_test 3.0 {
185 DROP TABLE IF EXISTS t1;
187 INSERT INTO t1 VALUES(randomblob(11000));
188 INSERT INTO t1 SELECT randomblob(11001) FROM t1; -- 2
189 INSERT INTO t1 SELECT randomblob(11002) FROM t1; -- 4
190 INSERT INTO t1 SELECT randomblob(11003) FROM t1; -- 8
191 INSERT INTO t1 SELECT randomblob(11004) FROM t1; -- 16
192 INSERT INTO t1 SELECT randomblob(11005) FROM t1; -- 32
193 INSERT INTO t1 SELECT randomblob(11006) FROM t1; -- 64
194 INSERT INTO t1 SELECT randomblob(11007) FROM t1; -- 128
195 INSERT INTO t1 SELECT randomblob(11008) FROM t1; -- 256
196 INSERT INTO t1 SELECT randomblob(11009) FROM t1; -- 512
199 faultsim_save_and_close
201 set ::custom_filter xOpen
202 proc xCustom {args} {
203 incr ::custom_ifail -1
204 if {$::custom_ifail==0} {
206 return "SQLITE_IOERR"
210 do_faultsim_test 3.1 -faults custom -prep {
211 faultsim_restore_and_reopen
213 execsql { CREATE INDEX i1 ON t1(x) }
214 faultsim_test_result {0 {}}
216 ifcapable memorymanage {
217 set soft_limit [sqlite3_soft_heap_limit 50000]
218 do_faultsim_test 3.2 -faults custom -prep {
219 faultsim_restore_and_reopen
221 execsql { CREATE INDEX i1 ON t1(x) }
222 faultsim_test_result {0 {}}
224 sqlite3_soft_heap_limit $soft_limit
227 set ::custom_filter {xOpen xWrite}
228 proc xCustom {method args} {
229 if {$method == "xOpen"} {
230 if {[lindex $args 0] == ""} {
232 if {$::nTmpOpen == 3} { return "failme" }
236 if {$::custom_ifail<200 && [lindex $args 1] == "failme"} {
237 incr ::custom_ifail -1
238 if {$::custom_ifail==0} {
240 return "SQLITE_IOERR"
246 do_faultsim_test 3.3 -faults custom -prep {
247 faultsim_restore_and_reopen
250 execsql { CREATE INDEX i1 ON t1(x) }
251 faultsim_test_result {0 {}}
254 ifcapable memorymanage {
255 set soft_limit [sqlite3_soft_heap_limit 50000]
256 do_faultsim_test 3.4 -faults custom -prep {
257 faultsim_restore_and_reopen
260 execsql { CREATE INDEX i1 ON t1(x) }
261 faultsim_test_result {0 {}}
263 sqlite3_soft_heap_limit $soft_limit
266 uninstall_custom_faultsim
268 #-------------------------------------------------------------------------
269 # Test 4: After a certain amount of data has been read from the main database
270 # file (and written into the temporary b-tree), sqlite3_release_memory() is
271 # called to free as much memory as possible. This causes the temp b-tree to be
272 # flushed to disk. So that before its contents can be transfered to a PMA they
273 # must be read back from disk - creating extra opportunities for IO errors.
275 install_custom_faultsim
281 do_execsql_test 4.0 {
283 DROP TABLE IF EXISTS t1;
285 INSERT INTO t1 VALUES(randomblob(11000));
286 INSERT INTO t1 SELECT randomblob(11001) FROM t1; -- 2
287 INSERT INTO t1 SELECT randomblob(11002) FROM t1; -- 4
288 INSERT INTO t1 SELECT randomblob(11003) FROM t1; -- 8
289 INSERT INTO t1 SELECT randomblob(11004) FROM t1; -- 16
290 INSERT INTO t1 SELECT randomblob(11005) FROM t1; -- 32
291 INSERT INTO t1 SELECT randomblob(11005) FROM t1; -- 64
294 faultsim_save_and_close
300 proc xRead {method file args} {
301 if {[file tail $file] == "test.db"} { incr ::nRead }
305 sqlite3 db test.db -vfs tvfs
306 execsql { CREATE INDEX i1 ON t1(x) }
312 set ::custom_filter xRead
313 proc xCustom {method file args} {
315 if {$::nReadCall >= ($::nRead/5)} {
316 if {$::nReadCall == ($::nRead/5)} {
317 set nByte [sqlite3_release_memory [expr 64*1024*1024]]
318 sqlite3_soft_heap_limit 20000
321 incr ::custom_ifail -1
322 if {$::custom_ifail==0} {
324 return "SQLITE_IOERR"
331 do_faultsim_test 4.2 -faults custom -prep {
332 faultsim_restore_and_reopen
334 sqlite3_soft_heap_limit 0
336 execsql { CREATE INDEX i1 ON t1(x) }
337 faultsim_test_result {0 {}}
340 uninstall_custom_faultsim