crash3.test 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # 2007 August 23
  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 contains tests that verify that SQLite can correctly rollback
  13. # databases after crashes when using the special IO modes triggered
  14. # by device IOCAP flags.
  15. #
  16. # $Id: crash3.test,v 1.4 2008/07/12 14:52:20 drh Exp $
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. ifcapable !crashtest {
  20. finish_test
  21. return
  22. }
  23. proc do_test2 {name tcl res1 res2} {
  24. set script [subst -nocommands {
  25. do_test $name {
  26. set res1 {$res1}
  27. set res2 {$res2}
  28. set res [eval {$tcl}]
  29. if {[set res] eq [set res1] || [set res] eq [set res2]} {
  30. set res "{[set res1]} or {[set res2]}"
  31. }
  32. set res
  33. } {{$res1} or {$res2}}
  34. }]
  35. uplevel $script
  36. }
  37. # This block tests crash-recovery when the IOCAP_ATOMIC flags is set.
  38. #
  39. # Each iteration of the following loop sets up the database to contain
  40. # the following schema and data:
  41. #
  42. # CREATE TABLE abc(a, b, c);
  43. # INSERT INTO abc VALUES(1, 2, 3);
  44. #
  45. # Then execute the SQL statement, scheduling a crash for part-way through
  46. # the first sync() of either the database file or the journal file (often
  47. # the journal file is not required - meaning no crash occurs).
  48. #
  49. # After the crash (or absence of a crash), open the database and
  50. # verify that:
  51. #
  52. # * The integrity check passes, and
  53. # * The contents of table abc is either {1 2 3} or the value specified
  54. # to the right of the SQL statement below.
  55. #
  56. # The procedure is repeated 10 times for each SQL statement. Five times
  57. # with the crash scheduled for midway through the first journal sync (if
  58. # any), and five times with the crash midway through the database sync.
  59. #
  60. set tn 1
  61. foreach {sql res2} [list \
  62. {INSERT INTO abc VALUES(4, 5, 6)} {1 2 3 4 5 6} \
  63. {DELETE FROM abc} {} \
  64. {INSERT INTO abc SELECT * FROM abc} {1 2 3 1 2 3} \
  65. {UPDATE abc SET a = 2} {2 2 3} \
  66. {INSERT INTO abc VALUES(4, 5, randstr(1000,1000))} {n/a} \
  67. {CREATE TABLE def(d, e, f)} {n/a} \
  68. ] {
  69. for {set ii 0} {$ii < 10} {incr ii} {
  70. db close
  71. forcedelete test.db test.db-journal
  72. sqlite3 db test.db
  73. do_test crash3-1.$tn.1 {
  74. execsql {
  75. PRAGMA page_size = 1024;
  76. BEGIN;
  77. CREATE TABLE abc(a, b, c);
  78. INSERT INTO abc VALUES(1, 2, 3);
  79. COMMIT;
  80. }
  81. } {}
  82. db close
  83. set crashfile test.db
  84. if {($ii%2)==0} { append crashfile -journal }
  85. set rand "SELECT randstr($tn,$tn);"
  86. do_test crash3-1.$tn.2 [subst {
  87. crashsql -file $crashfile -char atomic {$rand $sql}
  88. sqlite3 db test.db
  89. execsql { PRAGMA integrity_check; }
  90. }] {ok}
  91. do_test2 crash3-1.$tn.3 {
  92. execsql { SELECT * FROM abc }
  93. } {1 2 3} $res2
  94. incr tn
  95. }
  96. }
  97. # This block tests both the IOCAP_SEQUENTIAL and IOCAP_SAFE_APPEND flags.
  98. #
  99. db close
  100. forcedelete test.db test.db-journal
  101. sqlite3 db test.db
  102. do_test crash3-2.0 {
  103. execsql {
  104. BEGIN;
  105. CREATE TABLE abc(a PRIMARY KEY, b, c);
  106. CREATE TABLE def(d PRIMARY KEY, e, f);
  107. PRAGMA default_cache_size = 10;
  108. INSERT INTO abc VALUES(randstr(10,1000),randstr(10,1000),randstr(10,1000));
  109. INSERT INTO abc
  110. SELECT randstr(10,1000),randstr(10,1000),randstr(10,1000) FROM abc;
  111. INSERT INTO abc
  112. SELECT randstr(10,1000),randstr(10,1000),randstr(10,1000) FROM abc;
  113. INSERT INTO abc
  114. SELECT randstr(10,1000),randstr(10,1000),randstr(10,1000) FROM abc;
  115. INSERT INTO abc
  116. SELECT randstr(10,1000),randstr(10,1000),randstr(10,1000) FROM abc;
  117. INSERT INTO abc
  118. SELECT randstr(10,1000),randstr(10,1000),randstr(10,1000) FROM abc;
  119. INSERT INTO abc
  120. SELECT randstr(10,1000),randstr(10,1000),randstr(10,1000) FROM abc;
  121. COMMIT;
  122. }
  123. } {}
  124. set tn 1
  125. foreach {::crashfile ::delay ::char} {
  126. test.db 1 sequential
  127. test.db 1 safe_append
  128. test.db-journal 1 sequential
  129. test.db-journal 1 safe_append
  130. test.db-journal 2 safe_append
  131. test.db-journal 2 sequential
  132. test.db-journal 3 sequential
  133. test.db-journal 3 safe_append
  134. } {
  135. for {set ii 0} {$ii < 100} {incr ii} {
  136. set ::SQL [subst {
  137. SELECT randstr($ii,$ii+10);
  138. BEGIN;
  139. DELETE FROM abc WHERE random()%5;
  140. INSERT INTO abc
  141. SELECT randstr(10,1000),randstr(10,1000),randstr(10,1000)
  142. FROM abc
  143. WHERE (random()%5)==0;
  144. DELETE FROM def WHERE random()%5;
  145. INSERT INTO def
  146. SELECT randstr(10,1000),randstr(10,1000),randstr(10,1000)
  147. FROM def
  148. WHERE (random()%5)==0;
  149. COMMIT;
  150. }]
  151. do_test crash3-2.$tn.$ii {
  152. crashsql -file $::crashfile -delay $::delay -char $::char $::SQL
  153. db close
  154. sqlite3 db test.db
  155. execsql {PRAGMA integrity_check}
  156. } {ok}
  157. }
  158. incr tn
  159. }
  160. # The following block tests an interaction between IOCAP_ATOMIC and
  161. # IOCAP_SEQUENTIAL. At one point, if both flags were set, small
  162. # journal files that contained only a single page, but were required
  163. # for some other reason (i.e. nTrunk) were not being written to
  164. # disk.
  165. #
  166. for {set ii 0} {$ii < 10} {incr ii} {
  167. db close
  168. forcedelete test.db test.db-journal
  169. crashsql -file test.db -char {sequential atomic} {
  170. CREATE TABLE abc(a, b, c);
  171. }
  172. sqlite3 db test.db
  173. do_test crash3-3.$ii {
  174. execsql {PRAGMA integrity_check}
  175. } {ok}
  176. }
  177. finish_test