1
0

ioerr5.test 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.5 2008/08/28 18:35:34 danielk1977 Exp $
  18. set testdir [file dirname $argv0]
  19. source $testdir/tester.tcl
  20. ifcapable !memorymanage||!shared_cache {
  21. finish_test
  22. return
  23. }
  24. db close
  25. set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
  26. set ::soft_limit [sqlite3_soft_heap_limit 1048576]
  27. # This procedure prepares, steps and finalizes an SQL statement via the
  28. # UTF-16 APIs. The text representation of an SQLite error code is returned
  29. # ("SQLITE_OK", "SQLITE_IOERR" etc.). The actual results returned by the
  30. # SQL statement, if it is a SELECT, are not available.
  31. #
  32. # This can be useful for testing because it forces SQLite to make an extra
  33. # call to sqlite3_malloc() when translating from the supplied UTF-16 to
  34. # the UTF-8 encoding used internally.
  35. #
  36. proc dosql16 {zSql {db db}} {
  37. set sql [encoding convertto unicode $zSql]
  38. append sql "\00\00"
  39. set stmt [sqlite3_prepare16 $db $sql -1 {}]
  40. sqlite3_step $stmt
  41. set rc [sqlite3_finalize $stmt]
  42. }
  43. proc compilesql16 {zSql {db db}} {
  44. set sql [encoding convertto unicode $zSql]
  45. append sql "\00\00"
  46. set stmt [sqlite3_prepare16 $db $sql -1 {}]
  47. set rc [sqlite3_finalize $stmt]
  48. }
  49. # Open two database connections (handle db and db2) to database "test.db".
  50. #
  51. proc opendatabases {} {
  52. catch {db close}
  53. catch {db2 close}
  54. sqlite3 db test.db
  55. sqlite3 db2 test.db
  56. db2 cache size 0
  57. db cache size 0
  58. execsql {
  59. pragma page_size=512;
  60. pragma auto_vacuum=2;
  61. pragma cache_size=16;
  62. }
  63. }
  64. # Open two database connections and create a single table in the db.
  65. #
  66. do_test ioerr5-1.0 {
  67. opendatabases
  68. execsql { CREATE TABLE A(Id INTEGER, Name TEXT) }
  69. } {}
  70. foreach locking_mode {normal exclusive} {
  71. set nPage 2
  72. for {set iFail 1} {$iFail<200} {incr iFail} {
  73. sqlite3_soft_heap_limit 1048576
  74. opendatabases
  75. execsql { pragma locking_mode=exclusive }
  76. set nRow [db one {SELECT count(*) FROM a}]
  77. # Dirty (at least) one of the pages in the cache.
  78. do_test ioerr5-1.$locking_mode-$iFail.1 {
  79. execsql {
  80. BEGIN EXCLUSIVE;
  81. INSERT INTO a VALUES(1, 'ABCDEFGHIJKLMNOP');
  82. }
  83. } {}
  84. # Open a read-only cursor on table "a". If the COMMIT below is
  85. # interrupted by a persistent IO error, the pager will transition to
  86. # PAGER_ERROR state. If there are no other read-only cursors open,
  87. # from there the pager immediately discards all cached data and
  88. # switches to PAGER_OPEN state. This read-only cursor stops that
  89. # from happening, leaving the pager stuck in PAGER_ERROR state.
  90. #
  91. set channel [db incrblob -readonly a Name [db last_insert_rowid]]
  92. # Now try to commit the transaction. Cause an IO error to occur
  93. # within this operation, which moves the pager into the error state.
  94. #
  95. set ::sqlite_io_error_persist 1
  96. set ::sqlite_io_error_pending $iFail
  97. do_test ioerr5-1.$locking_mode-$iFail.2 {
  98. set rc [catchsql {COMMIT}]
  99. list
  100. } {}
  101. set ::sqlite_io_error_hit 0
  102. set ::sqlite_io_error_persist 0
  103. set ::sqlite_io_error_pending 0
  104. # Read the contents of the database file into a Tcl variable.
  105. #
  106. set fd [open test.db]
  107. fconfigure $fd -translation binary -encoding binary
  108. set zDatabase [read $fd]
  109. close $fd
  110. # Set a very low soft-limit and then try to compile an SQL statement
  111. # from UTF-16 text. To do this, SQLite will need to reclaim memory
  112. # from the pager that is in error state. Including that associated
  113. # with the dirty page.
  114. #
  115. do_test ioerr5-1.$locking_mode-$iFail.3 {
  116. sqlite3_soft_heap_limit 1024
  117. compilesql16 "SELECT 10"
  118. } {SQLITE_OK}
  119. close $channel
  120. # Ensure that nothing was written to the database while reclaiming
  121. # memory from the pager in error state.
  122. #
  123. do_test ioerr5-1.$locking_mode-$iFail.4 {
  124. set fd [open test.db]
  125. fconfigure $fd -translation binary -encoding binary
  126. set zDatabase2 [read $fd]
  127. close $fd
  128. expr {$zDatabase eq $zDatabase2}
  129. } {1}
  130. if {$rc eq [list 0 {}]} {
  131. do_test ioerr5.1-$locking_mode-$iFail.3 {
  132. execsql { SELECT count(*) FROM a }
  133. } [expr $nRow+1]
  134. break
  135. }
  136. }
  137. }
  138. # Make sure this test script doesn't leave any files open.
  139. #
  140. do_test ioerr5-1.X {
  141. catch { db close }
  142. catch { db2 close }
  143. set sqlite_open_file_count
  144. } 0
  145. do_test ioerr5-2.0 {
  146. sqlite3 db test.db
  147. execsql { CREATE INDEX i1 ON a(id, name); }
  148. } {}
  149. foreach locking_mode {exclusive normal} {
  150. for {set iFail 1} {$iFail<200} {incr iFail} {
  151. sqlite3_soft_heap_limit 1048576
  152. opendatabases
  153. execsql { pragma locking_mode=exclusive }
  154. set nRow [db one {SELECT count(*) FROM a}]
  155. do_test ioerr5-2.$locking_mode-$iFail.1 {
  156. execsql {
  157. BEGIN EXCLUSIVE;
  158. INSERT INTO a VALUES(1, 'ABCDEFGHIJKLMNOP');
  159. }
  160. } {}
  161. set ::sqlite_io_error_persist 1
  162. set ::sqlite_io_error_pending $iFail
  163. sqlite3_release_memory 10000
  164. set error_hit $::sqlite_io_error_hit
  165. set ::sqlite_io_error_hit 0
  166. set ::sqlite_io_error_persist 0
  167. set ::sqlite_io_error_pending 0
  168. if {$error_hit} {
  169. do_test ioerr5-2.$locking_mode-$iFail.3a {
  170. catchsql COMMIT
  171. } {1 {disk I/O error}}
  172. } else {
  173. do_test ioerr5-2.$locking_mode-$iFail.3b {
  174. execsql COMMIT
  175. } {}
  176. break
  177. }
  178. }
  179. }
  180. # Make sure this test script doesn't leave any files open.
  181. #
  182. do_test ioerr5-2.X {
  183. catch { db close }
  184. catch { db2 close }
  185. set sqlite_open_file_count
  186. } 0
  187. sqlite3_enable_shared_cache $::enable_shared_cache
  188. sqlite3_soft_heap_limit $::soft_limit
  189. finish_test