3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This file implements tests to verify that ticket #2409 has been
14 # fixed. More specifically, they verify that if SQLite cannot
15 # obtain an EXCLUSIVE lock while trying to spill the cache during
16 # any statement other than a COMMIT, an I/O error is returned instead
19 # $Id: tkt2409.test,v 1.6 2008/08/28 17:46:19 drh Exp $
23 # tkt-2409-1.*: Cause a cache-spill during an INSERT that is within
24 # a db transaction but does not start a statement transaction.
25 # Verify that the transaction is automatically rolled back
26 # and SQLITE_IOERR_BLOCKED is returned
28 # UPDATE: As of the pcache modifications, failing to upgrade to
29 # an exclusive lock when attempting a cache-spill is no longer an
30 # error. The pcache module allocates more space and keeps working
31 # in memory if this occurs.
33 # tkt-2409-2.*: Cause a cache-spill while updating the change-counter
34 # during a database COMMIT. Verify that the transaction is not
35 # rolled back and SQLITE_BUSY is returned.
37 # tkt-2409-3.*: Similar to 2409-1.*, but using many INSERT statements
38 # within a transaction instead of just one.
40 # UPDATE: Again, pcache now just keeps working in main memory.
42 # tkt-2409-4.*: Similar to 2409-1.*, but rig it so that the
43 # INSERT statement starts a statement transaction. Verify that
44 # SQLITE_BUSY is returned and the transaction is not rolled back.
46 # UPDATE: This time, SQLITE_BUSY is not returned. pcache just uses
47 # more malloc()'d memory.
50 set testdir [file dirname $argv0]
51 source $testdir/tester.tcl
53 ifcapable !pager_pragmas {
58 sqlite3_extended_result_codes $::DB 1
60 # Aquire a read-lock on the database using handle [db2].
62 proc read_lock_db {} {
64 set ::STMT [sqlite3_prepare db2 {SELECT rowid FROM sqlite_master} -1 TAIL]
65 set rc [sqlite3_step $::STMT]
66 if {$rc eq "SQLITE_ERROR"} {
73 # Release any read-lock obtained using [read_lock_db]
75 proc unread_lock_db {} {
77 sqlite3_finalize $::STMT
82 # Open the db handle used by [read_lock_db].
90 CREATE TABLE t1(x TEXT UNIQUE NOT NULL, y BLOB);
93 set ::zShort [string repeat 0123456789 1]
94 set ::zLong [string repeat 0123456789 1500]
97 INSERT INTO t1 VALUES($::zShort, $::zLong);
101 do_test tkt2409-1.2 {
102 sqlite3_errcode $::DB
105 # Check the integrity of the cache.
107 integrity_check tkt2409-1.3
109 # Check that the transaction was rolled back. Because the INSERT
110 # statement in which the "I/O error" occurred did not open a statement
111 # transaction, SQLite had no choice but to roll back the transaction.
113 do_test tkt2409-1.4 {
115 catchsql { ROLLBACK }
118 set ::zShort [string repeat 0123456789 1]
119 set ::zLong [string repeat 0123456789 1500]
121 for {set iCache 10} {$::rc} {incr iCache} {
122 execsql "PRAGMA cache_size = $iCache"
123 do_test tkt2409-2.1.$iCache {
128 INSERT INTO t1 VALUES($::zShort, $::zLong);
131 expr {($::rc == 1 && $msg eq "disk I/O error") || $::rc == 0}
135 do_test tkt2409-2.2 {
139 INSERT INTO t1 VALUES($::zShort, $::zLong);
142 } {1 {database is locked}}
144 do_test tkt2409-2.3 {
152 do_test tkt2409-3.1 {
154 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
155 sqlite3_extended_result_codes $::DB 1
157 PRAGMA cache_size=10;
161 set ::zShort [string repeat 0123456789 1]
162 set ::zLong [string repeat 0123456789 1500]
165 INSERT INTO t1 SELECT $::zShort, $::zLong;
169 do_test tkt2409-3.2 {
170 sqlite3_errcode $::DB
173 # Check the integrity of the cache.
175 integrity_check tkt2409-3.3
177 # Check that the transaction was rolled back. Because the INSERT
178 # statement in which the "I/O error" occurred did not open a statement
179 # transaction, SQLite had no choice but to roll back the transaction.
181 do_test tkt2409-3.4 {
183 catchsql { ROLLBACK }
185 integrity_check tkt2409-3.5
188 do_test tkt2409-4.1 {
190 PRAGMA cache_size=20;
192 CREATE TABLE t1 (x TEXT UNIQUE NOT NULL);
199 for {set i 0} {$i<5000} {incr i} {
201 while {[info exists t1($r)]} {
202 set r [expr {int(rand()*1000000000)}]
205 append sql "INSERT INTO t1 VALUES('some-text-$r');"
213 do_test tkt2409-4.2 {
214 sqlite3_errcode $::DB
217 # Check the integrity of the cache.
219 integrity_check tkt2409-4.3
221 do_test tkt2409-4.4 {
222 catchsql { ROLLBACK }
224 integrity_check tkt2409-4.5