tkt2409.test 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # 2007 June 13
  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. # This file implements regression tests for SQLite library.
  12. #
  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
  17. # of SQLITE_BUSY.
  18. #
  19. # $Id: tkt2409.test,v 1.6 2008/08/28 17:46:19 drh Exp $
  20. # Test Outline:
  21. #
  22. # tkt-2409-1.*: Cause a cache-spill during an INSERT that is within
  23. # a db transaction but does not start a statement transaction.
  24. # Verify that the transaction is automatically rolled back
  25. # and SQLITE_IOERR_BLOCKED is returned
  26. #
  27. # UPDATE: As of the pcache modifications, failing to upgrade to
  28. # an exclusive lock when attempting a cache-spill is no longer an
  29. # error. The pcache module allocates more space and keeps working
  30. # in memory if this occurs.
  31. #
  32. # tkt-2409-2.*: Cause a cache-spill while updating the change-counter
  33. # during a database COMMIT. Verify that the transaction is not
  34. # rolled back and SQLITE_BUSY is returned.
  35. #
  36. # tkt-2409-3.*: Similar to 2409-1.*, but using many INSERT statements
  37. # within a transaction instead of just one.
  38. #
  39. # UPDATE: Again, pcache now just keeps working in main memory.
  40. #
  41. # tkt-2409-4.*: Similar to 2409-1.*, but rig it so that the
  42. # INSERT statement starts a statement transaction. Verify that
  43. # SQLITE_BUSY is returned and the transaction is not rolled back.
  44. #
  45. # UPDATE: This time, SQLITE_BUSY is not returned. pcache just uses
  46. # more malloc()'d memory.
  47. #
  48. set testdir [file dirname $argv0]
  49. source $testdir/tester.tcl
  50. ifcapable !pager_pragmas {
  51. finish_test
  52. return
  53. }
  54. sqlite3_extended_result_codes $::DB 1
  55. # Aquire a read-lock on the database using handle [db2].
  56. #
  57. proc read_lock_db {} {
  58. if {$::STMT eq ""} {
  59. set ::STMT [sqlite3_prepare db2 {SELECT rowid FROM sqlite_master} -1 TAIL]
  60. set rc [sqlite3_step $::STMT]
  61. if {$rc eq "SQLITE_ERROR"} {
  62. unread_lock_db
  63. read_lock_db
  64. }
  65. }
  66. }
  67. # Release any read-lock obtained using [read_lock_db]
  68. #
  69. proc unread_lock_db {} {
  70. if {$::STMT ne ""} {
  71. sqlite3_finalize $::STMT
  72. set ::STMT ""
  73. }
  74. }
  75. # Open the db handle used by [read_lock_db].
  76. #
  77. sqlite3 db2 test.db
  78. set ::STMT ""
  79. do_test tkt2409-1.1 {
  80. execsql {
  81. PRAGMA cache_size=10;
  82. CREATE TABLE t1(x TEXT UNIQUE NOT NULL, y BLOB);
  83. }
  84. read_lock_db
  85. set ::zShort [string repeat 0123456789 1]
  86. set ::zLong [string repeat 0123456789 1500]
  87. catchsql {
  88. BEGIN;
  89. INSERT INTO t1 VALUES($::zShort, $::zLong);
  90. }
  91. } {0 {}}
  92. do_test tkt2409-1.2 {
  93. sqlite3_errcode $::DB
  94. } {SQLITE_OK}
  95. # Check the integrity of the cache.
  96. #
  97. integrity_check tkt2409-1.3
  98. # Check that the transaction was rolled back. Because the INSERT
  99. # statement in which the "I/O error" occurred did not open a statement
  100. # transaction, SQLite had no choice but to roll back the transaction.
  101. #
  102. do_test tkt2409-1.4 {
  103. unread_lock_db
  104. catchsql { ROLLBACK }
  105. } {0 {}}
  106. set ::zShort [string repeat 0123456789 1]
  107. set ::zLong [string repeat 0123456789 1500]
  108. set ::rc 1
  109. for {set iCache 10} {$::rc} {incr iCache} {
  110. execsql "PRAGMA cache_size = $iCache"
  111. do_test tkt2409-2.1.$iCache {
  112. read_lock_db
  113. set ::rc [catch {
  114. execsql {
  115. BEGIN;
  116. INSERT INTO t1 VALUES($::zShort, $::zLong);
  117. }
  118. } msg]
  119. expr {($::rc == 1 && $msg eq "disk I/O error") || $::rc == 0}
  120. } {1}
  121. }
  122. do_test tkt2409-2.2 {
  123. catchsql {
  124. ROLLBACK;
  125. BEGIN;
  126. INSERT INTO t1 VALUES($::zShort, $::zLong);
  127. COMMIT;
  128. }
  129. } {1 {database is locked}}
  130. do_test tkt2409-2.3 {
  131. unread_lock_db
  132. catchsql {
  133. COMMIT;
  134. }
  135. } {0 {}}
  136. do_test tkt2409-3.1 {
  137. db close
  138. set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
  139. sqlite3_extended_result_codes $::DB 1
  140. execsql {
  141. PRAGMA cache_size=10;
  142. DELETE FROM t1;
  143. }
  144. read_lock_db
  145. set ::zShort [string repeat 0123456789 1]
  146. set ::zLong [string repeat 0123456789 1500]
  147. catchsql {
  148. BEGIN;
  149. INSERT INTO t1 SELECT $::zShort, $::zLong;
  150. }
  151. } {0 {}}
  152. do_test tkt2409-3.2 {
  153. sqlite3_errcode $::DB
  154. } {SQLITE_OK}
  155. # Check the integrity of the cache.
  156. #
  157. integrity_check tkt2409-3.3
  158. # Check that the transaction was rolled back. Because the INSERT
  159. # statement in which the "I/O error" occurred did not open a statement
  160. # transaction, SQLite had no choice but to roll back the transaction.
  161. #
  162. do_test tkt2409-3.4 {
  163. unread_lock_db
  164. catchsql { ROLLBACK }
  165. } {0 {}}
  166. integrity_check tkt2409-3.5
  167. expr {srand(1)}
  168. do_test tkt2409-4.1 {
  169. execsql {
  170. PRAGMA cache_size=20;
  171. DROP TABLE t1;
  172. CREATE TABLE t1 (x TEXT UNIQUE NOT NULL);
  173. }
  174. unset -nocomplain t1
  175. array unset t1
  176. set t1(0) 1
  177. set sql ""
  178. for {set i 0} {$i<5000} {incr i} {
  179. set r 0
  180. while {[info exists t1($r)]} {
  181. set r [expr {int(rand()*1000000000)}]
  182. }
  183. set t1($r) 1
  184. append sql "INSERT INTO t1 VALUES('some-text-$r');"
  185. }
  186. read_lock_db
  187. execsql BEGIN
  188. catchsql $sql
  189. } {0 {}}
  190. do_test tkt2409-4.2 {
  191. sqlite3_errcode $::DB
  192. } {SQLITE_OK}
  193. # Check the integrity of the cache.
  194. #
  195. integrity_check tkt2409-4.3
  196. do_test tkt2409-4.4 {
  197. catchsql { ROLLBACK }
  198. } {0 {}}
  199. integrity_check tkt2409-4.5
  200. unread_lock_db
  201. db2 close
  202. unset -nocomplain t1
  203. finish_test