xref: /sqlite-3.40.0/test/ioerr5.test (revision 28bbd223)
1# 2008 May 12
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11#
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).
16#
17# $Id: ioerr5.test,v 1.2 2008/05/15 09:07:56 danielk1977 Exp $
18
19set testdir [file dirname $argv0]
20source $testdir/tester.tcl
21
22ifcapable !memorymanage||!shared_cache {
23  finish_test
24  return
25}
26
27db close
28
29set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
30set ::soft_limit [sqlite3_soft_heap_limit 1048576]
31
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.
36#
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.
40#
41proc dosql16 {zSql {db db}} {
42  set sql [encoding convertto unicode $zSql]
43  append sql "\00\00"
44  set stmt [sqlite3_prepare16 $db $sql -1 {}]
45  sqlite3_step $stmt
46  set rc [sqlite3_finalize $stmt]
47}
48
49proc compilesql16 {zSql {db db}} {
50  set sql [encoding convertto unicode $zSql]
51  append sql "\00\00"
52  set stmt [sqlite3_prepare16 $db $sql -1 {}]
53  set rc [sqlite3_finalize $stmt]
54}
55
56# Open two database connections (handle db and db2) to database "test.db".
57#
58proc opendatabases {} {
59  catch {db close}
60  catch {db2 close}
61  sqlite3 db test.db
62  sqlite3 db2 test.db
63  db2 cache size 0
64  db cache size 0
65  execsql {
66    pragma page_size=512;
67    pragma auto_vacuum=2;
68    pragma cache_size=16;
69  }
70}
71
72# Open two database connections and create a single table in the db.
73#
74do_test ioerr5-1.0 {
75  opendatabases
76  execsql { CREATE TABLE A(Id INTEGER, Name TEXT) }
77} {}
78
79foreach locking_mode {normal exclusive} {
80  for {set iFail 1} {$iFail<200} {incr iFail} {
81    sqlite3_soft_heap_limit 1048576
82    opendatabases
83    execsql { pragma locking_mode=exclusive }
84    set nRow [db one {SELECT count(*) FROM a}]
85
86    # Dirty (at least) one of the pages in the cache.
87    do_test ioerr5-$locking_mode-$iFail.1 {
88      execsql {
89        BEGIN EXCLUSIVE;
90        INSERT INTO a VALUES(1, 'ABCDEFGHIJKLMNOP');
91      }
92    } {}
93
94    # Now try to commit the transaction. Cause an IO error to occur
95    # within this operation, which moves the pager into the error state.
96    #
97    set ::sqlite_io_error_persist 1
98    set ::sqlite_io_error_pending $iFail
99    do_test ioerr5-$locking_mode-$iFail.2 {
100      set rc [catchsql {COMMIT}]
101      list
102    } {}
103    set ::sqlite_io_error_hit 0
104    set ::sqlite_io_error_persist 0
105    set ::sqlite_io_error_pending 0
106
107    # Read the contents of the database file into a Tcl variable.
108    #
109    set fd [open test.db]
110    fconfigure $fd -translation binary -encoding binary
111    set zDatabase [read $fd]
112    close $fd
113
114    # Set a very low soft-limit and then try to compile an SQL statement
115    # from UTF-16 text. To do this, SQLite will need to reclaim memory
116    # from the pager that is in error state. Including that associated
117    # with the dirty page.
118    #
119    do_test ioerr5-$locking_mode-$iFail.3 {
120      sqlite3_soft_heap_limit 1024
121      compilesql16 "SELECT 10"
122      set bt [btree_from_db db]
123      array set stats [btree_pager_stats $bt]
124      set stats(page)
125    } {0}
126
127    # Ensure that nothing was written to the database while reclaiming
128    # memory from the pager in error state.
129    #
130    do_test ioerr5-$locking_mode-$iFail.4 {
131      set fd [open test.db]
132      fconfigure $fd -translation binary -encoding binary
133      set zDatabase2 [read $fd]
134      close $fd
135      expr {$zDatabase eq $zDatabase2}
136    } {1}
137
138    if {$rc eq [list 0 {}]} {
139      do_test ioerr5-$locking_mode-$iFail.3 {
140        execsql { SELECT count(*) FROM a }
141      } [expr $nRow+1]
142      break
143    }
144  }
145}
146
147# Make sure this test script doesn't leave any files open.
148#
149do_test ioerr5-2.X {
150  catch { db close }
151  catch { db2 close }
152  set sqlite_open_file_count
153} 0
154
155sqlite3_enable_shared_cache $::enable_shared_cache
156sqlite3_soft_heap_limit $::soft_limit
157
158finish_test
159
160