Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(621)

Side by Side Diff: third_party/sqlite/src/test/malloc_common.tcl

Issue 5626002: Update sqlite to 3.7.3. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/third_party/sqlite/src
Patch Set: Remove misc change. Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/sqlite/src/test/mallocI.test ('k') | third_party/sqlite/src/test/memleak.test » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # 2007 May 05 1 # 2007 May 05
2 # 2 #
3 # The author disclaims copyright to this source code. In place of 3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing: 4 # a legal notice, here is a blessing:
5 # 5 #
6 # May you do good and not evil. 6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others. 7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give. 8 # May you share freely, never taking more than you give.
9 # 9 #
10 #*********************************************************************** 10 #***********************************************************************
11 # 11 #
12 # This file contains common code used by many different malloc tests 12 # This file contains common code used by many different malloc tests
13 # within the test suite. 13 # within the test suite.
14 # 14 #
15 # $Id: malloc_common.tcl,v 1.22 2008/09/23 16:41:30 danielk1977 Exp $ 15 # $Id: malloc_common.tcl,v 1.22 2008/09/23 16:41:30 danielk1977 Exp $
16 16
17 # If we did not compile with malloc testing enabled, then do nothing. 17 # If we did not compile with malloc testing enabled, then do nothing.
18 # 18 #
19 ifcapable builtin_test { 19 ifcapable builtin_test {
20 set MEMDEBUG 1 20 set MEMDEBUG 1
21 } else { 21 } else {
22 set MEMDEBUG 0 22 set MEMDEBUG 0
23 return 0 23 return 0
24 } 24 }
25 25
26 # Transient and persistent OOM errors:
27 #
28 set FAULTSIM(oom-transient) [list \
29 -injectstart {oom_injectstart 0} \
30 -injectstop oom_injectstop \
31 -injecterrlist {{1 {out of memory}}} \
32 ]
33 set FAULTSIM(oom-persistent) [list \
34 -injectstart {oom_injectstart 1000000} \
35 -injectstop oom_injectstop \
36 -injecterrlist {{1 {out of memory}}} \
37 ]
38
39 # Transient and persistent IO errors:
40 #
41 set FAULTSIM(ioerr-transient) [list \
42 -injectstart {ioerr_injectstart 0} \
43 -injectstop ioerr_injectstop \
44 -injecterrlist {{1 {disk I/O error}}} \
45 ]
46 set FAULTSIM(ioerr-persistent) [list \
47 -injectstart {ioerr_injectstart 1} \
48 -injectstop ioerr_injectstop \
49 -injecterrlist {{1 {disk I/O error}}} \
50 ]
51
52 # SQLITE_FULL errors (always persistent):
53 #
54 set FAULTSIM(full) [list \
55 -injectinstall fullerr_injectinstall \
56 -injectstart fullerr_injectstart \
57 -injectstop fullerr_injectstop \
58 -injecterrlist {{1 {database or disk is full}}} \
59 -injectuninstall fullerr_injectuninstall \
60 ]
61
62 # Transient and persistent SHM errors:
63 #
64 set FAULTSIM(shmerr-transient) [list \
65 -injectinstall shmerr_injectinstall \
66 -injectstart {shmerr_injectstart 0} \
67 -injectstop shmerr_injectstop \
68 -injecterrlist {{1 {disk I/O error}}} \
69 -injectuninstall shmerr_injectuninstall \
70 ]
71 set FAULTSIM(shmerr-persistent) [list \
72 -injectinstall shmerr_injectinstall \
73 -injectstart {shmerr_injectstart 1} \
74 -injectstop shmerr_injectstop \
75 -injecterrlist {{1 {disk I/O error}}} \
76 -injectuninstall shmerr_injectuninstall \
77 ]
78
79 # Transient and persistent CANTOPEN errors:
80 #
81 set FAULTSIM(cantopen-transient) [list \
82 -injectinstall cantopen_injectinstall \
83 -injectstart {cantopen_injectstart 0} \
84 -injectstop cantopen_injectstop \
85 -injecterrlist {{1 {unable to open database file}}} \
86 -injectuninstall cantopen_injectuninstall \
87 ]
88 set FAULTSIM(cantopen-persistent) [list \
89 -injectinstall cantopen_injectinstall \
90 -injectstart {cantopen_injectstart 1} \
91 -injectstop cantopen_injectstop \
92 -injecterrlist {{1 {unable to open database file}}} \
93 -injectuninstall cantopen_injectuninstall \
94 ]
95
96
97
98 #--------------------------------------------------------------------------
99 # Usage do_faultsim_test NAME ?OPTIONS...?
100 #
101 # -faults List of fault types to simulate.
102 #
103 # -prep Script to execute before -body.
104 #
105 # -body Script to execute (with fault injection).
106 #
107 # -test Script to execute after -body.
108 #
109 proc do_faultsim_test {name args} {
110 global FAULTSIM
111
112 set DEFAULT(-faults) [array names FAULTSIM]
113 set DEFAULT(-prep) ""
114 set DEFAULT(-body) ""
115 set DEFAULT(-test) ""
116
117 array set O [array get DEFAULT]
118 array set O $args
119 foreach o [array names O] {
120 if {[info exists DEFAULT($o)]==0} { error "unknown option: $o" }
121 }
122
123 set faultlist [list]
124 foreach f $O(-faults) {
125 set flist [array names FAULTSIM $f]
126 if {[llength $flist]==0} { error "unknown fault: $f" }
127 set faultlist [concat $faultlist $flist]
128 }
129
130 set testspec [list -prep $O(-prep) -body $O(-body) -test $O(-test)]
131 foreach f [lsort -unique $faultlist] {
132 eval do_one_faultsim_test "$name-$f" $FAULTSIM($f) $testspec
133 }
134 }
135
136 #-------------------------------------------------------------------------
137 # Procedures to save and restore the current file-system state:
138 #
139 # faultsim_save
140 # faultsim_restore
141 # faultsim_save_and_close
142 # faultsim_restore_and_reopen
143 # faultsim_delete_and_reopen
144 #
145 proc faultsim_save {} {
146 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
147 foreach f [glob -nocomplain test.db*] {
148 set f2 "sv_$f"
149 file copy -force $f $f2
150 }
151 }
152 proc faultsim_save_and_close {} {
153 faultsim_save
154 catch { db close }
155 return ""
156 }
157 proc faultsim_restore {} {
158 foreach f [glob -nocomplain test.db*] { forcedelete $f }
159 foreach f2 [glob -nocomplain sv_test.db*] {
160 set f [string range $f2 3 end]
161 file copy -force $f2 $f
162 }
163 }
164 proc faultsim_restore_and_reopen {{dbfile test.db}} {
165 catch { db close }
166 faultsim_restore
167 sqlite3 db $dbfile
168 sqlite3_extended_result_codes db 1
169 sqlite3_db_config_lookaside db 0 0 0
170 }
171
172 proc faultsim_integrity_check {{db db}} {
173 set ic [$db eval { PRAGMA integrity_check }]
174 if {$ic != "ok"} { error "Integrity check: $ic" }
175 }
176
177 proc faultsim_delete_and_reopen {{file test.db}} {
178 catch { db close }
179 foreach f [glob -nocomplain test.db*] { file delete -force $f }
180 sqlite3 db $file
181 }
182
183
184 # The following procs are used as [do_one_faultsim_test] callbacks when
185 # injecting OOM faults into test cases.
186 #
187 proc oom_injectstart {nRepeat iFail} {
188 sqlite3_memdebug_fail [expr $iFail-1] -repeat $nRepeat
189 }
190 proc oom_injectstop {} {
191 sqlite3_memdebug_fail -1
192 }
193
194 # The following procs are used as [do_one_faultsim_test] callbacks when
195 # injecting IO error faults into test cases.
196 #
197 proc ioerr_injectstart {persist iFail} {
198 set ::sqlite_io_error_persist $persist
199 set ::sqlite_io_error_pending $iFail
200 }
201 proc ioerr_injectstop {} {
202 set sv $::sqlite_io_error_hit
203 set ::sqlite_io_error_persist 0
204 set ::sqlite_io_error_pending 0
205 set ::sqlite_io_error_hardhit 0
206 set ::sqlite_io_error_hit 0
207 set ::sqlite_io_error_pending 0
208 return $sv
209 }
210
211 # The following procs are used as [do_one_faultsim_test] callbacks when
212 # injecting shared-memory related error faults into test cases.
213 #
214 proc shmerr_injectinstall {} {
215 testvfs shmfault -default true
216 shmfault filter {xShmOpen xShmMap xShmLock}
217 }
218 proc shmerr_injectuninstall {} {
219 catch {db close}
220 catch {db2 close}
221 shmfault delete
222 }
223 proc shmerr_injectstart {persist iFail} {
224 shmfault ioerr $iFail $persist
225 }
226 proc shmerr_injectstop {} {
227 shmfault ioerr
228 }
229
230 # The following procs are used as [do_one_faultsim_test] callbacks when
231 # injecting SQLITE_FULL error faults into test cases.
232 #
233 proc fullerr_injectinstall {} {
234 testvfs shmfault -default true
235 }
236 proc fullerr_injectuninstall {} {
237 catch {db close}
238 catch {db2 close}
239 shmfault delete
240 }
241 proc fullerr_injectstart {iFail} {
242 shmfault full $iFail 1
243 }
244 proc fullerr_injectstop {} {
245 shmfault full
246 }
247
248 # The following procs are used as [do_one_faultsim_test] callbacks when
249 # injecting SQLITE_CANTOPEN error faults into test cases.
250 #
251 proc cantopen_injectinstall {} {
252 testvfs shmfault -default true
253 }
254 proc cantopen_injectuninstall {} {
255 catch {db close}
256 catch {db2 close}
257 shmfault delete
258 }
259 proc cantopen_injectstart {persist iFail} {
260 shmfault cantopen $iFail $persist
261 }
262 proc cantopen_injectstop {} {
263 shmfault cantopen
264 }
265
266 # This command is not called directly. It is used by the
267 # [faultsim_test_result] command created by [do_faultsim_test] and used
268 # by -test scripts.
269 #
270 proc faultsim_test_result_int {args} {
271 upvar testrc testrc testresult testresult testnfail testnfail
272 set t [list $testrc $testresult]
273 set r $args
274 if { ($testnfail==0 && $t != [lindex $r 0]) || [lsearch $r $t]<0 } {
275 error "nfail=$testnfail rc=$testrc result=$testresult"
276 }
277 }
278
279 #--------------------------------------------------------------------------
280 # Usage do_one_faultsim_test NAME ?OPTIONS...?
281 #
282 # The first argument, <test number>, is used as a prefix of the test names
283 # taken by tests executed by this command. Options are as follows. All
284 # options take a single argument.
285 #
286 # -injectstart Script to enable fault-injection.
287 #
288 # -injectstop Script to disable fault-injection.
289 #
290 # -injecterrlist List of generally acceptable test results (i.e. error
291 # messages). Example: [list {1 {out of memory}}]
292 #
293 # -injectinstall
294 #
295 # -injectuninstall
296 #
297 # -prep Script to execute before -body.
298 #
299 # -body Script to execute (with fault injection).
300 #
301 # -test Script to execute after -body.
302 #
303 proc do_one_faultsim_test {testname args} {
304
305 set DEFAULT(-injectstart) "expr"
306 set DEFAULT(-injectstop) "expr 0"
307 set DEFAULT(-injecterrlist) [list]
308 set DEFAULT(-injectinstall) ""
309 set DEFAULT(-injectuninstall) ""
310 set DEFAULT(-prep) ""
311 set DEFAULT(-body) ""
312 set DEFAULT(-test) ""
313
314 array set O [array get DEFAULT]
315 array set O $args
316 foreach o [array names O] {
317 if {[info exists DEFAULT($o)]==0} { error "unknown option: $o" }
318 }
319
320 proc faultsim_test_proc {testrc testresult testnfail} $O(-test)
321 proc faultsim_test_result {args} "
322 uplevel faultsim_test_result_int \$args [list $O(-injecterrlist)]
323 "
324
325 eval $O(-injectinstall)
326
327 set stop 0
328 for {set iFail 1} {!$stop} {incr iFail} {
329
330 # Evaluate the -prep script.
331 #
332 eval $O(-prep)
333
334 # Start the fault-injection. Run the -body script. Stop the fault
335 # injection. Local var $nfail is set to the total number of faults
336 # injected into the system this trial.
337 #
338 eval $O(-injectstart) $iFail
339 set rc [catch $O(-body) res]
340 set nfail [eval $O(-injectstop)]
341
342 # Run the -test script. If it throws no error, consider this trial
343 # sucessful. If it does throw an error, cause a [do_test] test to
344 # fail (and print out the unexpected exception thrown by the -test
345 # script at the same time).
346 #
347 set rc [catch [list faultsim_test_proc $rc $res $nfail] res]
348 if {$rc == 0} {set res ok}
349 do_test $testname.$iFail [list list $rc $res] {0 ok}
350
351 # If no faults where injected this trial, don't bother running
352 # any more. This test is finished.
353 #
354 if {$nfail==0} { set stop 1 }
355 }
356
357 eval $O(-injectuninstall)
358 }
359
26 # Usage: do_malloc_test <test number> <options...> 360 # Usage: do_malloc_test <test number> <options...>
27 # 361 #
28 # The first argument, <test number>, is an integer used to name the 362 # The first argument, <test number>, is an integer used to name the
29 # tests executed by this proc. Options are as follows: 363 # tests executed by this proc. Options are as follows:
30 # 364 #
31 # -tclprep TCL script to run to prepare test. 365 # -tclprep TCL script to run to prepare test.
32 # -sqlprep SQL script to run to prepare test. 366 # -sqlprep SQL script to run to prepare test.
33 # -tclbody TCL script to run with malloc failure simulation. 367 # -tclbody TCL script to run with malloc failure simulation.
34 # -sqlbody TCL script to run with malloc failure simulation. 368 # -sqlbody TCL script to run with malloc failure simulation.
35 # -cleanup TCL script to run after the test. 369 # -cleanup TCL script to run after the test.
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 restore_prng_state 414 restore_prng_state
81 foreach file [glob -nocomplain test.db-mj*] {file delete -force $file} 415 foreach file [glob -nocomplain test.db-mj*] {file delete -force $file}
82 416
83 do_test ${tn}.${zRepeat}.${::n} { 417 do_test ${tn}.${zRepeat}.${::n} {
84 418
85 # Remove all traces of database files test.db and test2.db 419 # Remove all traces of database files test.db and test2.db
86 # from the file-system. Then open (empty database) "test.db" 420 # from the file-system. Then open (empty database) "test.db"
87 # with the handle [db]. 421 # with the handle [db].
88 # 422 #
89 catch {db close} 423 catch {db close}
90 catch {file delete -force test.db} 424 catch {db2 close}
91 catch {file delete -force test.db-journal} 425 forcedelete test.db
92 catch {file delete -force test2.db} 426 forcedelete test.db-journal
93 catch {file delete -force test2.db-journal} 427 forcedelete test.db-wal
428 forcedelete test2.db
429 forcedelete test2.db-journal
430 forcedelete test2.db-wal
94 if {[info exists ::mallocopts(-testdb)]} { 431 if {[info exists ::mallocopts(-testdb)]} {
95 file copy $::mallocopts(-testdb) test.db 432 file copy $::mallocopts(-testdb) test.db
96 } 433 }
97 catch { sqlite3 db test.db } 434 catch { sqlite3 db test.db }
98 if {[info commands db] ne ""} { 435 if {[info commands db] ne ""} {
99 sqlite3_extended_result_codes db 1 436 sqlite3_extended_result_codes db 1
100 } 437 }
101 sqlite3_db_config_lookaside db 0 0 0 438 sqlite3_db_config_lookaside db 0 0 0
102 439
103 # Execute any -tclprep and -sqlprep scripts. 440 # Execute any -tclprep and -sqlprep scripts.
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 } {1 1} 495 } {1 1}
159 496
160 if {[info exists ::mallocopts(-cleanup)]} { 497 if {[info exists ::mallocopts(-cleanup)]} {
161 catch [list uplevel #0 $::mallocopts(-cleanup)] msg 498 catch [list uplevel #0 $::mallocopts(-cleanup)] msg
162 } 499 }
163 } 500 }
164 } 501 }
165 unset ::mallocopts 502 unset ::mallocopts
166 sqlite3_memdebug_fail -1 503 sqlite3_memdebug_fail -1
167 } 504 }
505
506
507 #-------------------------------------------------------------------------
508 # This proc is used to test a single SELECT statement. Parameter $name is
509 # passed a name for the test case (i.e. "fts3_malloc-1.4.1") and parameter
510 # $sql is passed the text of the SELECT statement. Parameter $result is
511 # set to the expected output if the SELECT statement is successfully
512 # executed using [db eval].
513 #
514 # Example:
515 #
516 # do_select_test testcase-1.1 "SELECT 1+1, 1+2" {1 2}
517 #
518 # If global variable DO_MALLOC_TEST is set to a non-zero value, or if
519 # it is not defined at all, then OOM testing is performed on the SELECT
520 # statement. Each OOM test case is said to pass if either (a) executing
521 # the SELECT statement succeeds and the results match those specified
522 # by parameter $result, or (b) TCL throws an "out of memory" error.
523 #
524 # If DO_MALLOC_TEST is defined and set to zero, then the SELECT statement
525 # is executed just once. In this case the test case passes if the results
526 # match the expected results passed via parameter $result.
527 #
528 proc do_select_test {name sql result} {
529 uplevel [list doPassiveTest 0 $name $sql [list 0 $result]]
530 }
531
532 proc do_restart_select_test {name sql result} {
533 uplevel [list doPassiveTest 1 $name $sql [list 0 $result]]
534 }
535
536 proc do_error_test {name sql error} {
537 uplevel [list doPassiveTest 0 $name $sql [list 1 $error]]
538 }
539
540 proc doPassiveTest {isRestart name sql catchres} {
541 if {![info exists ::DO_MALLOC_TEST]} { set ::DO_MALLOC_TEST 1 }
542
543 switch $::DO_MALLOC_TEST {
544 0 { # No malloc failures.
545 do_test $name [list set {} [uplevel [list catchsql $sql]]] $catchres
546 return
547 }
548 1 { # Simulate transient failures.
549 set nRepeat 1
550 set zName "transient"
551 set nStartLimit 100000
552 set nBackup 1
553 }
554 2 { # Simulate persistent failures.
555 set nRepeat 1
556 set zName "persistent"
557 set nStartLimit 100000
558 set nBackup 1
559 }
560 3 { # Simulate transient failures with extra brute force.
561 set nRepeat 100000
562 set zName "ridiculous"
563 set nStartLimit 1
564 set nBackup 10
565 }
566 }
567
568 # The set of acceptable results from running [catchsql $sql].
569 #
570 set answers [list {1 {out of memory}} $catchres]
571 set str [join $answers " OR "]
572
573 set nFail 1
574 for {set iLimit $nStartLimit} {$nFail} {incr iLimit} {
575 for {set iFail 1} {$nFail && $iFail<=$iLimit} {incr iFail} {
576 for {set iTest 0} {$iTest<$nBackup && ($iFail-$iTest)>0} {incr iTest} {
577
578 if {$isRestart} { sqlite3 db test.db }
579
580 sqlite3_memdebug_fail [expr $iFail-$iTest] -repeat $nRepeat
581 set res [uplevel [list catchsql $sql]]
582 if {[lsearch -exact $answers $res]>=0} { set res $str }
583 set testname "$name.$zName.$iFail"
584 do_test "$name.$zName.$iLimit.$iFail" [list set {} $res] $str
585
586 set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign]
587 }
588 }
589 }
590 }
591
592
593 #-------------------------------------------------------------------------
594 # Test a single write to the database. In this case a "write" is a
595 # DELETE, UPDATE or INSERT statement.
596 #
597 # If OOM testing is performed, there are several acceptable outcomes:
598 #
599 # 1) The write succeeds. No error is returned.
600 #
601 # 2) An "out of memory" exception is thrown and:
602 #
603 # a) The statement has no effect, OR
604 # b) The current transaction is rolled back, OR
605 # c) The statement succeeds. This can only happen if the connection
606 # is in auto-commit mode (after the statement is executed, so this
607 # includes COMMIT statements).
608 #
609 # If the write operation eventually succeeds, zero is returned. If a
610 # transaction is rolled back, non-zero is returned.
611 #
612 # Parameter $name is the name to use for the test case (or test cases).
613 # The second parameter, $tbl, should be the name of the database table
614 # being modified. Parameter $sql contains the SQL statement to test.
615 #
616 proc do_write_test {name tbl sql} {
617 if {![info exists ::DO_MALLOC_TEST]} { set ::DO_MALLOC_TEST 1 }
618
619 # Figure out an statement to get a checksum for table $tbl.
620 db eval "SELECT * FROM $tbl" V break
621 set cksumsql "SELECT md5sum([join [concat rowid $V(*)] ,]) FROM $tbl"
622
623 # Calculate the initial table checksum.
624 set cksum1 [db one $cksumsql]
625
626 if {$::DO_MALLOC_TEST } {
627 set answers [list {1 {out of memory}} {0 {}}]
628 if {$::DO_MALLOC_TEST==1} {
629 set modes {100000 persistent}
630 } else {
631 set modes {1 transient}
632 }
633 } else {
634 set answers [list {0 {}}]
635 set modes [list 0 nofail]
636 }
637 set str [join $answers " OR "]
638
639 foreach {nRepeat zName} $modes {
640 for {set iFail 1} 1 {incr iFail} {
641 if {$::DO_MALLOC_TEST} {sqlite3_memdebug_fail $iFail -repeat $nRepeat}
642
643 set res [uplevel [list catchsql $sql]]
644 set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign]
645 if {$nFail==0} {
646 do_test $name.$zName.$iFail [list set {} $res] {0 {}}
647 return
648 } else {
649 if {[lsearch $answers $res]>=0} {
650 set res $str
651 }
652 do_test $name.$zName.$iFail [list set {} $res] $str
653 set cksum2 [db one $cksumsql]
654 if {$cksum1 != $cksum2} return
655 }
656 }
657 }
658 }
OLDNEW
« no previous file with comments | « third_party/sqlite/src/test/mallocI.test ('k') | third_party/sqlite/src/test/memleak.test » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698