1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-30 19:03:16 +03:00

Add some further tests and a bugfix for the atomic-write optimization. (CVS 4276)

FossilOrigin-Name: 5f0fb894f44069c4aa9b8dba62b4d8a262c991de
This commit is contained in:
danielk1977
2007-08-23 11:07:10 +00:00
parent 2ca0f86354
commit f8940aefb6
7 changed files with 190 additions and 16 deletions

100
test/crash3.test Normal file
View File

@ -0,0 +1,100 @@
# 2007 August 23
#
# 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.
#
#***********************************************************************
#
# $Id: crash3.test,v 1.1 2007/08/23 11:07:10 danielk1977 Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
ifcapable !crashtest {
finish_test
return
}
proc do_test2 {name tcl res1 res2} {
set script [subst -nocommands {
do_test $name {
set res1 {$res1}
set res2 {$res2}
set res [eval {$tcl}]
if {[set res] eq [set res1] || [set res] eq [set res2]} {
set res "{[set res1]} or {[set res2]}"
}
set res
} {{$res1} or {$res2}}
}]
uplevel $script
}
# Each iteration of the following loop sets up the database to contain
# the following schema and data:
#
# CREATE TABLE abc(a, b, c);
# INSERT INTO abc VALUES(1, 2, 3);
#
# Then execute the SQL statement, scheduling a crash for part-way through
# the first sync() of either the database file or the journal file (often
# the journal file is not required - meaning no crash occurs).
#
# After the crash (or absence of a crash), open the database and
# verify that:
#
# * The integrity check passes, and
# * The contents of table abc is either {1 2 3} or the value specified
# to the right of the SQL statement below.
#
# The procedure is repeated 10 times for each SQL statement. Five times
# with the crash scheduled for midway through the first journal sync (if
# any), and five times with the crash midway through the database sync.
#
set tn 1
foreach {sql res2} [list \
{INSERT INTO abc VALUES(4, 5, 6)} {1 2 3 4 5 6} \
{DELETE FROM abc} {} \
{INSERT INTO abc SELECT * FROM abc} {1 2 3 1 2 3} \
{UPDATE abc SET a = 2} {2 2 3} \
{INSERT INTO abc VALUES(4, 5, randstr(1000,1000))} {n/a} \
{CREATE TABLE def(d, e, f)} {n/a} \
] {
for {set ii 0} {$ii < 10} {incr ii} {
db close
file delete -force test.db test.db-journal
sqlite3 db test.db
do_test crash3-1.$tn.1 {
execsql {
BEGIN;
CREATE TABLE abc(a, b, c);
INSERT INTO abc VALUES(1, 2, 3);
COMMIT;
}
} {}
db close
set crashfile test.db
if {($ii%2)==0} { append crashfile -journal }
set rand "SELECT randstr($tn,$tn);"
do_test crash3-1.$tn.2 [subst {
crashsql -file $crashfile -char atomic {$rand $sql}
sqlite3 db test.db
execsql { PRAGMA integrity_check; }
}] {ok}
do_test2 crash3-1.$tn.3 {
execsql { SELECT * FROM abc }
} {1 2 3} $res2
incr tn
}
}
finish_test

View File

@ -13,7 +13,7 @@
# IO traffic generated by SQLite (making sure SQLite is not writing out
# more database pages than it has to, stuff like that).
#
# $Id: io.test,v 1.3 2007/08/23 08:06:45 danielk1977 Exp $
# $Id: io.test,v 1.4 2007/08/23 11:07:10 danielk1977 Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
@ -265,6 +265,57 @@ do_test io-2.8.3 {
}
} {1 2 3 4 5 6 7 8}
# Test that the atomic write optimisation is not enabled if the sector
# size is larger than the page-size.
#
do_test io-2.9.1 {
sqlite3_simulate_device -char atomic -sectorsize 2048
execsql {
BEGIN;
INSERT INTO abc VALUES(9, 10);
}
file exists test.db-journal
} {1}
do_test io-2.9.2 {
execsql { ROLLBACK; }
db close
file delete -force test.db test.db-journal
sqlite3 db test.db
execsql {
PRAGMA page_size = 2048;
CREATE TABLE abc(a, b);
}
execsql {
BEGIN;
INSERT INTO abc VALUES(9, 10);
}
file exists test.db-journal
} {0}
do_test io-2.9.3 {
execsql { COMMIT }
} {}
# Test a couple of the more specific IOCAP_ATOMIC flags
# (i.e IOCAP_ATOMIC2K etc.).
#
do_test io-2.10.1 {
sqlite3_simulate_device -char atomic1k
execsql {
BEGIN;
INSERT INTO abc VALUES(11, 12);
}
file exists test.db-journal
} {1}
do_test io-2.10.2 {
execsql { ROLLBACK }
sqlite3_simulate_device -char atomic2k
execsql {
BEGIN;
INSERT INTO abc VALUES(11, 12);
}
file exists test.db-journal
} {0}
sqlite3_simulate_device -char {} -sectorsize 0
finish_test

View File

@ -11,7 +11,7 @@
# This file implements some common TCL routines used for regression
# testing the SQLite library
#
# $Id: tester.tcl,v 1.87 2007/08/23 02:47:54 drh Exp $
# $Id: tester.tcl,v 1.88 2007/08/23 11:07:10 danielk1977 Exp $
# Make sure tclsqlite3 was compiled correctly. Abort now with an
# error message if not.
@ -350,7 +350,7 @@ proc ifcapable {expr code {else ""} {elsecode ""}} {
# error message. This is "child process exited abnormally" if the crash
# occured.
#
# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE $sql
# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
#
proc crashsql {args} {
if {$::tcl_platform(platform)!="unix"} {
@ -360,6 +360,7 @@ proc crashsql {args} {
set blocksize ""
set crashdelay 1
set crashfile ""
set dc ""
set sql [lindex $args end]
for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
@ -370,6 +371,7 @@ proc crashsql {args} {
if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
else { error "Unrecognized option: $z" }
}
@ -380,7 +382,7 @@ proc crashsql {args} {
set cfile [file join [pwd] $crashfile]
set f [open crash.tcl w]
puts $f "sqlite3_crashparams $blocksize $crashdelay $cfile"
puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
puts $f "set sqlite_pending_byte $::sqlite_pending_byte"
puts $f "sqlite3 db test.db"