drh | 5a3032b | 2007-09-03 16:12:09 +0000 | [diff] [blame] | 1 | # 2007 May 05 |
| 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 | #*********************************************************************** |
| 11 | # |
| 12 | # This file contains common code used by many different malloc tests |
| 13 | # within the test suite. |
| 14 | # |
danielk1977 | 98c2190 | 2008-09-23 16:41:29 +0000 | [diff] [blame] | 15 | # $Id: malloc_common.tcl,v 1.22 2008/09/23 16:41:30 danielk1977 Exp $ |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 16 | |
drh | 5a3032b | 2007-09-03 16:12:09 +0000 | [diff] [blame] | 17 | # If we did not compile with malloc testing enabled, then do nothing. |
| 18 | # |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 19 | ifcapable builtin_test { |
drh | 5efaf07 | 2008-03-18 00:07:10 +0000 | [diff] [blame] | 20 | set MEMDEBUG 1 |
| 21 | } else { |
drh | eee4c8c | 2008-02-18 22:24:57 +0000 | [diff] [blame] | 22 | set MEMDEBUG 0 |
danielk1977 | 369ff42 | 2007-09-03 07:31:09 +0000 | [diff] [blame] | 23 | return 0 |
danielk1977 | cdc3a6b | 2007-08-25 13:09:26 +0000 | [diff] [blame] | 24 | } |
| 25 | |
dan | a4bc123 | 2010-06-01 17:46:38 +0000 | [diff] [blame^] | 26 | |
| 27 | #-------------------------------------------------------------------------- |
| 28 | # Usage do_faultsim_test NAME ?OPTIONS...? |
| 29 | # |
| 30 | # -faults List of fault types to simulate. |
| 31 | # |
| 32 | # -prep Script to execute before -body. |
| 33 | # |
| 34 | # -body Script to execute (with fault injection). |
| 35 | # |
| 36 | # -test Script to execute after -body. |
| 37 | # |
| 38 | proc do_faultsim_test {name args} { |
| 39 | set DEFAULT(-faults) [list \ |
| 40 | oom-transient oom-persistent \ |
| 41 | ioerr-transient ioerr-persistent \ |
| 42 | ] |
| 43 | set DEFAULT(-prep) "" |
| 44 | set DEFAULT(-body) "" |
| 45 | set DEFAULT(-test) "" |
| 46 | |
| 47 | array set O [array get DEFAULT] |
| 48 | array set O $args |
| 49 | foreach o [array names O] { |
| 50 | if {[info exists DEFAULT($o)]==0} { error "unknown option: $o" } |
| 51 | } |
| 52 | |
| 53 | set A(oom-transient) [list \ |
| 54 | -injectstart {oom_injectstart 0} \ |
| 55 | -injectstop oom_injectstop \ |
| 56 | -injecterrlist {{1 {out of memory}}} \ |
| 57 | ] |
| 58 | set A(oom-persistent) [list \ |
| 59 | -injectstart {oom_injectstart 1000000} \ |
| 60 | -injectstop oom_injectstop \ |
| 61 | -injecterrlist {{1 {out of memory}}} \ |
| 62 | ] |
| 63 | |
| 64 | set A(ioerr-transient) [list \ |
| 65 | -injectstart {ioerr_injectstart 0} \ |
| 66 | -injectstop ioerr_injectstop \ |
| 67 | -injecterrlist {{1 {disk I/O error}}} \ |
| 68 | ] |
| 69 | |
| 70 | set A(ioerr-persistent) [list \ |
| 71 | -injectstart {ioerr_injectstart 1} \ |
| 72 | -injectstop ioerr_injectstop \ |
| 73 | -injecterrlist {{1 {disk I/O error}}} \ |
| 74 | ] |
| 75 | |
| 76 | foreach f $O(-faults) { |
| 77 | if {[info exists A($f)]==0} { error "unknown fault: $f" } |
| 78 | } |
| 79 | set testspec [list -prep $O(-prep) -body $O(-body) -test $O(-test)] |
| 80 | foreach f $O(-faults) { |
| 81 | eval do_one_faultsim_test "$name-$f" $A($f) $testspec |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | #------------------------------------------------------------------------- |
| 86 | # Procedures to save and restore the current file-system state: |
| 87 | # |
| 88 | # faultsim_save_and_close |
| 89 | # faultsim_restore_and_reopen |
| 90 | # |
| 91 | proc faultsim_save_and_close {} { |
| 92 | foreach {a => b} { |
| 93 | test.db => testX.db |
| 94 | test.db-wal => testX.db-wal |
| 95 | test.db-journal => testX.db-journal |
| 96 | } { |
| 97 | if {[file exists $a]} { |
| 98 | file copy -force $a $b |
| 99 | } else { |
| 100 | file delete -force $b |
| 101 | } |
| 102 | } |
| 103 | catch { db close } |
| 104 | return "" |
| 105 | } |
| 106 | proc faultsim_restore_and_reopen {} { |
| 107 | catch { db close } |
| 108 | foreach {a => b} { |
| 109 | testX.db => test.db |
| 110 | testX.db-wal => test.db-wal |
| 111 | testX.db-journal => test.db-journal |
| 112 | } { |
| 113 | if {[file exists $a]} { |
| 114 | file copy -force $a $b |
| 115 | } else { |
| 116 | file delete -force $b |
| 117 | } |
| 118 | } |
| 119 | sqlite3 db test.db |
| 120 | sqlite3_extended_result_codes db 1 |
| 121 | sqlite3_db_config_lookaside db 0 0 0 |
| 122 | } |
| 123 | |
| 124 | |
dan | 8521abf | 2010-05-31 06:38:34 +0000 | [diff] [blame] | 125 | # The following procs are used as [do_faultsim_test] when injecting OOM |
| 126 | # faults into test cases. |
| 127 | # |
| 128 | proc oom_injectstart {nRepeat iFail} { |
| 129 | sqlite3_memdebug_fail $iFail -repeat $nRepeat |
| 130 | } |
| 131 | proc oom_injectstop {} { |
| 132 | sqlite3_memdebug_fail -1 |
| 133 | } |
| 134 | |
dan | a4bc123 | 2010-06-01 17:46:38 +0000 | [diff] [blame^] | 135 | proc ioerr_injectstart {persist iFail} { |
| 136 | set ::sqlite_io_error_persist $persist |
| 137 | set ::sqlite_io_error_pending $iFail |
| 138 | } |
| 139 | proc ioerr_injectstop {} { |
| 140 | set sv $::sqlite_io_error_hit |
| 141 | set ::sqlite_io_error_persist 0 |
| 142 | set ::sqlite_io_error_pending 0 |
| 143 | set ::sqlite_io_error_hardhit 0 |
| 144 | set ::sqlite_io_error_hit 0 |
| 145 | set ::sqlite_io_error_pending 0 |
| 146 | return $sv |
| 147 | } |
| 148 | |
| 149 | # This command is not called directly. It is used by the |
| 150 | # [faultsim_test_result] command created by [do_faultsim_test] and used |
| 151 | # by -test scripts. |
dan | 8521abf | 2010-05-31 06:38:34 +0000 | [diff] [blame] | 152 | # |
dan | a4bc123 | 2010-06-01 17:46:38 +0000 | [diff] [blame^] | 153 | proc faultsim_test_result_int {args} { |
dan | 8521abf | 2010-05-31 06:38:34 +0000 | [diff] [blame] | 154 | upvar testrc testrc testresult testresult testnfail testnfail |
| 155 | set t [list $testrc $testresult] |
dan | a4bc123 | 2010-06-01 17:46:38 +0000 | [diff] [blame^] | 156 | set r $args |
dan | 8521abf | 2010-05-31 06:38:34 +0000 | [diff] [blame] | 157 | if { ($testnfail==0 && $t != [lindex $r 0]) || [lsearch $r $t]<0 } { |
| 158 | error "nfail=$testnfail rc=$testrc result=$testresult" |
| 159 | } |
| 160 | } |
| 161 | |
dan | a4bc123 | 2010-06-01 17:46:38 +0000 | [diff] [blame^] | 162 | #-------------------------------------------------------------------------- |
| 163 | # Usage do_one_faultsim_test NAME ?OPTIONS...? |
dan | 8521abf | 2010-05-31 06:38:34 +0000 | [diff] [blame] | 164 | # |
| 165 | # The first argument, <test number>, is used as a prefix of the test names |
| 166 | # taken by tests executed by this command. Options are as follows. All |
| 167 | # options take a single argument. |
| 168 | # |
| 169 | # -injectstart Script to enable fault-injection. |
| 170 | # |
| 171 | # -injectstop Script to disable fault-injection. |
| 172 | # |
dan | a4bc123 | 2010-06-01 17:46:38 +0000 | [diff] [blame^] | 173 | # -injecterrlist List of generally acceptable test results (i.e. error |
| 174 | # messages). Example: [list {1 {out of memory}}] |
| 175 | # |
dan | 8521abf | 2010-05-31 06:38:34 +0000 | [diff] [blame] | 176 | # -prep Script to execute before -body. |
| 177 | # |
| 178 | # -body Script to execute (with fault injection). |
| 179 | # |
| 180 | # -test Script to execute after -body. |
| 181 | # |
dan | a4bc123 | 2010-06-01 17:46:38 +0000 | [diff] [blame^] | 182 | proc do_one_faultsim_test {testname args} { |
dan | 8521abf | 2010-05-31 06:38:34 +0000 | [diff] [blame] | 183 | |
dan | a4bc123 | 2010-06-01 17:46:38 +0000 | [diff] [blame^] | 184 | set DEFAULT(-injectstart) {oom_injectstart 0} |
| 185 | set DEFAULT(-injectstop) {oom_injectstop} |
| 186 | set DEFAULT(-injecterrlist) [list {1 {out of memory}}] |
| 187 | set DEFAULT(-prep) "" |
| 188 | set DEFAULT(-body) "" |
| 189 | set DEFAULT(-test) "" |
dan | 8521abf | 2010-05-31 06:38:34 +0000 | [diff] [blame] | 190 | |
| 191 | array set O [array get DEFAULT] |
| 192 | array set O $args |
| 193 | foreach o [array names O] { |
| 194 | if {[info exists DEFAULT($o)]==0} { error "unknown option: $o" } |
| 195 | } |
| 196 | |
| 197 | proc faultsim_test_proc {testrc testresult testnfail} $O(-test) |
dan | a4bc123 | 2010-06-01 17:46:38 +0000 | [diff] [blame^] | 198 | proc faultsim_test_result {args} " |
| 199 | uplevel faultsim_test_result_int \$args [list $O(-injecterrlist)] |
| 200 | " |
dan | 8521abf | 2010-05-31 06:38:34 +0000 | [diff] [blame] | 201 | |
| 202 | set stop 0 |
| 203 | for {set iFail 1} {!$stop} {incr iFail} { |
| 204 | |
| 205 | # Evaluate the -prep script. |
| 206 | # |
| 207 | eval $O(-prep) |
| 208 | |
| 209 | # Start the fault-injection. Run the -body script. Stop the fault |
| 210 | # injection. Local var $nfail is set to the total number of faults |
| 211 | # injected into the system this trial. |
| 212 | # |
| 213 | eval $O(-injectstart) $iFail |
| 214 | set rc [catch $O(-body) res] |
| 215 | set nfail [eval $O(-injectstop)] |
| 216 | |
| 217 | # Run the -test script. If it throws no error, consider this trial |
| 218 | # sucessful. If it does throw an error, cause a [do_test] test to |
| 219 | # fail (and print out the unexpected exception thrown by the -test |
| 220 | # script at the same time). |
| 221 | # |
| 222 | set rc [catch [list faultsim_test_proc $rc $res $nfail] res] |
| 223 | if {$rc == 0} {set res ok} |
| 224 | do_test $testname.$iFail [list list $rc $res] {0 ok} |
| 225 | |
| 226 | # If no faults where injected this trial, don't bother running |
| 227 | # any more. This test is finished. |
| 228 | # |
| 229 | if {$nfail==0} { set stop 1 } |
| 230 | } |
| 231 | } |
| 232 | |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 233 | # Usage: do_malloc_test <test number> <options...> |
| 234 | # |
| 235 | # The first argument, <test number>, is an integer used to name the |
| 236 | # tests executed by this proc. Options are as follows: |
| 237 | # |
| 238 | # -tclprep TCL script to run to prepare test. |
| 239 | # -sqlprep SQL script to run to prepare test. |
| 240 | # -tclbody TCL script to run with malloc failure simulation. |
| 241 | # -sqlbody TCL script to run with malloc failure simulation. |
| 242 | # -cleanup TCL script to run after the test. |
| 243 | # |
| 244 | # This command runs a series of tests to verify SQLite's ability |
| 245 | # to handle an out-of-memory condition gracefully. It is assumed |
| 246 | # that if this condition occurs a malloc() call will return a |
| 247 | # NULL pointer. Linux, for example, doesn't do that by default. See |
| 248 | # the "BUGS" section of malloc(3). |
| 249 | # |
| 250 | # Each iteration of a loop, the TCL commands in any argument passed |
| 251 | # to the -tclbody switch, followed by the SQL commands in any argument |
| 252 | # passed to the -sqlbody switch are executed. Each iteration the |
| 253 | # Nth call to sqliteMalloc() is made to fail, where N is increased |
| 254 | # each time the loop runs starting from 1. When all commands execute |
| 255 | # successfully, the loop ends. |
| 256 | # |
| 257 | proc do_malloc_test {tn args} { |
| 258 | array unset ::mallocopts |
| 259 | array set ::mallocopts $args |
| 260 | |
| 261 | if {[string is integer $tn]} { |
| 262 | set tn malloc-$tn |
| 263 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 264 | if {[info exists ::mallocopts(-start)]} { |
| 265 | set start $::mallocopts(-start) |
| 266 | } else { |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 267 | set start 0 |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 268 | } |
drh | 1527ff4 | 2008-01-18 17:03:32 +0000 | [diff] [blame] | 269 | if {[info exists ::mallocopts(-end)]} { |
| 270 | set end $::mallocopts(-end) |
| 271 | } else { |
| 272 | set end 50000 |
| 273 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 274 | save_prng_state |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 275 | |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 276 | foreach ::iRepeat {0 10000000} { |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 277 | set ::go 1 |
drh | 1527ff4 | 2008-01-18 17:03:32 +0000 | [diff] [blame] | 278 | for {set ::n $start} {$::go && $::n <= $end} {incr ::n} { |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 279 | |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 280 | # If $::iRepeat is 0, then the malloc() failure is transient - it |
| 281 | # fails and then subsequent calls succeed. If $::iRepeat is 1, |
| 282 | # then the failure is persistent - once malloc() fails it keeps |
| 283 | # failing. |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 284 | # |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 285 | set zRepeat "transient" |
| 286 | if {$::iRepeat} {set zRepeat "persistent"} |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 287 | restore_prng_state |
| 288 | foreach file [glob -nocomplain test.db-mj*] {file delete -force $file} |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 289 | |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 290 | do_test ${tn}.${zRepeat}.${::n} { |
| 291 | |
| 292 | # Remove all traces of database files test.db and test2.db |
| 293 | # from the file-system. Then open (empty database) "test.db" |
| 294 | # with the handle [db]. |
| 295 | # |
| 296 | catch {db close} |
| 297 | catch {file delete -force test.db} |
| 298 | catch {file delete -force test.db-journal} |
dan | 5e9e482 | 2010-05-03 18:01:21 +0000 | [diff] [blame] | 299 | catch {file delete -force test.db-wal} |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 300 | catch {file delete -force test2.db} |
| 301 | catch {file delete -force test2.db-journal} |
dan | 5e9e482 | 2010-05-03 18:01:21 +0000 | [diff] [blame] | 302 | catch {file delete -force test2.db-wal} |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 303 | if {[info exists ::mallocopts(-testdb)]} { |
| 304 | file copy $::mallocopts(-testdb) test.db |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 305 | } |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 306 | catch { sqlite3 db test.db } |
| 307 | if {[info commands db] ne ""} { |
| 308 | sqlite3_extended_result_codes db 1 |
| 309 | } |
drh | e9d1c72 | 2008-08-04 20:13:26 +0000 | [diff] [blame] | 310 | sqlite3_db_config_lookaside db 0 0 0 |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 311 | |
| 312 | # Execute any -tclprep and -sqlprep scripts. |
| 313 | # |
| 314 | if {[info exists ::mallocopts(-tclprep)]} { |
| 315 | eval $::mallocopts(-tclprep) |
| 316 | } |
| 317 | if {[info exists ::mallocopts(-sqlprep)]} { |
| 318 | execsql $::mallocopts(-sqlprep) |
| 319 | } |
| 320 | |
| 321 | # Now set the ${::n}th malloc() to fail and execute the -tclbody |
| 322 | # and -sqlbody scripts. |
| 323 | # |
| 324 | sqlite3_memdebug_fail $::n -repeat $::iRepeat |
| 325 | set ::mallocbody {} |
| 326 | if {[info exists ::mallocopts(-tclbody)]} { |
| 327 | append ::mallocbody "$::mallocopts(-tclbody)\n" |
| 328 | } |
| 329 | if {[info exists ::mallocopts(-sqlbody)]} { |
| 330 | append ::mallocbody "db eval {$::mallocopts(-sqlbody)}" |
| 331 | } |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 332 | |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 333 | # The following block sets local variables as follows: |
| 334 | # |
| 335 | # isFail - True if an error (any error) was reported by sqlite. |
| 336 | # nFail - The total number of simulated malloc() failures. |
| 337 | # nBenign - The number of benign simulated malloc() failures. |
| 338 | # |
| 339 | set isFail [catch $::mallocbody msg] |
| 340 | set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign] |
drh | 643167f | 2008-01-22 21:30:53 +0000 | [diff] [blame] | 341 | # puts -nonewline " (isFail=$isFail nFail=$nFail nBenign=$nBenign) " |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 342 | |
| 343 | # If one or more mallocs failed, run this loop body again. |
| 344 | # |
| 345 | set go [expr {$nFail>0}] |
| 346 | |
| 347 | if {($nFail-$nBenign)==0} { |
| 348 | if {$isFail} { |
| 349 | set v2 $msg |
| 350 | } else { |
| 351 | set isFail 1 |
| 352 | set v2 1 |
| 353 | } |
| 354 | } elseif {!$isFail} { |
| 355 | set v2 $msg |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 356 | } elseif { |
| 357 | [info command db]=="" || |
| 358 | [db errorcode]==7 || |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 359 | $msg=="out of memory" |
| 360 | } { |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 361 | set v2 1 |
| 362 | } else { |
| 363 | set v2 $msg |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 364 | puts [db errorcode] |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 365 | } |
| 366 | lappend isFail $v2 |
| 367 | } {1 1} |
| 368 | |
| 369 | if {[info exists ::mallocopts(-cleanup)]} { |
| 370 | catch [list uplevel #0 $::mallocopts(-cleanup)] msg |
| 371 | } |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | unset ::mallocopts |
danielk1977 | 7751940 | 2007-08-30 11:48:31 +0000 | [diff] [blame] | 375 | sqlite3_memdebug_fail -1 |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 376 | } |
dan | ef37802 | 2010-05-04 11:06:03 +0000 | [diff] [blame] | 377 | |
| 378 | |
| 379 | #------------------------------------------------------------------------- |
| 380 | # This proc is used to test a single SELECT statement. Parameter $name is |
| 381 | # passed a name for the test case (i.e. "fts3_malloc-1.4.1") and parameter |
| 382 | # $sql is passed the text of the SELECT statement. Parameter $result is |
| 383 | # set to the expected output if the SELECT statement is successfully |
| 384 | # executed using [db eval]. |
| 385 | # |
| 386 | # Example: |
| 387 | # |
| 388 | # do_select_test testcase-1.1 "SELECT 1+1, 1+2" {1 2} |
| 389 | # |
| 390 | # If global variable DO_MALLOC_TEST is set to a non-zero value, or if |
| 391 | # it is not defined at all, then OOM testing is performed on the SELECT |
| 392 | # statement. Each OOM test case is said to pass if either (a) executing |
| 393 | # the SELECT statement succeeds and the results match those specified |
| 394 | # by parameter $result, or (b) TCL throws an "out of memory" error. |
| 395 | # |
| 396 | # If DO_MALLOC_TEST is defined and set to zero, then the SELECT statement |
| 397 | # is executed just once. In this case the test case passes if the results |
| 398 | # match the expected results passed via parameter $result. |
| 399 | # |
| 400 | proc do_select_test {name sql result} { |
| 401 | uplevel [list doPassiveTest 0 $name $sql [list 0 $result]] |
| 402 | } |
| 403 | |
| 404 | proc do_restart_select_test {name sql result} { |
| 405 | uplevel [list doPassiveTest 1 $name $sql [list 0 $result]] |
| 406 | } |
| 407 | |
| 408 | proc do_error_test {name sql error} { |
| 409 | uplevel [list doPassiveTest 0 $name $sql [list 1 $error]] |
| 410 | } |
| 411 | |
| 412 | proc doPassiveTest {isRestart name sql catchres} { |
| 413 | if {![info exists ::DO_MALLOC_TEST]} { set ::DO_MALLOC_TEST 1 } |
| 414 | |
| 415 | switch $::DO_MALLOC_TEST { |
| 416 | 0 { # No malloc failures. |
| 417 | do_test $name [list set {} [uplevel [list catchsql $sql]]] $catchres |
| 418 | return |
| 419 | } |
| 420 | 1 { # Simulate transient failures. |
| 421 | set nRepeat 1 |
| 422 | set zName "transient" |
| 423 | set nStartLimit 100000 |
| 424 | set nBackup 1 |
| 425 | } |
| 426 | 2 { # Simulate persistent failures. |
| 427 | set nRepeat 1 |
| 428 | set zName "persistent" |
| 429 | set nStartLimit 100000 |
| 430 | set nBackup 1 |
| 431 | } |
| 432 | 3 { # Simulate transient failures with extra brute force. |
| 433 | set nRepeat 100000 |
| 434 | set zName "ridiculous" |
| 435 | set nStartLimit 1 |
| 436 | set nBackup 10 |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | # The set of acceptable results from running [catchsql $sql]. |
| 441 | # |
| 442 | set answers [list {1 {out of memory}} $catchres] |
| 443 | set str [join $answers " OR "] |
| 444 | |
| 445 | set nFail 1 |
| 446 | for {set iLimit $nStartLimit} {$nFail} {incr iLimit} { |
| 447 | for {set iFail 1} {$nFail && $iFail<=$iLimit} {incr iFail} { |
| 448 | for {set iTest 0} {$iTest<$nBackup && ($iFail-$iTest)>0} {incr iTest} { |
| 449 | |
| 450 | if {$isRestart} { sqlite3 db test.db } |
| 451 | |
| 452 | sqlite3_memdebug_fail [expr $iFail-$iTest] -repeat $nRepeat |
| 453 | set res [uplevel [list catchsql $sql]] |
| 454 | if {[lsearch -exact $answers $res]>=0} { set res $str } |
| 455 | set testname "$name.$zName.$iFail" |
| 456 | do_test "$name.$zName.$iLimit.$iFail" [list set {} $res] $str |
| 457 | |
| 458 | set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign] |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | |
| 465 | #------------------------------------------------------------------------- |
| 466 | # Test a single write to the database. In this case a "write" is a |
| 467 | # DELETE, UPDATE or INSERT statement. |
| 468 | # |
| 469 | # If OOM testing is performed, there are several acceptable outcomes: |
| 470 | # |
| 471 | # 1) The write succeeds. No error is returned. |
| 472 | # |
| 473 | # 2) An "out of memory" exception is thrown and: |
| 474 | # |
| 475 | # a) The statement has no effect, OR |
| 476 | # b) The current transaction is rolled back, OR |
| 477 | # c) The statement succeeds. This can only happen if the connection |
| 478 | # is in auto-commit mode (after the statement is executed, so this |
| 479 | # includes COMMIT statements). |
| 480 | # |
| 481 | # If the write operation eventually succeeds, zero is returned. If a |
| 482 | # transaction is rolled back, non-zero is returned. |
| 483 | # |
| 484 | # Parameter $name is the name to use for the test case (or test cases). |
| 485 | # The second parameter, $tbl, should be the name of the database table |
| 486 | # being modified. Parameter $sql contains the SQL statement to test. |
| 487 | # |
| 488 | proc do_write_test {name tbl sql} { |
| 489 | if {![info exists ::DO_MALLOC_TEST]} { set ::DO_MALLOC_TEST 1 } |
| 490 | |
| 491 | # Figure out an statement to get a checksum for table $tbl. |
| 492 | db eval "SELECT * FROM $tbl" V break |
| 493 | set cksumsql "SELECT md5sum([join [concat rowid $V(*)] ,]) FROM $tbl" |
| 494 | |
| 495 | # Calculate the initial table checksum. |
| 496 | set cksum1 [db one $cksumsql] |
| 497 | |
| 498 | if {$::DO_MALLOC_TEST } { |
| 499 | set answers [list {1 {out of memory}} {0 {}}] |
| 500 | if {$::DO_MALLOC_TEST==1} { |
| 501 | set modes {100000 transient} |
| 502 | } else { |
| 503 | set modes {1 persistent} |
| 504 | } |
| 505 | } else { |
| 506 | set answers [list {0 {}}] |
| 507 | set modes [list 0 nofail] |
| 508 | } |
| 509 | set str [join $answers " OR "] |
| 510 | |
| 511 | foreach {nRepeat zName} $modes { |
| 512 | for {set iFail 1} 1 {incr iFail} { |
| 513 | if {$::DO_MALLOC_TEST} {sqlite3_memdebug_fail $iFail -repeat $nRepeat} |
| 514 | |
| 515 | set res [uplevel [list catchsql $sql]] |
| 516 | set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign] |
| 517 | if {$nFail==0} { |
| 518 | do_test $name.$zName.$iFail [list set {} $res] {0 {}} |
| 519 | return |
| 520 | } else { |
| 521 | if {[lsearch $answers $res]>=0} { |
| 522 | set res $str |
| 523 | } |
| 524 | do_test $name.$zName.$iFail [list set {} $res] $str |
| 525 | set cksum2 [db one $cksumsql] |
| 526 | if {$cksum1 != $cksum2} return |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | } |