sl@0: # 2001 October 12 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: incrvacuum_ioerr.test,v 1.6 2008/07/12 14:52:20 drh Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: # If this build of the library does not support auto-vacuum, omit this sl@0: # whole file. sl@0: ifcapable {!autovacuum} { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: do_ioerr_test incrvacuum-ioerr-1 -cksum 1 -sqlprep { sl@0: PRAGMA auto_vacuum = 'incremental'; sl@0: CREATE TABLE abc(a); sl@0: INSERT INTO abc VALUES(randstr(1500,1500)); sl@0: } -sqlbody { sl@0: BEGIN; sl@0: CREATE TABLE abc2(a); sl@0: DELETE FROM abc; sl@0: PRAGMA incremental_vacuum; sl@0: COMMIT; sl@0: } sl@0: sl@0: # do_ioerr_test incrvacuum-ioerr-3 -start 1 -cksum 1 -tclprep { sl@0: # db eval { sl@0: # PRAGMA auto_vacuum = 'full'; sl@0: # PRAGMA cache_size = 10; sl@0: # BEGIN; sl@0: # CREATE TABLE abc(a, UNIQUE(a)); sl@0: # } sl@0: # for {set ii 0} {$ii < 25} {incr ii} { sl@0: # db eval {INSERT INTO abc VALUES(randstr(1500,1500))} sl@0: # } sl@0: # db eval COMMIT sl@0: # } -sqlbody { sl@0: # BEGIN; sl@0: # DELETE FROM abc WHERE (oid%3)==0; sl@0: # INSERT INTO abc SELECT a || '1234567890' FROM abc WHERE oid%2; sl@0: # CREATE INDEX abc_i ON abc(a); sl@0: # DELETE FROM abc WHERE (oid%2)==0; sl@0: # DROP INDEX abc_i; sl@0: # COMMIT; sl@0: # } sl@0: sl@0: do_ioerr_test incrvacuum-ioerr-2 -start 1 -cksum 1 -tclprep { sl@0: db eval { sl@0: PRAGMA auto_vacuum = 'full'; sl@0: PRAGMA cache_size = 10; sl@0: BEGIN; sl@0: CREATE TABLE abc(a, UNIQUE(a)); sl@0: } sl@0: for {set ii 0} {$ii < 25} {incr ii} { sl@0: db eval {INSERT INTO abc VALUES(randstr(1500,1500))} sl@0: } sl@0: db eval COMMIT sl@0: } -sqlbody { sl@0: BEGIN; sl@0: PRAGMA incremental_vacuum; sl@0: DELETE FROM abc WHERE (oid%3)==0; sl@0: PRAGMA incremental_vacuum; sl@0: INSERT INTO abc SELECT a || '1234567890' FROM abc WHERE oid%2; sl@0: PRAGMA incremental_vacuum; sl@0: CREATE INDEX abc_i ON abc(a); sl@0: DELETE FROM abc WHERE (oid%2)==0; sl@0: PRAGMA incremental_vacuum; sl@0: DROP INDEX abc_i; sl@0: PRAGMA incremental_vacuum; sl@0: COMMIT; sl@0: } sl@0: sl@0: do_ioerr_test incrvacuum-ioerr-3 -start 1 -cksum 1 -tclprep { sl@0: db eval { sl@0: PRAGMA auto_vacuum = 'incremental'; sl@0: BEGIN; sl@0: CREATE TABLE a(i integer, b blob); sl@0: INSERT INTO a VALUES(1, randstr(1500,1500)); sl@0: INSERT INTO a VALUES(2, randstr(1500,1500)); sl@0: } sl@0: db eval COMMIT sl@0: db eval {DELETE FROM a WHERE oid} sl@0: } -sqlbody { sl@0: PRAGMA incremental_vacuum(5); sl@0: } -cleanup { sl@0: sqlite3 db test.db sl@0: integrity_check incrvacuum-ioerr-2.$n.integritycheck sl@0: db close sl@0: } sl@0: sl@0: sl@0: ifcapable shared_cache { sl@0: sl@0: catch { db close } sl@0: file delete -force test.db sl@0: set ::enable_shared_cache [sqlite3_enable_shared_cache 1] sl@0: sl@0: # Create two connections to a single shared-cache: sl@0: # sl@0: sqlite3 db1 test.db sl@0: sqlite3 db2 test.db sl@0: sl@0: # Create a database with around 20 free pages. sl@0: # sl@0: do_test incrvacuum-ioerr-4.0 { sl@0: execsql { sl@0: PRAGMA page_size = 1024; sl@0: PRAGMA locking_mode = exclusive; sl@0: PRAGMA auto_vacuum = 'incremental'; sl@0: BEGIN; sl@0: CREATE TABLE a(i integer, b blob); sl@0: } db1 sl@0: for {set ii 0} {$ii < 20} {incr ii} { sl@0: execsql { INSERT INTO a VALUES($ii, randstr(800,1500)); } db1 sl@0: } sl@0: execsql COMMIT db1 sl@0: execsql {DELETE FROM a WHERE oid} db1 sl@0: } {} sl@0: sl@0: set ::rc 1 sl@0: for {set iTest 1} {$::rc && $iTest<2000} {incr iTest} { sl@0: sl@0: # Figure out how big the database is and how many free pages it sl@0: # has before running incremental-vacuum. sl@0: # sl@0: set nPage [expr {[file size test.db]/1024}] sl@0: set nFree [execsql {pragma freelist_count} db1] sl@0: sl@0: # Now run incremental-vacuum to vacuum 5 pages from the db file. sl@0: # The iTest'th I/O call is set to fail. sl@0: # sl@0: set ::sqlite_io_error_pending $iTest sl@0: set ::sqlite_io_error_persist 1 sl@0: do_test incrvacuum-ioerr-4.$iTest.1 { sl@0: set ::rc [catch {execsql {pragma incremental_vacuum(5)} db1} msg] sl@0: expr {$::rc==0 || $msg eq "disk I/O error"} sl@0: } {1} sl@0: sl@0: set ::sqlite_io_error_pending 0 sl@0: set ::sqlite_io_error_persist 0 sl@0: set ::sqlite_io_error_hit 0 sl@0: set ::sqlite_io_error_hardhit 0 sl@0: sl@0: set nFree2 [execsql {pragma freelist_count} db1] sl@0: set nPage2 [expr {[file size test.db]/1024}] sl@0: sl@0: do_test incrvacuum-ioerr-4.$iTest.2 { sl@0: set shrink [expr {$nPage-$nPage2}] sl@0: expr {$shrink==0 || $shrink==5} sl@0: } {1} sl@0: sl@0: do_test incrvacuum-ioerr-4.$iTest.3 { sl@0: expr {$nPage - $nPage2} sl@0: } [expr {$nFree - $nFree2}] sl@0: } sl@0: sl@0: # Close the two database connections and restore the default sl@0: # shared-cache mode setting. sl@0: # sl@0: db1 close sl@0: db2 close sl@0: sqlite3_enable_shared_cache $::enable_shared_cache sl@0: } sl@0: sl@0: finish_test