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 #***********************************************************************
12 # This file tests that if sqlite3_release_memory() is called to reclaim
13 # memory from a pager that is in the error-state, SQLite does not
14 # incorrectly write dirty pages out to the database (not safe to do
15 # once the pager is in error state).
17 # $Id: ioerr5.test,v 1.5 2008/08/28 18:35:34 danielk1977 Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 ifcapable !memorymanage||!shared_cache {
29 set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
30 set ::soft_limit [sqlite3_soft_heap_limit 1048576]
32 # This procedure prepares, steps and finalizes an SQL statement via the
33 # UTF-16 APIs. The text representation of an SQLite error code is returned
34 # ("SQLITE_OK", "SQLITE_IOERR" etc.). The actual results returned by the
35 # SQL statement, if it is a SELECT, are not available.
37 # This can be useful for testing because it forces SQLite to make an extra
38 # call to sqlite3_malloc() when translating from the supplied UTF-16 to
39 # the UTF-8 encoding used internally.
41 proc dosql16 {zSql {db db}} {
42 set sql [encoding convertto unicode $zSql]
44 set stmt [sqlite3_prepare16 $db $sql -1 {}]
46 set rc [sqlite3_finalize $stmt]
49 proc compilesql16 {zSql {db db}} {
50 set sql [encoding convertto unicode $zSql]
52 set stmt [sqlite3_prepare16 $db $sql -1 {}]
53 set rc [sqlite3_finalize $stmt]
56 # Open two database connections (handle db and db2) to database "test.db".
58 proc opendatabases {} {
72 # Open two database connections and create a single table in the db.
76 execsql { CREATE TABLE A(Id INTEGER, Name TEXT) }
79 foreach locking_mode {normal exclusive} {
81 for {set iFail 1} {$iFail<200} {incr iFail} {
82 sqlite3_soft_heap_limit 1048576
84 execsql { pragma locking_mode=exclusive }
85 set nRow [db one {SELECT count(*) FROM a}]
87 # Dirty (at least) one of the pages in the cache.
88 do_test ioerr5-1.$locking_mode-$iFail.1 {
91 INSERT INTO a VALUES(1, 'ABCDEFGHIJKLMNOP');
95 # Open a read-only cursor on table "a". If the COMMIT below is
96 # interrupted by a persistent IO error, the pager will transition to
97 # PAGER_ERROR state. If there are no other read-only cursors open,
98 # from there the pager immediately discards all cached data and
99 # switches to PAGER_OPEN state. This read-only cursor stops that
100 # from happening, leaving the pager stuck in PAGER_ERROR state.
102 set channel [db incrblob -readonly a Name [db last_insert_rowid]]
104 # Now try to commit the transaction. Cause an IO error to occur
105 # within this operation, which moves the pager into the error state.
107 set ::sqlite_io_error_persist 1
108 set ::sqlite_io_error_pending $iFail
109 do_test ioerr5-1.$locking_mode-$iFail.2 {
110 set rc [catchsql {COMMIT}]
113 set ::sqlite_io_error_hit 0
114 set ::sqlite_io_error_persist 0
115 set ::sqlite_io_error_pending 0
117 # Read the contents of the database file into a Tcl variable.
119 set fd [open test.db]
120 fconfigure $fd -translation binary -encoding binary
121 set zDatabase [read $fd]
124 # Set a very low soft-limit and then try to compile an SQL statement
125 # from UTF-16 text. To do this, SQLite will need to reclaim memory
126 # from the pager that is in error state. Including that associated
127 # with the dirty page.
129 do_test ioerr5-1.$locking_mode-$iFail.3 {
130 sqlite3_soft_heap_limit 1024
131 compilesql16 "SELECT 10"
136 # Ensure that nothing was written to the database while reclaiming
137 # memory from the pager in error state.
139 do_test ioerr5-1.$locking_mode-$iFail.4 {
140 set fd [open test.db]
141 fconfigure $fd -translation binary -encoding binary
142 set zDatabase2 [read $fd]
144 expr {$zDatabase eq $zDatabase2}
147 if {$rc eq [list 0 {}]} {
148 do_test ioerr5.1-$locking_mode-$iFail.3 {
149 execsql { SELECT count(*) FROM a }
156 # Make sure this test script doesn't leave any files open.
161 set sqlite_open_file_count
166 execsql { CREATE INDEX i1 ON a(id, name); }
169 foreach locking_mode {exclusive normal} {
170 for {set iFail 1} {$iFail<200} {incr iFail} {
171 sqlite3_soft_heap_limit 1048576
173 execsql { pragma locking_mode=exclusive }
174 set nRow [db one {SELECT count(*) FROM a}]
176 do_test ioerr5-2.$locking_mode-$iFail.1 {
179 INSERT INTO a VALUES(1, 'ABCDEFGHIJKLMNOP');
183 set ::sqlite_io_error_persist 1
184 set ::sqlite_io_error_pending $iFail
186 sqlite3_release_memory 10000
188 set error_hit $::sqlite_io_error_hit
189 set ::sqlite_io_error_hit 0
190 set ::sqlite_io_error_persist 0
191 set ::sqlite_io_error_pending 0
193 do_test ioerr5-2.$locking_mode-$iFail.3a {
195 } {1 {disk I/O error}}
197 do_test ioerr5-2.$locking_mode-$iFail.3b {
205 # Make sure this test script doesn't leave any files open.
210 set sqlite_open_file_count
213 sqlite3_enable_shared_cache $::enable_shared_cache
214 sqlite3_soft_heap_limit $::soft_limit