walcrash.test 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. # 2010 February 8
  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. The
  12. # focus of this file is testing the operation of the library when
  13. # recovering a database following a simulated system failure in
  14. # "PRAGMA journal_mode=WAL" mode.
  15. #
  16. #
  17. # These are 'warm-body' tests of database recovery used while developing
  18. # the WAL code. They serve to prove that a few really simple cases work:
  19. #
  20. # walcrash-1.*: Recover a database.
  21. # walcrash-2.*: Recover a database where the failed transaction spanned more
  22. # than one page.
  23. # walcrash-3.*: Recover multiple databases where the failed transaction
  24. # was a multi-file transaction.
  25. #
  26. set testdir [file dirname $argv0]
  27. source $testdir/tester.tcl
  28. ifcapable !wal {finish_test ; return }
  29. db close
  30. set seed 0
  31. set REPEATS 100
  32. # walcrash-1.*
  33. #
  34. for {set i 1} {$i < $REPEATS} {incr i} {
  35. forcedelete test.db test.db-wal
  36. do_test walcrash-1.$i.1 {
  37. crashsql -delay 4 -file test.db-wal -seed [incr seed] {
  38. PRAGMA journal_mode = WAL;
  39. CREATE TABLE t1(a, b);
  40. INSERT INTO t1 VALUES(1, 1);
  41. INSERT INTO t1 VALUES(2, 3);
  42. INSERT INTO t1 VALUES(3, 6);
  43. }
  44. } {1 {child process exited abnormally}}
  45. do_test walcrash-1.$i.2 {
  46. sqlite3 db test.db
  47. execsql { SELECT sum(a)==max(b) FROM t1 }
  48. } {1}
  49. integrity_check walcrash-1.$i.3
  50. db close
  51. do_test walcrash-1.$i.4 {
  52. crashsql -delay 2 -file test.db-wal -seed [incr seed] {
  53. INSERT INTO t1 VALUES(4, (SELECT sum(a) FROM t1) + 4);
  54. INSERT INTO t1 VALUES(5, (SELECT sum(a) FROM t1) + 5);
  55. }
  56. } {1 {child process exited abnormally}}
  57. do_test walcrash-1.$i.5 {
  58. sqlite3 db test.db
  59. execsql { SELECT sum(a)==max(b) FROM t1 }
  60. } {1}
  61. integrity_check walcrash-1.$i.6
  62. do_test walcrash-1.$i.7 {
  63. execsql { PRAGMA main.journal_mode }
  64. } {wal}
  65. db close
  66. }
  67. # walcrash-2.*
  68. #
  69. for {set i 1} {$i < $REPEATS} {incr i} {
  70. forcedelete test.db test.db-wal
  71. do_test walcrash-2.$i.1 {
  72. crashsql -delay 5 -file test.db-wal -seed [incr seed] {
  73. PRAGMA journal_mode = WAL;
  74. CREATE TABLE t1(a PRIMARY KEY, b);
  75. INSERT INTO t1 VALUES(1, 2);
  76. INSERT INTO t1 VALUES(3, 4);
  77. INSERT INTO t1 VALUES(5, 9);
  78. }
  79. } {1 {child process exited abnormally}}
  80. do_test walcrash-2.$i.2 {
  81. sqlite3 db test.db
  82. execsql { SELECT sum(a)==max(b) FROM t1 }
  83. } {1}
  84. integrity_check walcrash-2.$i.3
  85. db close
  86. do_test walcrash-2.$i.4 {
  87. crashsql -delay 2 -file test.db-wal -seed [incr seed] {
  88. INSERT INTO t1 VALUES(6, (SELECT sum(a) FROM t1) + 6);
  89. INSERT INTO t1 VALUES(7, (SELECT sum(a) FROM t1) + 7);
  90. }
  91. } {1 {child process exited abnormally}}
  92. do_test walcrash-2.$i.5 {
  93. sqlite3 db test.db
  94. execsql { SELECT sum(a)==max(b) FROM t1 }
  95. } {1}
  96. integrity_check walcrash-2.$i.6
  97. do_test walcrash-2.$i.6 {
  98. execsql { PRAGMA main.journal_mode }
  99. } {wal}
  100. db close
  101. }
  102. # walcrash-3.*
  103. #
  104. # for {set i 1} {$i < $REPEATS} {incr i} {
  105. # forcedelete test.db test.db-wal
  106. # forcedelete test2.db test2.db-wal
  107. #
  108. # do_test walcrash-3.$i.1 {
  109. # crashsql -delay 2 -file test2.db-wal -seed [incr seed] {
  110. # PRAGMA journal_mode = WAL;
  111. # ATTACH 'test2.db' AS aux;
  112. # CREATE TABLE t1(a PRIMARY KEY, b);
  113. # CREATE TABLE aux.t2(a PRIMARY KEY, b);
  114. # BEGIN;
  115. # INSERT INTO t1 VALUES(1, 2);
  116. # INSERT INTO t2 VALUES(1, 2);
  117. # COMMIT;
  118. # }
  119. # } {1 {child process exited abnormally}}
  120. #
  121. # do_test walcrash-3.$i.2 {
  122. # sqlite3_wal db test.db
  123. # execsql {
  124. # ATTACH 'test2.db' AS aux;
  125. # SELECT * FROM t1 EXCEPT SELECT * FROM t2;
  126. # }
  127. # } {}
  128. # do_test walcrash-3.$i.3 { execsql { PRAGMA main.integrity_check } } {ok}
  129. # do_test walcrash-3.$i.4 { execsql { PRAGMA aux.integrity_check } } {ok}
  130. #
  131. # db close
  132. # }
  133. # walcrash-4.*
  134. #
  135. for {set i 1} {$i < $REPEATS} {incr i} {
  136. forcedelete test.db test.db-wal
  137. forcedelete test2.db test2.db-wal
  138. do_test walcrash-4.$i.1 {
  139. crashsql -delay 4 -file test.db-wal -seed [incr seed] -blocksize 4096 {
  140. PRAGMA journal_mode = WAL;
  141. PRAGMA page_size = 1024;
  142. CREATE TABLE t1(a PRIMARY KEY, b);
  143. INSERT INTO t1 VALUES(1, 2);
  144. INSERT INTO t1 VALUES(3, 4);
  145. }
  146. } {1 {child process exited abnormally}}
  147. do_test walcrash-4.$i.2 {
  148. sqlite3 db test.db
  149. execsql {
  150. SELECT * FROM t1 WHERE a = 1;
  151. }
  152. } {1 2}
  153. do_test walcrash-4.$i.3 { execsql { PRAGMA main.integrity_check } } {ok}
  154. do_test walcrash-4.$i.4 { execsql { PRAGMA main.journal_mode } } {wal}
  155. db close
  156. }
  157. # walcrash-5.*
  158. #
  159. for {set i 1} {$i < $REPEATS} {incr i} {
  160. forcedelete test.db test.db-wal
  161. forcedelete test2.db test2.db-wal
  162. do_test walcrash-5.$i.1 {
  163. crashsql -delay 13 -file test.db-wal -seed [incr seed] -blocksize 4096 {
  164. PRAGMA journal_mode = WAL;
  165. PRAGMA page_size = 1024;
  166. BEGIN;
  167. CREATE TABLE t1(x PRIMARY KEY);
  168. INSERT INTO t1 VALUES(randomblob(900));
  169. INSERT INTO t1 VALUES(randomblob(900));
  170. INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4 */
  171. COMMIT;
  172. INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 8 */
  173. INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 12 */
  174. INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 16 */
  175. INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 20 */
  176. INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 24 */
  177. INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 28 */
  178. INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 32 */
  179. PRAGMA wal_checkpoint;
  180. INSERT INTO t1 VALUES(randomblob(900));
  181. INSERT INTO t1 VALUES(randomblob(900));
  182. INSERT INTO t1 VALUES(randomblob(900));
  183. }
  184. } {1 {child process exited abnormally}}
  185. do_test walcrash-5.$i.2 {
  186. sqlite3 db test.db
  187. execsql { SELECT count(*)==33 OR count(*)==34 FROM t1 WHERE x != 1 }
  188. } {1}
  189. do_test walcrash-5.$i.3 { execsql { PRAGMA main.integrity_check } } {ok}
  190. do_test walcrash-5.$i.4 { execsql { PRAGMA main.journal_mode } } {wal}
  191. db close
  192. }
  193. # walcrash-6.*
  194. #
  195. for {set i 1} {$i < $REPEATS} {incr i} {
  196. forcedelete test.db test.db-wal
  197. forcedelete test2.db test2.db-wal
  198. do_test walcrash-6.$i.1 {
  199. crashsql -delay 14 -file test.db-wal -seed [incr seed] -blocksize 512 {
  200. PRAGMA journal_mode = WAL;
  201. PRAGMA page_size = 1024;
  202. BEGIN;
  203. CREATE TABLE t1(x PRIMARY KEY);
  204. INSERT INTO t1 VALUES(randomblob(900));
  205. INSERT INTO t1 VALUES(randomblob(900));
  206. INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4 */
  207. COMMIT;
  208. INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 8 */
  209. INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 12 */
  210. INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 16 */
  211. INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 20 */
  212. INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 24 */
  213. INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 28 */
  214. INSERT INTO t1 SELECT randomblob(900) FROM t1 LIMIT 4; /* 32 */
  215. PRAGMA wal_checkpoint;
  216. INSERT INTO t1 VALUES(randomblob(9000));
  217. INSERT INTO t1 VALUES(randomblob(9000));
  218. INSERT INTO t1 VALUES(randomblob(9000));
  219. }
  220. } {1 {child process exited abnormally}}
  221. do_test walcrash-6.$i.2 {
  222. sqlite3 db test.db
  223. execsql { SELECT count(*)==34 OR count(*)==35 FROM t1 WHERE x != 1 }
  224. } {1}
  225. do_test walcrash-6.$i.3 { execsql { PRAGMA main.integrity_check } } {ok}
  226. do_test walcrash-6.$i.4 { execsql { PRAGMA main.journal_mode } } {wal}
  227. db close
  228. }
  229. #-------------------------------------------------------------------------
  230. # This test case simulates a crash while checkpointing the database. Page
  231. # 1 is one of the pages overwritten by the checkpoint. This is a special
  232. # case because it means the content of page 1 may be damaged. SQLite will
  233. # have to determine:
  234. #
  235. # (a) that the database is a WAL database, and
  236. # (b) the database page-size
  237. #
  238. # based on the log file.
  239. #
  240. for {set i 1} {$i < $REPEATS} {incr i} {
  241. forcedelete test.db test.db-wal
  242. # Select a page-size for this test.
  243. #
  244. set pgsz [lindex {512 1024 2048 4096 8192 16384} [expr $i%6]]
  245. do_test walcrash-7.$i.1 {
  246. crashsql -delay 3 -file test.db -seed [incr seed] -blocksize 512 "
  247. PRAGMA page_size = $pgsz;
  248. PRAGMA journal_mode = wal;
  249. BEGIN;
  250. CREATE TABLE t1(a, b);
  251. INSERT INTO t1 VALUES(1, 2);
  252. COMMIT;
  253. PRAGMA wal_checkpoint;
  254. CREATE INDEX i1 ON t1(a);
  255. PRAGMA wal_checkpoint;
  256. "
  257. } {1 {child process exited abnormally}}
  258. do_test walcrash-7.$i.2 {
  259. sqlite3 db test.db
  260. execsql { SELECT b FROM t1 WHERE a = 1 }
  261. } {2}
  262. do_test walcrash-7.$i.3 { execsql { PRAGMA main.integrity_check } } {ok}
  263. do_test walcrash-7.$i.4 { execsql { PRAGMA main.journal_mode } } {wal}
  264. db close
  265. }
  266. finish_test