Fix more problems with malloc() and IO failures. (CVS 2985)

FossilOrigin-Name: 29281dea81c909b70b2d914d7061a6df8f388195
This commit is contained in:
danielk1977
2006-01-21 12:08:54 +00:00
parent 02afc86171
commit c4da5b9f2c
8 changed files with 183 additions and 27 deletions
+146 -4
View File
@@ -13,7 +13,7 @@
# cache context. What happens to connection B if one connection A encounters
# an IO-error whilst reading or writing the file-system?
#
# $Id: shared_err.test,v 1.2 2006/01/20 16:32:05 danielk1977 Exp $
# $Id: shared_err.test,v 1.3 2006/01/21 12:08:55 danielk1977 Exp $
proc skip {args} {}
@@ -28,6 +28,101 @@ ifcapable !shared_cache||!subquery {
}
set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
# Todo: This is a copy of the [do_malloc_test] proc in malloc.test
# It would be better if these were consolidated.
# 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 shared_malloc-$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]
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"}]
if {!$v2} {puts "\nError message returned: $msg"}
lappend v $v2
}
} {1 1}
sqlite_malloc_fail 0
if {[info exists ::mallocopts(-cleanup)]} {
catch [list uplevel #0 $::mallocopts(-cleanup)] msg
}
}
unset ::mallocopts
}
do_ioerr_test shared_ioerr-1 -tclprep {
sqlite3 db2 test.db
execsql {
@@ -134,6 +229,53 @@ do_ioerr_test shared_ioerr-2 -tclprep {
# "saved" (because another cursor is going to modify the underlying table).
#
do_ioerr_test shared_ioerr-3 -tclprep {
sqlite3 db2 test.db
execsql {
PRAGMA read_uncommitted = 1;
PRAGMA cache_size = 10;
BEGIN;
CREATE TABLE t1(a, b, UNIQUE(a, b));
} db2
for {set i 0} {$i < 200} {incr i} {
set a [string range [string repeat "[format %03d $i]." 5] 0 end-1]
set b [string repeat $i 2000]
execsql {INSERT INTO t1 VALUES($a, $b)} db2
}
execsql {COMMIT} db2
set ::DB2 [sqlite3_connection_pointer db2]
set ::STMT [sqlite3_prepare $::DB2 "SELECT a FROM t1 ORDER BY a" -1 DUMMY]
sqlite3_step $::STMT ;# Cursor points at 000.000.000.000
sqlite3_step $::STMT ;# Cursor points at 001.001.001.001
} -tclbody {
execsql {
INSERT INTO t1 VALUES('201.201.201.201.201', NULL);
}
} -cleanup {
do_test shared_ioerr-3.$n.cleanup.1 {
sqlite3_step $::STMT
} {SQLITE_ROW}
do_test shared_ioerr-3.$n.cleanup.2 {
sqlite3_column_text $::STMT 0
} {002.002.002.002.002}
do_test shared_ioerr-3.$n.cleanup.3 {
sqlite3_finalize $::STMT
} {SQLITE_OK}
# db2 eval {select * from sqlite_master}
db2 close
}
# Provoke a malloc() failure when a cursor position is being saved. This
# only happens with index cursors (because they malloc() space to save the
# current key value). It does not happen with tables, because an integer
# key does not require a malloc() to store.
#
# The library should return an SQLITE_NOMEM to the caller. The query that
# owns the cursor (the one for which the position is not saved) should
# continue unaffected.
#
do_malloc_test 4 -tclprep {
sqlite3 db2 test.db
execsql {
PRAGMA read_uncommitted = 1;
@@ -155,13 +297,13 @@ do_ioerr_test shared_ioerr-3 -tclprep {
INSERT INTO t1 VALUES(6, NULL);
}
} -cleanup {
do_test shared_ioerr-3.$n.cleanup.1 {
do_test shared_malloc-4.$::n.cleanup.1 {
sqlite3_step $::STMT
} {SQLITE_ROW}
do_test shared_ioerr-3.$n.cleanup.2 {
do_test shared_malloc-4.$::n.cleanup.2 {
sqlite3_column_text $::STMT 0
} {2222222222}
do_test shared_ioerr-3.$n.cleanup.3 {
do_test shared_malloc-4.$::n.cleanup.3 {
sqlite3_finalize $::STMT
} {SQLITE_OK}
# db2 eval {select * from sqlite_master}