OLD | NEW |
1 # 2005 November 30 | 1 # 2005 November 30 |
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 tests to ensure that the library handles malloc() failures | 12 # This file contains tests to ensure that the library handles malloc() failures |
13 # correctly. The emphasis of these tests are the _prepare(), _step() and | 13 # correctly. The emphasis of these tests are the _prepare(), _step() and |
14 # _finalize() calls. | 14 # _finalize() calls. |
15 # | 15 # |
16 # $Id: malloc3.test,v 1.24 2008/10/14 15:54:08 drh Exp $ | 16 # $Id: malloc3.test,v 1.24 2008/10/14 15:54:08 drh Exp $ |
17 | 17 |
18 set testdir [file dirname $argv0] | 18 set testdir [file dirname $argv0] |
19 source $testdir/tester.tcl | 19 source $testdir/tester.tcl |
20 source $testdir/malloc_common.tcl | 20 source $testdir/malloc_common.tcl |
21 | 21 |
22 # Only run these tests if memory debugging is turned on. | 22 # Only run these tests if memory debugging is turned on. |
23 # | 23 # |
24 if {!$MEMDEBUG} { | 24 if {!$MEMDEBUG} { |
25 puts "Skipping malloc3 tests: not compiled with -DSQLITE_MEMDEBUG..." | 25 puts "Skipping malloc3 tests: not compiled with -DSQLITE_MEMDEBUG..." |
26 finish_test | 26 finish_test |
27 return | 27 return |
28 } | 28 } |
29 | 29 |
| 30 |
| 31 # Do not run these tests with an in-memory journal. |
| 32 # |
| 33 # In the pager layer, if an IO or OOM error occurs during a ROLLBACK, or |
| 34 # when flushing a page to disk due to cache-stress, the pager enters an |
| 35 # "error state". The only way out of the error state is to unlock the |
| 36 # database file and end the transaction, leaving whatever journal and |
| 37 # database files happen to be on disk in place. The next time the current |
| 38 # (or any other) connection opens a read transaction, hot-journal rollback |
| 39 # is performed if necessary. |
| 40 # |
| 41 # Of course, this doesn't work with an in-memory journal. |
| 42 # |
| 43 if {[permutation]=="inmemory_journal"} { |
| 44 finish_test |
| 45 return |
| 46 } |
| 47 |
30 #-------------------------------------------------------------------------- | 48 #-------------------------------------------------------------------------- |
31 # NOTES ON RECOVERING FROM A MALLOC FAILURE | 49 # NOTES ON RECOVERING FROM A MALLOC FAILURE |
32 # | 50 # |
33 # The tests in this file test the behaviours described in the following | 51 # The tests in this file test the behaviours described in the following |
34 # paragraphs. These tests test the behaviour of the system when malloc() fails | 52 # paragraphs. These tests test the behaviour of the system when malloc() fails |
35 # inside of a call to _prepare(), _step(), _finalize() or _reset(). The | 53 # inside of a call to _prepare(), _step(), _finalize() or _reset(). The |
36 # handling of malloc() failures within ancillary procedures is tested | 54 # handling of malloc() failures within ancillary procedures is tested |
37 # elsewhere. | 55 # elsewhere. |
38 # | 56 # |
39 # Overview: | 57 # Overview: |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 # * If a PREP block starts a transaction, it must finish it. | 158 # * If a PREP block starts a transaction, it must finish it. |
141 # * A PREP block may not close a transaction it did not start. | 159 # * A PREP block may not close a transaction it did not start. |
142 # | 160 # |
143 #-------------------------------------------------------------------------- | 161 #-------------------------------------------------------------------------- |
144 | 162 |
145 | 163 |
146 # These procs are used to build up a "program" in global variable | 164 # These procs are used to build up a "program" in global variable |
147 # ::run_test_script. At the end of this file, the proc [run_test] is used | 165 # ::run_test_script. At the end of this file, the proc [run_test] is used |
148 # to execute the program (and all test cases contained therein). | 166 # to execute the program (and all test cases contained therein). |
149 # | 167 # |
| 168 set ::run_test_sql_id 0 |
150 set ::run_test_script [list] | 169 set ::run_test_script [list] |
151 proc TEST {id t} {lappend ::run_test_script -test [list $id $t]} | 170 proc TEST {id t} {lappend ::run_test_script -test [list $id $t]} |
152 proc PREP {p} {lappend ::run_test_script -prep [string trim $p]} | 171 proc PREP {p} {lappend ::run_test_script -prep [string trim $p]} |
153 proc DEBUG {s} {lappend ::run_test_script -debug $s} | 172 proc DEBUG {s} {lappend ::run_test_script -debug $s} |
154 | 173 |
155 # SQL -- | 174 # SQL -- |
156 # | 175 # |
157 # SQL ?-norollback? <sql-text> | 176 # SQL ?-norollback? <sql-text> |
158 # | 177 # |
159 # Add an 'SQL' primitive to the program (see notes above). If the -norollback | 178 # Add an 'SQL' primitive to the program (see notes above). If the -norollback |
160 # switch is present, then the statement is not allowed to automatically roll | 179 # switch is present, then the statement is not allowed to automatically roll |
161 # back any active transaction if malloc() fails. It must rollback the statement | 180 # back any active transaction if malloc() fails. It must rollback the statement |
162 # transaction only. | 181 # transaction only. |
163 # | 182 # |
164 proc SQL {a1 {a2 ""}} { | 183 proc SQL {a1 {a2 ""}} { |
165 # An SQL primitive parameter is a list of two elements, a boolean value | 184 # An SQL primitive parameter is a list of three elements, an id, a boolean |
166 # indicating if the statement may cause transaction rollback when malloc() | 185 # value indicating if the statement may cause transaction rollback when |
167 # fails, and the sql statement itself. | 186 # malloc() fails, and the sql statement itself. |
| 187 set id [incr ::run_test_sql_id] |
168 if {$a2 == ""} { | 188 if {$a2 == ""} { |
169 lappend ::run_test_script -sql [list true [string trim $a1]] | 189 lappend ::run_test_script -sql [list $id true [string trim $a1]] |
170 } else { | 190 } else { |
171 lappend ::run_test_script -sql [list false [string trim $a2]] | 191 lappend ::run_test_script -sql [list $id false [string trim $a2]] |
172 } | 192 } |
173 } | 193 } |
174 | 194 |
175 # TEST_AUTOCOMMIT -- | 195 # TEST_AUTOCOMMIT -- |
176 # | 196 # |
177 # A shorthand test to see if a transaction is active or not. The first | 197 # A shorthand test to see if a transaction is active or not. The first |
178 # argument - $id - is the integer number of the test case. The second | 198 # argument - $id - is the integer number of the test case. The second |
179 # argument is either 1 or 0, the expected value of the auto-commit flag. | 199 # argument is either 1 or 0, the expected value of the auto-commit flag. |
180 # | 200 # |
181 proc TEST_AUTOCOMMIT {id a} { | 201 proc TEST_AUTOCOMMIT {id a} { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 execsql { | 271 execsql { |
252 SELECT name, tbl_name FROM sqlite_master ORDER BY name; | 272 SELECT name, tbl_name FROM sqlite_master ORDER BY name; |
253 SELECT * FROM abc; | 273 SELECT * FROM abc; |
254 } | 274 } |
255 } {abc abc abc_i abc abc_t abc abc_v abc_v 1 2 3} | 275 } {abc abc abc_i abc abc_t abc abc_v abc_v 1 2 3} |
256 } | 276 } |
257 | 277 |
258 set sql { | 278 set sql { |
259 BEGIN;DELETE FROM abc; | 279 BEGIN;DELETE FROM abc; |
260 } | 280 } |
261 for {set i 1} {$i < 15} {incr i} { | 281 for {set i 1} {$i < 100} {incr i} { |
262 set a $i | 282 set a $i |
263 set b "String value $i" | 283 set b "String value $i" |
264 set c [string repeat X $i] | 284 set c [string repeat X $i] |
265 append sql "INSERT INTO abc VALUES ($a, '$b', '$c');" | 285 append sql "INSERT INTO abc VALUES ($a, '$b', '$c');" |
266 } | 286 } |
267 append sql {COMMIT;} | 287 append sql {COMMIT;} |
268 PREP $sql | 288 PREP $sql |
269 | 289 |
270 SQL { | 290 SQL { |
271 DELETE FROM abc WHERE oid IN (SELECT oid FROM abc ORDER BY random() LIMIT 5); | 291 DELETE FROM abc WHERE oid IN (SELECT oid FROM abc ORDER BY random() LIMIT 5); |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 TEST 29 { | 476 TEST 29 { |
457 do_test $testid { | 477 do_test $testid { |
458 execsql { | 478 execsql { |
459 SELECT * FROM v1 WHERE d = g; | 479 SELECT * FROM v1 WHERE d = g; |
460 } | 480 } |
461 } {a b c a b c 1 2 3 1 2 3} | 481 } {a b c a b c 1 2 3 1 2 3} |
462 } | 482 } |
463 | 483 |
464 # Test a simple multi-file transaction | 484 # Test a simple multi-file transaction |
465 # | 485 # |
466 file delete -force test2.db | 486 forcedelete test2.db |
467 ifcapable attach { | 487 ifcapable attach { |
468 SQL {ATTACH 'test2.db' AS aux;} | 488 SQL {ATTACH 'test2.db' AS aux;} |
469 SQL {BEGIN} | 489 SQL {BEGIN} |
470 SQL {CREATE TABLE aux.tbl2(x, y, z)} | 490 SQL {CREATE TABLE aux.tbl2(x, y, z)} |
471 SQL {INSERT INTO tbl2 VALUES(1, 2, 3)} | 491 SQL {INSERT INTO tbl2 VALUES(1, 2, 3)} |
472 SQL {INSERT INTO def VALUES(4, 5, 6)} | 492 SQL {INSERT INTO def VALUES(4, 5, 6)} |
473 TEST 30 { | 493 TEST 30 { |
474 do_test $testid { | 494 do_test $testid { |
475 execsql { | 495 execsql { |
476 SELECT * FROM tbl2, def WHERE d = x; | 496 SELECT * FROM tbl2, def WHERE d = x; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 # | 542 # |
523 # End of test program declaration | 543 # End of test program declaration |
524 #-------------------------------------------------------------------------- | 544 #-------------------------------------------------------------------------- |
525 | 545 |
526 proc run_test {arglist iRepeat {pcstart 0} {iFailStart 1}} { | 546 proc run_test {arglist iRepeat {pcstart 0} {iFailStart 1}} { |
527 if {[llength $arglist] %2} { | 547 if {[llength $arglist] %2} { |
528 error "Uneven number of arguments to TEST" | 548 error "Uneven number of arguments to TEST" |
529 } | 549 } |
530 | 550 |
531 for {set i 0} {$i < $pcstart} {incr i} { | 551 for {set i 0} {$i < $pcstart} {incr i} { |
532 set k2 [lindex $arglist [expr 2 * $i]] | 552 set k2 [lindex $arglist [expr {2 * $i}]] |
533 set v2 [lindex $arglist [expr 2 * $i + 1]] | 553 set v2 [lindex $arglist [expr {2 * $i + 1}]] |
534 set ac [sqlite3_get_autocommit $::DB] ;# Auto-Commit | 554 set ac [sqlite3_get_autocommit $::DB] ;# Auto-Commit |
535 switch -- $k2 { | 555 switch -- $k2 { |
536 -sql {db eval [lindex $v2 1]} | 556 -sql {db eval [lindex $v2 2]} |
537 -prep {db eval $v2} | 557 -prep {db eval $v2} |
| 558 -debug {eval $v2} |
538 } | 559 } |
539 set nac [sqlite3_get_autocommit $::DB] ;# New Auto-Commit | 560 set nac [sqlite3_get_autocommit $::DB] ;# New Auto-Commit |
540 if {$ac && !$nac} {set begin_pc $i} | 561 if {$ac && !$nac} {set begin_pc $i} |
541 } | 562 } |
542 | 563 |
543 db rollback_hook [list incr ::rollback_hook_count] | 564 db rollback_hook [list incr ::rollback_hook_count] |
544 | 565 |
545 set iFail $iFailStart | 566 set iFail $iFailStart |
546 set pc $pcstart | 567 set pc $pcstart |
547 while {$pc*2 < [llength $arglist]} { | 568 while {$pc*2 < [llength $arglist]} { |
| 569 # Fetch the current instruction type and payload. |
| 570 set k [lindex $arglist [expr {2 * $pc}]] |
| 571 set v [lindex $arglist [expr {2 * $pc + 1}]] |
548 | 572 |
549 # Id of this iteration: | 573 # Id of this iteration: |
550 set k [lindex $arglist [expr 2 * $pc]] | |
551 set iterid "pc=$pc.iFail=$iFail$k" | 574 set iterid "pc=$pc.iFail=$iFail$k" |
552 set v [lindex $arglist [expr 2 * $pc + 1]] | |
553 | 575 |
554 switch -- $k { | 576 switch -- $k { |
555 | 577 |
556 -test { | 578 -test { |
557 foreach {id script} $v {} | 579 foreach {id script} $v {} |
| 580 set testid "malloc3-(test $id).$iterid" |
| 581 eval $script |
558 incr pc | 582 incr pc |
559 } | 583 } |
560 | 584 |
561 -sql { | 585 -sql { |
562 set ::rollback_hook_count 0 | 586 set ::rollback_hook_count 0 |
563 | 587 |
| 588 set id [lindex $v 0] |
| 589 set testid "malloc3-(integrity $id).$iterid" |
| 590 |
564 set ac [sqlite3_get_autocommit $::DB] ;# Auto-Commit | 591 set ac [sqlite3_get_autocommit $::DB] ;# Auto-Commit |
565 sqlite3_memdebug_fail $iFail -repeat 0 | 592 sqlite3_memdebug_fail $iFail -repeat 0 |
566 set rc [catch {db eval [lindex $v 1]} msg] ;# True error occurs | 593 set rc [catch {db eval [lindex $v 2]} msg] ;# True error occurs |
567 set nac [sqlite3_get_autocommit $::DB] ;# New Auto-Commit | 594 set nac [sqlite3_get_autocommit $::DB] ;# New Auto-Commit |
568 | 595 |
569 if {$rc != 0 && $nac && !$ac} { | 596 if {$rc != 0 && $nac && !$ac} { |
570 # Before [db eval] the auto-commit flag was clear. Now it | 597 # Before [db eval] the auto-commit flag was clear. Now it |
571 # is set. Since an error occured we assume this was not a | 598 # is set. Since an error occurred we assume this was not a |
572 # commit - therefore a rollback occured. Check that the | 599 # commit - therefore a rollback occurred. Check that the |
573 # rollback-hook was invoked. | 600 # rollback-hook was invoked. |
574 do_test malloc3-rollback_hook.$iterid { | 601 do_test malloc3-rollback_hook_count.$iterid { |
575 set ::rollback_hook_count | 602 set ::rollback_hook_count |
576 } {1} | 603 } {1} |
577 } | 604 } |
578 | 605 |
579 set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign] | 606 set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign] |
580 if {$rc == 0} { | 607 if {$rc == 0} { |
581 # Successful execution of sql. The number of failed malloc() | 608 # Successful execution of sql. The number of failed malloc() |
582 # calls should be equal to the number of benign failures. | 609 # calls should be equal to the number of benign failures. |
583 # Otherwise a malloc() failed and the error was not reported. | 610 # Otherwise a malloc() failed and the error was not reported. |
584 # | 611 # |
585 if {$nFail!=$nBenign} { | 612 set expr {$nFail!=$nBenign} |
586 error "Unreported malloc() failure" | 613 if {[expr $expr]} { |
| 614 error "Unreported malloc() failure, test \"$testid\", $expr" |
587 } | 615 } |
588 | 616 |
589 if {$ac && !$nac} { | 617 if {$ac && !$nac} { |
590 # Before the [db eval] the auto-commit flag was set, now it | 618 # Before the [db eval] the auto-commit flag was set, now it |
591 # is clear. We can deduce that a "BEGIN" statement has just | 619 # is clear. We can deduce that a "BEGIN" statement has just |
592 # been successfully executed. | 620 # been successfully executed. |
593 set begin_pc $pc | 621 set begin_pc $pc |
594 } | 622 } |
595 | 623 |
596 incr pc | 624 incr pc |
597 set iFail 1 | 625 set iFail 1 |
598 integrity_check "malloc3-(integrity).$iterid" | 626 integrity_check $testid |
599 } elseif {[regexp {.*out of memory} $msg] || [db errorcode] == 3082} { | 627 } elseif {[regexp {.*out of memory} $msg] || [db errorcode] == 3082} { |
600 # Out of memory error, as expected. | 628 # Out of memory error, as expected. |
601 # | 629 # |
602 integrity_check "malloc3-(integrity).$iterid" | 630 integrity_check $testid |
603 incr iFail | 631 incr iFail |
604 if {$nac && !$ac} { | 632 if {$nac && !$ac} { |
605 | 633 if {![lindex $v 1] && [db errorcode] != 3082} { |
606 if {![lindex $v 0] && [db errorcode] != 3082} { | 634 # error "Statement \"[lindex $v 2]\" caused a rollback" |
607 # error "Statement \"[lindex $v 1]\" caused a rollback" | |
608 } | 635 } |
609 | 636 |
610 for {set i $begin_pc} {$i < $pc} {incr i} { | 637 for {set i $begin_pc} {$i < $pc} {incr i} { |
611 set k2 [lindex $arglist [expr 2 * $i]] | 638 set k2 [lindex $arglist [expr {2 * $i}]] |
612 set v2 [lindex $arglist [expr 2 * $i + 1]] | 639 set v2 [lindex $arglist [expr {2 * $i + 1}]] |
613 set catchupsql "" | 640 set catchupsql "" |
614 switch -- $k2 { | 641 switch -- $k2 { |
615 -sql {set catchupsql [lindex $v2 1]} | 642 -sql {set catchupsql [lindex $v2 2]} |
616 -prep {set catchupsql $v2} | 643 -prep {set catchupsql $v2} |
617 } | 644 } |
618 db eval $catchupsql | 645 db eval $catchupsql |
619 } | 646 } |
620 } | 647 } |
621 } else { | 648 } else { |
622 error $msg | 649 error $msg |
623 } | 650 } |
624 | 651 |
625 while {[lindex $arglist [expr 2 * ($pc -1)]] == "-test"} { | 652 # back up to the previous "-test" block. |
| 653 while {[lindex $arglist [expr {2 * ($pc - 1)}]] == "-test"} { |
626 incr pc -1 | 654 incr pc -1 |
627 } | 655 } |
628 } | 656 } |
629 | 657 |
630 -prep { | 658 -prep { |
631 db eval $v | 659 db eval $v |
632 incr pc | 660 incr pc |
633 } | 661 } |
634 | 662 |
635 -debug { | 663 -debug { |
636 eval $v | 664 eval $v |
637 incr pc | 665 incr pc |
638 } | 666 } |
639 | 667 |
640 default { error "Unknown switch: $k" } | 668 default { error "Unknown switch: $k" } |
641 } | 669 } |
642 } | 670 } |
643 } | 671 } |
644 | 672 |
645 # Turn of the Tcl interface's prepared statement caching facility. Then | 673 # Turn off the Tcl interface's prepared statement caching facility. Then |
646 # run the tests with "persistent" malloc failures. | 674 # run the tests with "persistent" malloc failures. |
647 sqlite3_extended_result_codes db 1 | 675 sqlite3_extended_result_codes db 1 |
648 db cache size 0 | 676 db cache size 0 |
649 run_test $::run_test_script 1 | 677 run_test $::run_test_script 1 |
650 | 678 |
651 # Close and reopen the db. | 679 # Close and reopen the db. |
652 db close | 680 db close |
653 file delete -force test.db test.db-journal test2.db test2.db-journal | 681 forcedelete test.db test.db-journal test2.db test2.db-journal |
654 sqlite3 db test.db | 682 sqlite3 db test.db |
655 sqlite3_extended_result_codes db 1 | 683 sqlite3_extended_result_codes db 1 |
656 set ::DB [sqlite3_connection_pointer db] | 684 set ::DB [sqlite3_connection_pointer db] |
657 | 685 |
658 # Turn off the Tcl interface's prepared statement caching facility in | 686 # Turn off the Tcl interface's prepared statement caching facility in |
659 # the new connnection. Then run the tests with "transient" malloc failures. | 687 # the new connnection. Then run the tests with "transient" malloc failures. |
660 db cache size 0 | 688 db cache size 0 |
661 run_test $::run_test_script 0 | 689 run_test $::run_test_script 0 |
662 | 690 |
663 sqlite3_memdebug_fail -1 | 691 sqlite3_memdebug_fail -1 |
664 finish_test | 692 finish_test |
OLD | NEW |