1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/tkt2409.test Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,229 @@
1.4 +# 2007 June 13
1.5 +#
1.6 +# The author disclaims copyright to this source code. In place of
1.7 +# a legal notice, here is a blessing:
1.8 +#
1.9 +# May you do good and not evil.
1.10 +# May you find forgiveness for yourself and forgive others.
1.11 +# May you share freely, never taking more than you give.
1.12 +#
1.13 +#***********************************************************************
1.14 +# This file implements regression tests for SQLite library.
1.15 +#
1.16 +# This file implements tests to verify that ticket #2409 has been
1.17 +# fixed. More specifically, they verify that if SQLite cannot
1.18 +# obtain an EXCLUSIVE lock while trying to spill the cache during
1.19 +# any statement other than a COMMIT, an I/O error is returned instead
1.20 +# of SQLITE_BUSY.
1.21 +#
1.22 +# $Id: tkt2409.test,v 1.6 2008/08/28 17:46:19 drh Exp $
1.23 +
1.24 +# Test Outline:
1.25 +#
1.26 +# tkt-2409-1.*: Cause a cache-spill during an INSERT that is within
1.27 +# a db transaction but does not start a statement transaction.
1.28 +# Verify that the transaction is automatically rolled back
1.29 +# and SQLITE_IOERR_BLOCKED is returned
1.30 +#
1.31 +# UPDATE: As of the pcache modifications, failing to upgrade to
1.32 +# an exclusive lock when attempting a cache-spill is no longer an
1.33 +# error. The pcache module allocates more space and keeps working
1.34 +# in memory if this occurs.
1.35 +#
1.36 +# tkt-2409-2.*: Cause a cache-spill while updating the change-counter
1.37 +# during a database COMMIT. Verify that the transaction is not
1.38 +# rolled back and SQLITE_BUSY is returned.
1.39 +#
1.40 +# tkt-2409-3.*: Similar to 2409-1.*, but using many INSERT statements
1.41 +# within a transaction instead of just one.
1.42 +#
1.43 +# UPDATE: Again, pcache now just keeps working in main memory.
1.44 +#
1.45 +# tkt-2409-4.*: Similar to 2409-1.*, but rig it so that the
1.46 +# INSERT statement starts a statement transaction. Verify that
1.47 +# SQLITE_BUSY is returned and the transaction is not rolled back.
1.48 +#
1.49 +# UPDATE: This time, SQLITE_BUSY is not returned. pcache just uses
1.50 +# more malloc()'d memory.
1.51 +#
1.52 +
1.53 +set testdir [file dirname $argv0]
1.54 +source $testdir/tester.tcl
1.55 +
1.56 +ifcapable !pager_pragmas {
1.57 + finish_test
1.58 + return
1.59 +}
1.60 +
1.61 +sqlite3_extended_result_codes $::DB 1
1.62 +
1.63 +# Aquire a read-lock on the database using handle [db2].
1.64 +#
1.65 +proc read_lock_db {} {
1.66 + if {$::STMT eq ""} {
1.67 + set ::STMT [sqlite3_prepare db2 {SELECT rowid FROM sqlite_master} -1 TAIL]
1.68 + set rc [sqlite3_step $::STMT]
1.69 + if {$rc eq "SQLITE_ERROR"} {
1.70 + unread_lock_db
1.71 + read_lock_db
1.72 + }
1.73 + }
1.74 +}
1.75 +
1.76 +# Release any read-lock obtained using [read_lock_db]
1.77 +#
1.78 +proc unread_lock_db {} {
1.79 + if {$::STMT ne ""} {
1.80 + sqlite3_finalize $::STMT
1.81 + set ::STMT ""
1.82 + }
1.83 +}
1.84 +
1.85 +# Open the db handle used by [read_lock_db].
1.86 +#
1.87 +sqlite3 db2 test.db
1.88 +set ::STMT ""
1.89 +
1.90 +do_test tkt2409-1.1 {
1.91 + execsql {
1.92 + PRAGMA cache_size=10;
1.93 + CREATE TABLE t1(x TEXT UNIQUE NOT NULL, y BLOB);
1.94 + }
1.95 + read_lock_db
1.96 + set ::zShort [string repeat 0123456789 1]
1.97 + set ::zLong [string repeat 0123456789 1500]
1.98 + catchsql {
1.99 + BEGIN;
1.100 + INSERT INTO t1 VALUES($::zShort, $::zLong);
1.101 + }
1.102 +} {0 {}}
1.103 +
1.104 +do_test tkt2409-1.2 {
1.105 + sqlite3_errcode $::DB
1.106 +} {SQLITE_OK}
1.107 +
1.108 +# Check the integrity of the cache.
1.109 +#
1.110 +integrity_check tkt2409-1.3
1.111 +
1.112 +# Check that the transaction was rolled back. Because the INSERT
1.113 +# statement in which the "I/O error" occured did not open a statement
1.114 +# transaction, SQLite had no choice but to roll back the transaction.
1.115 +#
1.116 +do_test tkt2409-1.4 {
1.117 + unread_lock_db
1.118 + catchsql { ROLLBACK }
1.119 +} {0 {}}
1.120 +
1.121 +set ::zShort [string repeat 0123456789 1]
1.122 +set ::zLong [string repeat 0123456789 1500]
1.123 +set ::rc 1
1.124 +for {set iCache 10} {$::rc} {incr iCache} {
1.125 + execsql "PRAGMA cache_size = $iCache"
1.126 + do_test tkt2409-2.1.$iCache {
1.127 + read_lock_db
1.128 + set ::rc [catch {
1.129 + execsql {
1.130 + BEGIN;
1.131 + INSERT INTO t1 VALUES($::zShort, $::zLong);
1.132 + }
1.133 + } msg]
1.134 + expr {($::rc == 1 && $msg eq "disk I/O error") || $::rc == 0}
1.135 + } {1}
1.136 +}
1.137 +
1.138 +do_test tkt2409-2.2 {
1.139 + catchsql {
1.140 + ROLLBACK;
1.141 + BEGIN;
1.142 + INSERT INTO t1 VALUES($::zShort, $::zLong);
1.143 + COMMIT;
1.144 + }
1.145 +} {1 {database is locked}}
1.146 +
1.147 +do_test tkt2409-2.3 {
1.148 + unread_lock_db
1.149 + catchsql {
1.150 + COMMIT;
1.151 + }
1.152 +} {0 {}}
1.153 +
1.154 +
1.155 +do_test tkt2409-3.1 {
1.156 + db close
1.157 + set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
1.158 + sqlite3_extended_result_codes $::DB 1
1.159 + execsql {
1.160 + PRAGMA cache_size=10;
1.161 + DELETE FROM t1;
1.162 + }
1.163 + read_lock_db
1.164 + set ::zShort [string repeat 0123456789 1]
1.165 + set ::zLong [string repeat 0123456789 1500]
1.166 + catchsql {
1.167 + BEGIN;
1.168 + INSERT INTO t1 SELECT $::zShort, $::zLong;
1.169 + }
1.170 +} {0 {}}
1.171 +
1.172 +do_test tkt2409-3.2 {
1.173 + sqlite3_errcode $::DB
1.174 +} {SQLITE_OK}
1.175 +
1.176 +# Check the integrity of the cache.
1.177 +#
1.178 +integrity_check tkt2409-3.3
1.179 +
1.180 +# Check that the transaction was rolled back. Because the INSERT
1.181 +# statement in which the "I/O error" occured did not open a statement
1.182 +# transaction, SQLite had no choice but to roll back the transaction.
1.183 +#
1.184 +do_test tkt2409-3.4 {
1.185 + unread_lock_db
1.186 + catchsql { ROLLBACK }
1.187 +} {0 {}}
1.188 +integrity_check tkt2409-3.5
1.189 +
1.190 +expr {srand(1)}
1.191 +do_test tkt2409-4.1 {
1.192 + execsql {
1.193 + PRAGMA cache_size=20;
1.194 + DROP TABLE t1;
1.195 + CREATE TABLE t1 (x TEXT UNIQUE NOT NULL);
1.196 + }
1.197 +
1.198 + unset -nocomplain t1
1.199 + array unset t1
1.200 + set t1(0) 1
1.201 + set sql ""
1.202 + for {set i 0} {$i<5000} {incr i} {
1.203 + set r 0
1.204 + while {[info exists t1($r)]} {
1.205 + set r [expr {int(rand()*1000000000)}]
1.206 + }
1.207 + set t1($r) 1
1.208 + append sql "INSERT INTO t1 VALUES('some-text-$r');"
1.209 + }
1.210 +
1.211 + read_lock_db
1.212 + execsql BEGIN
1.213 + catchsql $sql
1.214 +} {0 {}}
1.215 +
1.216 +do_test tkt2409-4.2 {
1.217 + sqlite3_errcode $::DB
1.218 +} {SQLITE_OK}
1.219 +
1.220 +# Check the integrity of the cache.
1.221 +#
1.222 +integrity_check tkt2409-4.3
1.223 +
1.224 +do_test tkt2409-4.4 {
1.225 + catchsql { ROLLBACK }
1.226 +} {0 {}}
1.227 +integrity_check tkt2409-4.5
1.228 +
1.229 +unread_lock_db
1.230 +db2 close
1.231 +unset -nocomplain t1
1.232 +finish_test