📄 malloc3.test
字号:
SQL -norollback {UPDATE abc SET a = a + 1, c = c + 1;}TEST_AUTOCOMMIT 12 0SQL {DELETE FROM abc WHERE a = 10;}TEST_AUTOCOMMIT 13 0SQL {COMMIT;}TEST 14 { do_test $testid.1 { sqlite3_get_autocommit $::DB } {1} do_test $testid.2 { execsql {SELECT a, b, c FROM abc} } {1 2 3 4 5 6 7 8 9}}PREP [subst { DROP TABLE abc; CREATE TABLE abc(a, padding, b, c); INSERT INTO abc VALUES(1, '$padding', 2, 3); INSERT INTO abc VALUES(4, '$padding', 5, 6); INSERT INTO abc VALUES(7, '$padding', 8, 9); CREATE INDEX abc_i ON abc(a, padding, b, c);}]TEST 15 { db eval {PRAGMA cache_size = 10}}SQL {BEGIN;}SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc}TEST 16 { do_test $testid { execsql {SELECT a, count(*) FROM abc GROUP BY a;} } {1 2 4 2 7 2}}SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc}TEST 17 { do_test $testid { execsql {SELECT a, count(*) FROM abc GROUP BY a;} } {1 4 4 4 7 4}}SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc}TEST 18 { do_test $testid { execsql {SELECT a, count(*) FROM abc GROUP BY a;} } {1 8 4 8 7 8}}SQL -norllbck {INSERT INTO abc (oid, a, padding, b, c) SELECT NULL, * FROM abc}TEST 19 { do_test $testid { execsql {SELECT a, count(*) FROM abc GROUP BY a;} } {1 16 4 16 7 16}}SQL {COMMIT;}TEST 21 { do_test $testid { execsql {SELECT a, count(*) FROM abc GROUP BY a;} } {1 16 4 16 7 16}}SQL {BEGIN;}SQL {DELETE FROM abc WHERE oid %2}TEST 22 { do_test $testid { execsql {SELECT a, count(*) FROM abc GROUP BY a;} } {1 8 4 8 7 8}}SQL {DELETE FROM abc}TEST 23 { do_test $testid { execsql {SELECT * FROM abc} } {}}SQL {ROLLBACK;}TEST 24 { do_test $testid { execsql {SELECT a, count(*) FROM abc GROUP BY a;} } {1 16 4 16 7 16}}# Test some schema modifications inside of a transaction. These should all# cause transaction rollback if they fail. Also query a view, to cover a bit# more code.#PREP {DROP VIEW abc_v;}TEST 25 { do_test $testid { execsql { SELECT name, tbl_name FROM sqlite_master; } } {abc abc abc_i abc}}SQL {BEGIN;}SQL {CREATE TABLE def(d, e, f);}SQL {CREATE TABLE ghi(g, h, i);}TEST 26 { do_test $testid { execsql { SELECT name, tbl_name FROM sqlite_master; } } {abc abc abc_i abc def def ghi ghi}}SQL {CREATE VIEW v1 AS SELECT * FROM def, ghi}SQL {CREATE UNIQUE INDEX ghi_i1 ON ghi(g);}TEST 27 { do_test $testid { execsql { SELECT name, tbl_name FROM sqlite_master; } } {abc abc abc_i abc def def ghi ghi v1 v1 ghi_i1 ghi}}SQL {INSERT INTO def VALUES('a', 'b', 'c')}SQL {INSERT INTO def VALUES(1, 2, 3)}SQL -norollback {INSERT INTO ghi SELECT * FROM def}TEST 28 { do_test $testid { execsql { SELECT * FROM def, ghi WHERE d = g; } } {a b c a b c 1 2 3 1 2 3}}SQL {COMMIT}TEST 29 { do_test $testid { execsql { SELECT * FROM v1 WHERE d = g; } } {a b c a b c 1 2 3 1 2 3}}# Test a simple multi-file transaction #file delete -force test2.dbifcapable attach { SQL {ATTACH 'test2.db' AS aux;} SQL {BEGIN} SQL {CREATE TABLE aux.tbl2(x, y, z)} SQL {INSERT INTO tbl2 VALUES(1, 2, 3)} SQL {INSERT INTO def VALUES(4, 5, 6)} TEST 30 { do_test $testid { execsql { SELECT * FROM tbl2, def WHERE d = x; } } {1 2 3 1 2 3} } SQL {COMMIT} TEST 31 { do_test $testid { execsql { SELECT * FROM tbl2, def WHERE d = x; } } {1 2 3 1 2 3} }}# Test what happens when a malloc() fails while there are other active# statements. This changes the way sqlite3VdbeHalt() works.TEST 32 { if {![info exists ::STMT32]} { set sql "SELECT name FROM sqlite_master" set ::STMT32 [sqlite3_prepare $::DB $sql -1 DUMMY] do_test $testid { sqlite3_step $::STMT32 } {SQLITE_ROW} }}SQL BEGINTEST 33 { do_test $testid { execsql {SELECT * FROM ghi} } {a b c 1 2 3}}SQL -norollback { -- There is a unique index on ghi(g), so this statement may not cause -- an automatic ROLLBACK. Hence the "-norollback" switch. INSERT INTO ghi SELECT '2'||g, h, i FROM ghi;}TEST 34 { if {[info exists ::STMT32]} { do_test $testid { sqlite3_finalize $::STMT32 } {SQLITE_OK} unset ::STMT32 }}SQL COMMIT## End of test program declaration#--------------------------------------------------------------------------proc run_test {arglist iRepeat {pcstart 0} {iFailStart 1}} { if {[llength $arglist] %2} { error "Uneven number of arguments to TEST" } for {set i 0} {$i < $pcstart} {incr i} { set k2 [lindex $arglist [expr 2 * $i]] set v2 [lindex $arglist [expr 2 * $i + 1]] set ac [sqlite3_get_autocommit $::DB] ;# Auto-Commit switch -- $k2 { -sql {db eval [lindex $v2 1]} -prep {db eval $v2} } set nac [sqlite3_get_autocommit $::DB] ;# New Auto-Commit if {$ac && !$nac} {set begin_pc $i} } db rollback_hook [list incr ::rollback_hook_count] set iFail $iFailStart set pc $pcstart while {$pc*2 < [llength $arglist]} { # Id of this iteration: set k [lindex $arglist [expr 2 * $pc]] set iterid "pc=$pc.iFail=$iFail$k" set v [lindex $arglist [expr 2 * $pc + 1]] switch -- $k { -test { foreach {id script} $v {} incr pc } -sql { set ::rollback_hook_count 0 set ac [sqlite3_get_autocommit $::DB] ;# Auto-Commit sqlite3_memdebug_fail $iFail -repeat 0 set rc [catch {db eval [lindex $v 1]} msg] ;# True error occurs set nac [sqlite3_get_autocommit $::DB] ;# New Auto-Commit if {$rc != 0 && $nac && !$ac} { # Before [db eval] the auto-commit flag was clear. Now it # is set. Since an error occured we assume this was not a # commit - therefore a rollback occured. Check that the # rollback-hook was invoked. do_test malloc3-rollback_hook.$iterid { set ::rollback_hook_count } {1} } set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign] if {$rc == 0} { # Successful execution of sql. The number of failed malloc() # calls should be equal to the number of benign failures. # Otherwise a malloc() failed and the error was not reported. # if {$nFail!=$nBenign} { error "Unreported malloc() failure" } if {$ac && !$nac} { # Before the [db eval] the auto-commit flag was set, now it # is clear. We can deduce that a "BEGIN" statement has just # been successfully executed. set begin_pc $pc } incr pc set iFail 1 integrity_check "malloc3-(integrity).$iterid" } elseif {[regexp {.*out of memory} $msg] || [db errorcode] == 3082} { # Out of memory error, as expected. # integrity_check "malloc3-(integrity).$iterid" incr iFail if {$nac && !$ac} { if {![lindex $v 0] && [db errorcode] != 3082} { # error "Statement \"[lindex $v 1]\" caused a rollback" } for {set i $begin_pc} {$i < $pc} {incr i} { set k2 [lindex $arglist [expr 2 * $i]] set v2 [lindex $arglist [expr 2 * $i + 1]] set catchupsql "" switch -- $k2 { -sql {set catchupsql [lindex $v2 1]} -prep {set catchupsql $v2} } db eval $catchupsql } } } else { error $msg } while {[lindex $arglist [expr 2 * ($pc -1)]] == "-test"} { incr pc -1 } } -prep { db eval $v incr pc } -debug { eval $v incr pc } default { error "Unknown switch: $k" } } }}# Turn of the Tcl interface's prepared statement caching facility. Then# run the tests with "persistent" malloc failures.sqlite3_extended_result_codes db 1db cache size 0run_test $::run_test_script 1# Close and reopen the db.db closefile delete -force test.db test.db-journal test2.db test2.db-journalsqlite3 db test.dbsqlite3_extended_result_codes db 1set ::DB [sqlite3_connection_pointer db]# Turn of the Tcl interface's prepared statement caching facility in# the new connnection. Then run the tests with "transient" malloc failures.db cache size 0run_test $::run_test_script 0sqlite3_memdebug_fail -1finish_test
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -