sl@0: # 2007 April 2 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. The sl@0: # focus of this file is testing for correct handling of I/O errors sl@0: # such as writes failing because the disk is full. sl@0: # sl@0: # The tests in this file use special facilities that are only sl@0: # available in the SQLite test fixture. sl@0: # sl@0: # $Id: ioerr2.test,v 1.9 2008/08/20 14:49:25 danielk1977 Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: ifcapable !integrityck { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: do_test ioerr2-1.1 { sl@0: execsql { sl@0: PRAGMA cache_size = 10; sl@0: PRAGMA default_cache_size = 10; sl@0: CREATE TABLE t1(a, b, PRIMARY KEY(a, b)); sl@0: INSERT INTO t1 VALUES(randstr(400,400),randstr(400,400)); sl@0: INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 2 sl@0: INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 4 sl@0: INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 8 sl@0: INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 16 sl@0: INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 32 sl@0: } sl@0: } {} sl@0: sl@0: set ::cksum [execsql {SELECT md5sum(a, b) FROM t1}] sl@0: proc check_db {testname} { sl@0: sl@0: # Make sure no I/O errors are simulated in this proc. sl@0: set ::sqlite_io_error_hit 0 sl@0: set ::sqlite_io_error_persist 0 sl@0: set ::sqlite_io_error_pending 0 sl@0: sl@0: # Run an integrity-check. If "disk I/O error" is returned, the sl@0: # pager must be in error state. In this case open a new database sl@0: # connection. Otherwise, try a ROLLBACK, in case a transaction sl@0: # is still active. sl@0: set rc [catch {execsql {PRAGMA integrity_check}} msg] sl@0: if {$rc && ($msg eq "disk I/O error" || $msg eq "database is locked")} { sl@0: db close sl@0: sqlite3 db test.db sl@0: set refcnt 0 sl@0: } else { sl@0: if {$rc || $msg ne "ok"} { sl@0: error $msg sl@0: } sl@0: catch {execsql ROLLBACK} sl@0: } sl@0: sl@0: # Check that the database checksum is still $::cksum, and that sl@0: # the integrity-check passes. sl@0: set ck [execsql {SELECT md5sum(a, b) FROM t1}] sl@0: do_test ${testname}.cksum [list set ck $ck] $::cksum sl@0: integrity_check ${testname}.integrity sl@0: do_test ${testname}.refcnt { sl@0: lindex [sqlite3_pager_refcounts db] 0 sl@0: } 0 sl@0: } sl@0: sl@0: check_db ioerr2-2 sl@0: sl@0: set sql { sl@0: PRAGMA cache_size = 10; sl@0: PRAGMA default_cache_size = 10; sl@0: BEGIN; sl@0: DELETE FROM t1 WHERE (oid%7)==0; sl@0: INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) sl@0: WHERE (random()%7)==0; sl@0: UPDATE t1 SET a = randstr(400,400), b = randstr(400,400) sl@0: WHERE (random()%7)==0; sl@0: ROLLBACK; sl@0: } sl@0: sl@0: foreach bPersist [list 0 1] { sl@0: set ::go 1 sl@0: for {set ::N 1} {$::go} {incr ::N} { sl@0: db close sl@0: sqlite3 db test.db sl@0: set ::sqlite_io_error_hit 0 sl@0: set ::sqlite_io_error_persist $bPersist sl@0: set ::sqlite_io_error_pending $::N sl@0: sl@0: foreach {::go res} [catchsql $sql] {} sl@0: check_db ioerr2-3.$bPersist.$::N sl@0: } sl@0: } sl@0: foreach bPersist [list 0 1] { sl@0: set ::go 1 sl@0: for {set ::N 1} {$::go} {incr ::N} { sl@0: set ::sqlite_io_error_hit 0 sl@0: set ::sqlite_io_error_persist $bPersist sl@0: set ::sqlite_io_error_pending $::N sl@0: sl@0: foreach {::go res} [catchsql $sql] {} sl@0: check_db ioerr2-4.[expr {$bPersist+2}].$::N sl@0: } sl@0: } sl@0: sl@0: do_test ioerr2-5 { sl@0: execsql { sl@0: CREATE TABLE t2 AS SELECT * FROM t1; sl@0: PRAGMA temp_store = memory; sl@0: } sl@0: set ::sqlite_io_error_persist 0 sl@0: set ::go 1 sl@0: set rc [catch { sl@0: for {set ::N 2} {$::N<200} {incr ::N} { sl@0: db eval {SELECT * FROM t1 WHERE rowid IN (1, 5, 10, 15, 20)} { sl@0: set ::sqlite_io_error_hit 0 sl@0: set ::sqlite_io_error_pending $::N sl@0: set sql {UPDATE t2 SET b = randstr(400,400)} sl@0: foreach {::go res} [catchsql $sql] {} sl@0: } sl@0: } sl@0: } msg] sl@0: list $rc $msg sl@0: } {1 {callback requested query abort}} sl@0: sl@0: finish_test