drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 1 | # 2007 Aug 13 |
| 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 tests aspects of the malloc failure while parsing |
| 13 | # CREATE TABLE statements in auto_vacuum mode. |
| 14 | # |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame^] | 15 | # $Id: mallocC.test,v 1.8 2008/01/16 17:46:38 drh Exp $ |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 16 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | source $testdir/tester.tcl |
| 19 | |
| 20 | # Only run these tests if memory debugging is turned on. |
| 21 | # |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 22 | ifcapable !memdebug||!compound { |
drh | 5a3032b | 2007-09-03 16:12:09 +0000 | [diff] [blame] | 23 | puts "Skipping mallocC tests: not compiled with -DSQLITE_MEMDEBUG..." |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 24 | finish_test |
| 25 | return |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 26 | } |
| 27 | |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 28 | proc do_mallocC_test {tn args} { |
| 29 | array set ::mallocopts $args |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame^] | 30 | #set sum [allcksum db] |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 31 | |
| 32 | for {set ::n 1} {true} {incr ::n} { |
| 33 | |
| 34 | # Run the SQL. Malloc number $::n is set to fail. A malloc() failure |
| 35 | # may or may not be reported. |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 36 | sqlite3_memdebug_fail $::n -repeat 1 |
drh | b403574 | 2007-08-13 12:58:18 +0000 | [diff] [blame] | 37 | do_test mallocC-$tn.$::n.1 { |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 38 | set res [catchsql [string trim $::mallocopts(-sql)]] |
| 39 | set rc [expr { |
| 40 | 0==[string compare $res {1 {out of memory}}] || |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 41 | [db errorcode] == 3082 || |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 42 | 0==[lindex $res 0] |
| 43 | }] |
| 44 | if {$rc!=1} { |
| 45 | puts "Error: $res" |
| 46 | } |
| 47 | set rc |
| 48 | } {1} |
| 49 | |
| 50 | # If $::n is greater than the number of malloc() calls required to |
| 51 | # execute the SQL, then this test is finished. Break out of the loop. |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 52 | set nFail [sqlite3_memdebug_fail -1] |
drh | ed138fb | 2007-08-22 22:04:37 +0000 | [diff] [blame] | 53 | if {$nFail==0} { |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 54 | break |
| 55 | } |
| 56 | |
| 57 | # Recover from the malloc failure. |
| 58 | # |
| 59 | # Update: The new malloc() failure handling means that a transaction may |
| 60 | # still be active even if a malloc() has failed. But when these tests were |
| 61 | # written this was not the case. So do a manual ROLLBACK here so that the |
| 62 | # tests pass. |
drh | b403574 | 2007-08-13 12:58:18 +0000 | [diff] [blame] | 63 | do_test mallocC-$tn.$::n.2 { |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 64 | catch { |
| 65 | execsql { |
| 66 | ROLLBACK; |
| 67 | } |
| 68 | } |
| 69 | expr 0 |
| 70 | } {0} |
| 71 | |
| 72 | # Checksum the database. |
drh | b403574 | 2007-08-13 12:58:18 +0000 | [diff] [blame] | 73 | #do_test mallocC-$tn.$::n.3 { |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame^] | 74 | # allcksum db |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 75 | #} $sum |
| 76 | |
drh | b403574 | 2007-08-13 12:58:18 +0000 | [diff] [blame] | 77 | #integrity_check mallocC-$tn.$::n.4 |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 78 | if {$::nErr>1} return |
| 79 | } |
| 80 | unset ::mallocopts |
| 81 | } |
| 82 | |
danielk1977 | ae72d98 | 2007-10-03 08:46:44 +0000 | [diff] [blame] | 83 | sqlite3_extended_result_codes db 1 |
| 84 | |
drh | b3226e8 | 2007-08-13 12:39:03 +0000 | [diff] [blame] | 85 | execsql { |
| 86 | PRAGMA auto_vacuum=1; |
| 87 | CREATE TABLE t0(a, b, c); |
| 88 | } |
| 89 | do_mallocC_test 1 -sql { |
| 90 | BEGIN; |
| 91 | -- Allocate 32 new root pages. This will exercise the 'extract specific |
| 92 | -- page from the freelist' code when in auto-vacuum mode (see the |
| 93 | -- allocatePage() routine in btree.c). |
| 94 | CREATE TABLE t1(a UNIQUE, b UNIQUE, c UNIQUE); |
| 95 | CREATE TABLE t2(a UNIQUE, b UNIQUE, c UNIQUE); |
| 96 | CREATE TABLE t3(a UNIQUE, b UNIQUE, c UNIQUE); |
| 97 | CREATE TABLE t4(a UNIQUE, b UNIQUE, c UNIQUE); |
| 98 | CREATE TABLE t5(a UNIQUE, b UNIQUE, c UNIQUE); |
| 99 | CREATE TABLE t6(a UNIQUE, b UNIQUE, c UNIQUE); |
| 100 | CREATE TABLE t7(a UNIQUE, b UNIQUE, c UNIQUE); |
| 101 | CREATE TABLE t8(a UNIQUE, b UNIQUE, c UNIQUE); |
| 102 | |
| 103 | ROLLBACK; |
| 104 | } |
| 105 | |
| 106 | finish_test |