mallocC.test 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #
  15. # $Id: mallocC.test,v 1.10 2009/04/11 16:27:50 drh Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. source $testdir/malloc_common.tcl
  19. # Only run these tests if memory debugging is turned on.
  20. #
  21. if {!$MEMDEBUG} {
  22. puts "Skipping mallocC tests: not compiled with -DSQLITE_MEMDEBUG..."
  23. finish_test
  24. return
  25. }
  26. proc do_mallocC_test {tn args} {
  27. array set ::mallocopts $args
  28. #set sum [allcksum db]
  29. for {set ::n 1} {true} {incr ::n} {
  30. # Run the SQL. Malloc number $::n is set to fail. A malloc() failure
  31. # may or may not be reported.
  32. sqlite3_memdebug_fail $::n -repeat 1
  33. do_test mallocC-$tn.$::n.1 {
  34. set res [catchsql [string trim $::mallocopts(-sql)]]
  35. set rc [expr {
  36. 0==[string compare $res {1 {out of memory}}] ||
  37. [db errorcode] == 3082 ||
  38. 0==[lindex $res 0]
  39. }]
  40. if {$rc!=1} {
  41. puts "Error: $res"
  42. }
  43. set rc
  44. } {1}
  45. # If $::n is greater than the number of malloc() calls required to
  46. # execute the SQL, then this test is finished. Break out of the loop.
  47. set nFail [sqlite3_memdebug_fail -1]
  48. if {$nFail==0} {
  49. break
  50. }
  51. # Recover from the malloc failure.
  52. #
  53. # Update: The new malloc() failure handling means that a transaction may
  54. # still be active even if a malloc() has failed. But when these tests were
  55. # written this was not the case. So do a manual ROLLBACK here so that the
  56. # tests pass.
  57. do_test mallocC-$tn.$::n.2 {
  58. catch {
  59. execsql {
  60. ROLLBACK;
  61. }
  62. }
  63. expr 0
  64. } {0}
  65. # Checksum the database.
  66. #do_test mallocC-$tn.$::n.3 {
  67. # allcksum db
  68. #} $sum
  69. #integrity_check mallocC-$tn.$::n.4
  70. }
  71. unset ::mallocopts
  72. }
  73. sqlite3_extended_result_codes db 1
  74. execsql {
  75. PRAGMA auto_vacuum=1;
  76. CREATE TABLE t0(a, b, c);
  77. }
  78. # The number of memory allocation failures is different on 64-bit
  79. # and 32-bit systems due to larger structures on 64-bit systems
  80. # overflowing the lookaside more often. To debug problems, it is
  81. # sometimes helpful to reduce the size of the lookaside allocation
  82. # blocks. But this is normally disabled.
  83. #
  84. if {0} {
  85. db close
  86. sqlite3_shutdown
  87. sqlite3_config_lookaside 50 500
  88. sqlite3_initialize
  89. autoinstall_test_functions
  90. sqlite3 db test.db
  91. }
  92. do_mallocC_test 1 -sql {
  93. BEGIN;
  94. -- Allocate 32 new root pages. This will exercise the 'extract specific
  95. -- page from the freelist' code when in auto-vacuum mode (see the
  96. -- allocatePage() routine in btree.c).
  97. CREATE TABLE t1(a UNIQUE, b UNIQUE, c UNIQUE);
  98. CREATE TABLE t2(a UNIQUE, b UNIQUE, c UNIQUE);
  99. CREATE TABLE t3(a UNIQUE, b UNIQUE, c UNIQUE);
  100. CREATE TABLE t4(a UNIQUE, b UNIQUE, c UNIQUE);
  101. CREATE TABLE t5(a UNIQUE, b UNIQUE, c UNIQUE);
  102. CREATE TABLE t6(a UNIQUE, b UNIQUE, c UNIQUE);
  103. CREATE TABLE t7(a UNIQUE, b UNIQUE, c UNIQUE);
  104. CREATE TABLE t8(a UNIQUE, b UNIQUE, c UNIQUE);
  105. ROLLBACK;
  106. }
  107. finish_test