sqlite/test/walfault.test
dan 459564f43f Delay opening the sub-journal until SQLite actually needs to write data to it.
FossilOrigin-Name: c43deb33ae5f191ea2e054181759beeeb9ea71bf
2010-06-03 12:35:28 +00:00

403 lines
12 KiB
Plaintext

# 2010 May 03
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. The
# focus of this file is testing the operation of the library in
# "PRAGMA journal_mode=WAL" mode.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/malloc_common.tcl
ifcapable !wal {finish_test ; return }
#-------------------------------------------------------------------------
# This test case, walfault-1-*, simulates faults while executing a
#
# PRAGMA journal_mode = WAL;
#
# statement immediately after creating a new database.
#
do_test walfault-1-pre-1 {
faultsim_delete_and_reopen
faultsim_save_and_close
} {}
do_faultsim_test walfault-1 -prep {
faultsim_restore_and_reopen
} -body {
db eval { PRAGMA main.journal_mode = WAL }
} -test {
faultsim_test_result {0 wal}
# Test that the connection that encountered an error as part of
# "PRAGMA journal_mode = WAL" and a new connection use the same
# journal mode when accessing the database.
#
# If "PRAGMA journal_mode" is executed immediately, connection [db] (the
# one that hit the error in journal_mode="WAL") might return "wal" even
# if it failed to switch the database to WAL mode. This is not considered
# a problem. When it tries to read the database, connection [db] correctly
# recognizes that it is a rollback database and switches back to a
# rollback compatible journal mode.
#
set jm [db one {SELECT * FROM sqlite_master ; PRAGMA main.journal_mode}]
sqlite3 db2 test.db
set jm2 [db2 one {SELECT * FROM sqlite_master ; PRAGMA main.journal_mode}]
db2 close
if { $jm!=$jm2 } { error "Journal modes do not match: $jm $jm2" }
if { $testrc==0 && $jm!="wal" } { error "Journal mode is not WAL" }
}
#--------------------------------------------------------------------------
# Test case walfault-2-* tests fault injection during recovery of a
# short WAL file (a dozen frames or thereabouts).
#
do_test walfault-2-pre-1 {
sqlite3 db test.db
execsql {
PRAGMA journal_mode = WAL;
BEGIN;
CREATE TABLE x(y, z, UNIQUE(y, z));
INSERT INTO x VALUES(randomblob(100), randomblob(100));
COMMIT;
PRAGMA wal_checkpoint;
INSERT INTO x SELECT randomblob(100), randomblob(100) FROM x;
INSERT INTO x SELECT randomblob(100), randomblob(100) FROM x;
INSERT INTO x SELECT randomblob(100), randomblob(100) FROM x;
}
execsql {
SELECT count(*) FROM x
}
} {8}
do_test walfault-2-pre-2 {
faultsim_save_and_close
faultsim_restore_and_reopen
execsql { SELECT count(*) FROM x }
} {8}
do_faultsim_test walfault-2 -prep {
faultsim_restore_and_reopen
} -body {
execsql { SELECT count(*) FROM x }
} -test {
faultsim_test_result {0 8}
faultsim_integrity_check
}
#--------------------------------------------------------------------------
# Test fault injection while writing and checkpointing a small WAL file.
#
do_test walfault-3-pre-1 {
sqlite3 db test.db
execsql {
PRAGMA auto_vacuum = 1;
PRAGMA journal_mode = WAL;
CREATE TABLE abc(a PRIMARY KEY);
INSERT INTO abc VALUES(randomblob(1500));
}
db close
faultsim_save_and_close
} {}
do_faultsim_test walfault-3 -prep {
faultsim_restore_and_reopen
} -body {
db eval {
DELETE FROM abc;
PRAGMA wal_checkpoint;
}
} -test {
faultsim_test_result {0 {}}
}
#--------------------------------------------------------------------------
#
faultsim_delete_and_reopen
faultsim_save_and_close
do_faultsim_test walfault-4 -prep {
faultsim_restore_and_reopen
} -body {
execsql {
PRAGMA journal_mode = WAL;
CREATE TABLE t1(a PRIMARY KEY, b);
INSERT INTO t1 VALUES('a', 'b');
PRAGMA wal_checkpoint;
SELECT * FROM t1;
}
} -test {
faultsim_test_result {0 {wal a b}}
faultsim_integrity_check
}
#--------------------------------------------------------------------------
#
do_test walfault-5-pre-1 {
faultsim_delete_and_reopen
execsql {
PRAGMA page_size = 512;
PRAGMA journal_mode = WAL;
}
faultsim_save_and_close
} {}
do_faultsim_test walfault-5 -faults shmerr* -prep {
faultsim_restore_and_reopen
execsql { PRAGMA wal_autocheckpoint = 0 }
shmfault filter xShmSize
} -body {
execsql {
CREATE TABLE t1(x);
BEGIN;
INSERT INTO t1 VALUES(randomblob(400)); /* 1 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 2 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 4 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 8 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 16 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 32 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 64 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 128 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 256 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 512 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 1024 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 2048 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 4096 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 8192 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 16384 */
COMMIT;
SELECT count(*) FROM t1;
}
} -test {
faultsim_test_result {0 16384}
faultsim_integrity_check
}
#--------------------------------------------------------------------------
#
do_test walfault-6-pre-1 {
faultsim_delete_and_reopen
execsql {
PRAGMA page_size = 512;
PRAGMA journal_mode = WAL;
PRAGMA wal_autocheckpoint = 0;
CREATE TABLE t1(x);
BEGIN;
INSERT INTO t1 VALUES(randomblob(400)); /* 1 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 2 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 4 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 8 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 16 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 32 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 64 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 128 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 256 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 512 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 1024 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 2048 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 4096 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 8192 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 16384 */
COMMIT;
}
faultsim_save_and_close
} {}
do_faultsim_test walfault-6 -faults shmerr* -prep {
faultsim_restore_and_reopen
shmfault filter xShmSize
} -body {
execsql { SELECT count(*) FROM t1 }
} -test {
faultsim_test_result {0 16384}
faultsim_integrity_check
set n [db one {SELECT count(*) FROM t1}]
if {$n != 16384 && $n != 0} { error "Incorrect number of rows: $n" }
}
#--------------------------------------------------------------------------
#
do_test walfault-7-pre-1 {
faultsim_delete_and_reopen
execsql {
PRAGMA page_size = 512;
PRAGMA journal_mode = WAL;
PRAGMA wal_autocheckpoint = 0;
CREATE TABLE t1(x);
BEGIN;
INSERT INTO t1 VALUES(randomblob(400)); /* 1 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 2 */
INSERT INTO t1 SELECT randomblob(400) FROM t1; /* 4 */
COMMIT;
}
faultsim_save_and_close
} {}
do_faultsim_test walfault-7 -prep {
faultsim_restore_and_reopen
} -body {
execsql { SELECT count(*) FROM t1 }
} -test {
faultsim_test_result {0 4}
set n [db one {SELECT count(*) FROM t1}]
if {$n != 4 && $n != 0} { error "Incorrect number of rows: $n" }
}
#--------------------------------------------------------------------------
#
do_test walfault-8-pre-1 {
faultsim_delete_and_reopen
execsql {
PRAGMA journal_mode = WAL;
CREATE TABLE abc(a PRIMARY KEY);
INSERT INTO abc VALUES(randomblob(900));
}
faultsim_save_and_close
} {}
do_faultsim_test walfault-8 -prep {
faultsim_restore_and_reopen
execsql { PRAGMA cache_size = 10 }
} -body {
execsql {
BEGIN;
INSERT INTO abc SELECT randomblob(900) FROM abc; /* 1 */
--INSERT INTO abc SELECT randomblob(900) FROM abc; /* 2 */
--INSERT INTO abc SELECT randomblob(900) FROM abc; /* 4 */
--INSERT INTO abc SELECT randomblob(900) FROM abc; /* 8 */
ROLLBACK;
SELECT count(*) FROM abc;
}
} -test {
faultsim_test_result {0 1}
faultsim_integrity_check
catch { db eval ROLLBACK }
faultsim_integrity_check
set n [db one {SELECT count(*) FROM abc}]
if {$n != 1} { error "Incorrect number of rows: $n" }
}
#--------------------------------------------------------------------------
#
do_test walfault-9-pre-1 {
faultsim_delete_and_reopen
execsql {
PRAGMA journal_mode = WAL;
CREATE TABLE abc(a PRIMARY KEY);
INSERT INTO abc VALUES(randomblob(900));
}
faultsim_save_and_close
} {}
do_faultsim_test walfault-9 -prep {
#if {$iFail<73} { set iFail 73 }
#if {$iFail>73} { exit }
faultsim_restore_and_reopen
execsql { PRAGMA cache_size = 10 }
} -body {
execsql {
BEGIN;
INSERT INTO abc SELECT randomblob(900) FROM abc; /* 1 */
SAVEPOINT spoint;
INSERT INTO abc SELECT randomblob(900) FROM abc; /* 2 */
INSERT INTO abc SELECT randomblob(900) FROM abc; /* 4 */
INSERT INTO abc SELECT randomblob(900) FROM abc; /* 8 */
ROLLBACK TO spoint;
COMMIT;
SELECT count(*) FROM abc;
}
} -test {
faultsim_test_result {0 2}
faultsim_integrity_check
catch { db eval { ROLLBACK TO spoint } }
catch { db eval { COMMIT } }
set n [db one {SELECT count(*) FROM abc}]
if {$n != 1 && $n != 2} { error "Incorrect number of rows: $n" }
}
#-------------------------------------------------------------------------
# When a database is checkpointed, SQLite does the following:
#
# 1. xShmLock(CHECKPOINT) to lock the WAL.
# 2. xShmGet(-1) to get a mapping to read the wal-index header.
# 3. If the mapping obtained in (2) is not large enough to cover the
# entire wal-index, call xShmGet(nReq) to get a larger mapping.
# 4. Do the checkpoint.
# 5. Release the lock and mapping.
#
# This test case tests the outcome of an IO error in step 2.
#
proc walfault_10_vfs_cb {method args} {
switch -- $::shm_state {
0 { return SQLITE_OK }
1 {
if {$method == "xShmGet"} {
set ::wal_index [tvfs shm [lindex $args 0]]
tvfs shm [lindex $args 0] [string range $::wal_index 0 65535]
set ::shm_state 2
}
}
2 {
if {$method == "xShmGet"} {
tvfs shm [lindex $args 0] $::wal_index
return SQLITE_IOERR
}
}
}
return SQLITE_OK
}
do_test walfault-10.1 {
set ::shm_state 0
testvfs tvfs
tvfs script walfault_10_vfs_cb
sqlite3 db test.db -vfs tvfs
sqlite3 db2 test.db -vfs tvfs
execsql {
PRAGMA journal_mode = WAL;
PRAGMA wal_autocheckpoint = 0;
CREATE TABLE t1(x);
INSERT INTO t1 VALUES(randomblob(900));
}
} {wal 0}
do_test walfault-10.2 {
execsql {
PRAGMA wal_autocheckpoint = 0;
BEGIN;
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2 */
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4 */
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8 */
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16 */
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 32 */
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 64 */
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 128 */
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 256 */
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 512 */
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 1024 */
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2048 */
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4096 */
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8192 */
INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16384 */
COMMIT;
} db2
} {0}
do_test walfault-10.3 {
set ::shm_state 1
catchsql { PRAGMA wal_checkpoint } db2
} {1 {disk I/O error}}
set ::shm_state 0
db close
db2 close
tvfs delete
unset -nocomplain ::wal_index ::shm_state
finish_test