sl@0: # 2007 June 13 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. sl@0: # sl@0: # This file implements tests to verify that ticket #2409 has been sl@0: # fixed. More specifically, they verify that if SQLite cannot sl@0: # obtain an EXCLUSIVE lock while trying to spill the cache during sl@0: # any statement other than a COMMIT, an I/O error is returned instead sl@0: # of SQLITE_BUSY. sl@0: # sl@0: # $Id: tkt2409.test,v 1.6 2008/08/28 17:46:19 drh Exp $ sl@0: sl@0: # Test Outline: sl@0: # sl@0: # tkt-2409-1.*: Cause a cache-spill during an INSERT that is within sl@0: # a db transaction but does not start a statement transaction. sl@0: # Verify that the transaction is automatically rolled back sl@0: # and SQLITE_IOERR_BLOCKED is returned sl@0: # sl@0: # UPDATE: As of the pcache modifications, failing to upgrade to sl@0: # an exclusive lock when attempting a cache-spill is no longer an sl@0: # error. The pcache module allocates more space and keeps working sl@0: # in memory if this occurs. sl@0: # sl@0: # tkt-2409-2.*: Cause a cache-spill while updating the change-counter sl@0: # during a database COMMIT. Verify that the transaction is not sl@0: # rolled back and SQLITE_BUSY is returned. sl@0: # sl@0: # tkt-2409-3.*: Similar to 2409-1.*, but using many INSERT statements sl@0: # within a transaction instead of just one. sl@0: # sl@0: # UPDATE: Again, pcache now just keeps working in main memory. sl@0: # sl@0: # tkt-2409-4.*: Similar to 2409-1.*, but rig it so that the sl@0: # INSERT statement starts a statement transaction. Verify that sl@0: # SQLITE_BUSY is returned and the transaction is not rolled back. sl@0: # sl@0: # UPDATE: This time, SQLITE_BUSY is not returned. pcache just uses sl@0: # more malloc()'d memory. sl@0: # sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: ifcapable !pager_pragmas { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: sqlite3_extended_result_codes $::DB 1 sl@0: sl@0: # Aquire a read-lock on the database using handle [db2]. sl@0: # sl@0: proc read_lock_db {} { sl@0: if {$::STMT eq ""} { sl@0: set ::STMT [sqlite3_prepare db2 {SELECT rowid FROM sqlite_master} -1 TAIL] sl@0: set rc [sqlite3_step $::STMT] sl@0: if {$rc eq "SQLITE_ERROR"} { sl@0: unread_lock_db sl@0: read_lock_db sl@0: } sl@0: } sl@0: } sl@0: sl@0: # Release any read-lock obtained using [read_lock_db] sl@0: # sl@0: proc unread_lock_db {} { sl@0: if {$::STMT ne ""} { sl@0: sqlite3_finalize $::STMT sl@0: set ::STMT "" sl@0: } sl@0: } sl@0: sl@0: # Open the db handle used by [read_lock_db]. sl@0: # sl@0: sqlite3 db2 test.db sl@0: set ::STMT "" sl@0: sl@0: do_test tkt2409-1.1 { sl@0: execsql { sl@0: PRAGMA cache_size=10; sl@0: CREATE TABLE t1(x TEXT UNIQUE NOT NULL, y BLOB); sl@0: } sl@0: read_lock_db sl@0: set ::zShort [string repeat 0123456789 1] sl@0: set ::zLong [string repeat 0123456789 1500] sl@0: catchsql { sl@0: BEGIN; sl@0: INSERT INTO t1 VALUES($::zShort, $::zLong); sl@0: } sl@0: } {0 {}} sl@0: sl@0: do_test tkt2409-1.2 { sl@0: sqlite3_errcode $::DB sl@0: } {SQLITE_OK} sl@0: sl@0: # Check the integrity of the cache. sl@0: # sl@0: integrity_check tkt2409-1.3 sl@0: sl@0: # Check that the transaction was rolled back. Because the INSERT sl@0: # statement in which the "I/O error" occured did not open a statement sl@0: # transaction, SQLite had no choice but to roll back the transaction. sl@0: # sl@0: do_test tkt2409-1.4 { sl@0: unread_lock_db sl@0: catchsql { ROLLBACK } sl@0: } {0 {}} sl@0: sl@0: set ::zShort [string repeat 0123456789 1] sl@0: set ::zLong [string repeat 0123456789 1500] sl@0: set ::rc 1 sl@0: for {set iCache 10} {$::rc} {incr iCache} { sl@0: execsql "PRAGMA cache_size = $iCache" sl@0: do_test tkt2409-2.1.$iCache { sl@0: read_lock_db sl@0: set ::rc [catch { sl@0: execsql { sl@0: BEGIN; sl@0: INSERT INTO t1 VALUES($::zShort, $::zLong); sl@0: } sl@0: } msg] sl@0: expr {($::rc == 1 && $msg eq "disk I/O error") || $::rc == 0} sl@0: } {1} sl@0: } sl@0: sl@0: do_test tkt2409-2.2 { sl@0: catchsql { sl@0: ROLLBACK; sl@0: BEGIN; sl@0: INSERT INTO t1 VALUES($::zShort, $::zLong); sl@0: COMMIT; sl@0: } sl@0: } {1 {database is locked}} sl@0: sl@0: do_test tkt2409-2.3 { sl@0: unread_lock_db sl@0: catchsql { sl@0: COMMIT; sl@0: } sl@0: } {0 {}} sl@0: sl@0: sl@0: do_test tkt2409-3.1 { sl@0: db close sl@0: set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] sl@0: sqlite3_extended_result_codes $::DB 1 sl@0: execsql { sl@0: PRAGMA cache_size=10; sl@0: DELETE FROM t1; sl@0: } sl@0: read_lock_db sl@0: set ::zShort [string repeat 0123456789 1] sl@0: set ::zLong [string repeat 0123456789 1500] sl@0: catchsql { sl@0: BEGIN; sl@0: INSERT INTO t1 SELECT $::zShort, $::zLong; sl@0: } sl@0: } {0 {}} sl@0: sl@0: do_test tkt2409-3.2 { sl@0: sqlite3_errcode $::DB sl@0: } {SQLITE_OK} sl@0: sl@0: # Check the integrity of the cache. sl@0: # sl@0: integrity_check tkt2409-3.3 sl@0: sl@0: # Check that the transaction was rolled back. Because the INSERT sl@0: # statement in which the "I/O error" occured did not open a statement sl@0: # transaction, SQLite had no choice but to roll back the transaction. sl@0: # sl@0: do_test tkt2409-3.4 { sl@0: unread_lock_db sl@0: catchsql { ROLLBACK } sl@0: } {0 {}} sl@0: integrity_check tkt2409-3.5 sl@0: sl@0: expr {srand(1)} sl@0: do_test tkt2409-4.1 { sl@0: execsql { sl@0: PRAGMA cache_size=20; sl@0: DROP TABLE t1; sl@0: CREATE TABLE t1 (x TEXT UNIQUE NOT NULL); sl@0: } sl@0: sl@0: unset -nocomplain t1 sl@0: array unset t1 sl@0: set t1(0) 1 sl@0: set sql "" sl@0: for {set i 0} {$i<5000} {incr i} { sl@0: set r 0 sl@0: while {[info exists t1($r)]} { sl@0: set r [expr {int(rand()*1000000000)}] sl@0: } sl@0: set t1($r) 1 sl@0: append sql "INSERT INTO t1 VALUES('some-text-$r');" sl@0: } sl@0: sl@0: read_lock_db sl@0: execsql BEGIN sl@0: catchsql $sql sl@0: } {0 {}} sl@0: sl@0: do_test tkt2409-4.2 { sl@0: sqlite3_errcode $::DB sl@0: } {SQLITE_OK} sl@0: sl@0: # Check the integrity of the cache. sl@0: # sl@0: integrity_check tkt2409-4.3 sl@0: sl@0: do_test tkt2409-4.4 { sl@0: catchsql { ROLLBACK } sl@0: } {0 {}} sl@0: integrity_check tkt2409-4.5 sl@0: sl@0: unread_lock_db sl@0: db2 close sl@0: unset -nocomplain t1 sl@0: finish_test