1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-30 19:03:16 +03:00

Add tests and fixes for handling malloc() failures related to the virtual table feature. (CVS 3285)

FossilOrigin-Name: 5d1d907189ff3ca7afada83033280cf258984ac0
This commit is contained in:
danielk1977
2006-06-23 08:05:19 +00:00
parent a298e90d50
commit be71889703
13 changed files with 257 additions and 92 deletions

View File

@ -6,7 +6,7 @@
#***********************************************************************
# This file runs all tests.
#
# $Id: quick.test,v 1.44 2006/06/14 10:38:03 danielk1977 Exp $
# $Id: quick.test,v 1.45 2006/06/23 08:05:38 danielk1977 Exp $
proc lshift {lvar} {
upvar $lvar l
@ -55,6 +55,7 @@ set EXCLUDE {
btree8.test
utf16.test
shared_err.test
vtab_err.test
}
if {[sqlite3 -has-codec]} {

View File

@ -11,7 +11,7 @@
# This file implements some common TCL routines used for regression
# testing the SQLite library
#
# $Id: tester.tcl,v 1.64 2006/01/24 13:09:33 danielk1977 Exp $
# $Id: tester.tcl,v 1.65 2006/06/23 08:05:38 danielk1977 Exp $
# Make sure tclsqlite3 was compiled correctly. Abort now with an
# error message if not.
@ -149,6 +149,7 @@ proc finalize_testing {} {
catch {
pp_check_for_leaks
}
breakpoint
sqlite3 db {}
# sqlite3_clear_tsd_memdebug
db close

View File

@ -9,7 +9,7 @@
#
#***********************************************************************
#
# $Id: vtab_err.test,v 1.1 2006/06/22 09:53:50 danielk1977 Exp $
# $Id: vtab_err.test,v 1.2 2006/06/23 08:05:39 danielk1977 Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
@ -19,6 +19,107 @@ ifcapable !vtab {
return
}
# Usage: do_malloc_test <test number> <options...>
#
# The first argument, <test number>, is an integer used to name the
# tests executed by this proc. Options are as follows:
#
# -tclprep TCL script to run to prepare test.
# -sqlprep SQL script to run to prepare test.
# -tclbody TCL script to run with malloc failure simulation.
# -sqlbody TCL script to run with malloc failure simulation.
# -cleanup TCL script to run after the test.
#
# This command runs a series of tests to verify SQLite's ability
# to handle an out-of-memory condition gracefully. It is assumed
# that if this condition occurs a malloc() call will return a
# NULL pointer. Linux, for example, doesn't do that by default. See
# the "BUGS" section of malloc(3).
#
# Each iteration of a loop, the TCL commands in any argument passed
# to the -tclbody switch, followed by the SQL commands in any argument
# passed to the -sqlbody switch are executed. Each iteration the
# Nth call to sqliteMalloc() is made to fail, where N is increased
# each time the loop runs starting from 1. When all commands execute
# successfully, the loop ends.
#
proc do_malloc_test {tn args} {
array unset ::mallocopts
array set ::mallocopts $args
set ::go 1
for {set ::n 1} {$::go && $::n < 50000} {incr ::n} {
do_test $tn.$::n {
# Remove all traces of database files test.db and test2.db from the files
# system. Then open (empty database) "test.db" with the handle [db].
#
sqlite_malloc_fail 0
catch {db close}
catch {file delete -force test.db}
catch {file delete -force test.db-journal}
catch {file delete -force test2.db}
catch {file delete -force test2.db-journal}
catch {sqlite3 db test.db}
set ::DB [sqlite3_connection_pointer db]
# Execute any -tclprep and -sqlprep scripts.
#
if {[info exists ::mallocopts(-tclprep)]} {
eval $::mallocopts(-tclprep)
}
if {[info exists ::mallocopts(-sqlprep)]} {
execsql $::mallocopts(-sqlprep)
}
# Now set the ${::n}th malloc() to fail and execute the -tclbody and
# -sqlbody scripts.
#
sqlite_malloc_fail $::n
set ::mallocbody {}
if {[info exists ::mallocopts(-tclbody)]} {
append ::mallocbody "$::mallocopts(-tclbody)\n"
}
if {[info exists ::mallocopts(-sqlbody)]} {
append ::mallocbody "db eval {$::mallocopts(-sqlbody)}"
}
set v [catch $::mallocbody msg]
# If the test fails (if $v!=0) and the database connection actually
# exists, make sure the failure code is SQLITE_NOMEM.
if {$v&&[info command db]=="db"&&[info exists ::mallocopts(-sqlbody)]} {
if {[db errorcode]!=7 && $msg!="vtable constructor failed: e"} {
set v 999
}
}
set leftover [lindex [sqlite_malloc_stat] 2]
if {$leftover>0} {
if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v Message=$msg"}
set ::go 0
if {$v} {
puts "\nError message returned: $msg"
} else {
set v {1 1}
}
} else {
set v2 [expr {
$msg == "" || $msg == "out of memory" ||
$msg == "vtable constructor failed: e"
}]
if {!$v2} {puts "\nError message returned: $msg"}
lappend v $v2
}
} {1 1}
if {[info exists ::mallocopts(-cleanup)]} {
catch [list uplevel #0 $::mallocopts(-cleanup)] msg
}
}
unset ::mallocopts
}
do_ioerr_test vtab_err-1 -tclprep {
register_echo_module [sqlite3_connection_pointer db]
} -sqlbody {
@ -38,5 +139,24 @@ do_ioerr_test vtab_err-1 -tclprep {
}
do_malloc_test vtab_err-2 -tclprep {
register_echo_module [sqlite3_connection_pointer db]
} -sqlbody {
BEGIN;
CREATE TABLE r(a PRIMARY KEY, b, c);
CREATE VIRTUAL TABLE e USING echo(r);
INSERT INTO e VALUES(1, 2, 3);
INSERT INTO e VALUES('a', 'b', 'c');
UPDATE e SET c = 10;
DELETE FROM e WHERE a = 'a';
COMMIT;
BEGIN;
CREATE TABLE r2(a, b, c);
INSERT INTO r2 SELECT * FROM e;
INSERT INTO e SELECT a||'x', b, c FROM r2;
COMMIT;
}
sqlite_malloc_fail 0
finish_test