walcrash3.test 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # 2011 December 16
  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 test simulates an application crash immediately following a
  13. # system call to truncate a file. Specifically, the system call that
  14. # truncates the WAL file if "PRAGMA journal_size_limit" is configured.
  15. #
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. ifcapable !wal {finish_test ; return }
  19. set testprefix walcrash3
  20. db close
  21. testvfs tvfs
  22. tvfs filter {xTruncate xWrite}
  23. tvfs script tvfs_callback
  24. proc tvfs_callback {args} {}
  25. sqlite3 db test.db -vfs tvfs
  26. do_execsql_test 1.1 {
  27. PRAGMA page_size = 1024;
  28. PRAGMA journal_mode = WAL;
  29. PRAGMA wal_autocheckpoint = 128;
  30. PRAGMA journal_size_limit = 16384;
  31. CREATE TABLE t1(a BLOB, b BLOB, UNIQUE(a, b));
  32. INSERT INTO t1 VALUES(randomblob(10), randomblob(1000));
  33. } {wal 128 16384}
  34. proc tvfs_callback {method file arglist} {
  35. if {$::state==1} {
  36. foreach f [glob -nocomplain xx_test.*] { forcedelete $f }
  37. foreach f [glob -nocomplain test.*] { forcecopy $f "xx_$f" }
  38. set ::state 2
  39. }
  40. if {$::state==0 && $method=="xTruncate" && [file tail $file]=="test.db-wal"} {
  41. set ::state 1
  42. }
  43. }
  44. for {set i 2} {$i<1000} {incr i} {
  45. # If the WAL file is truncated within the following, within the following
  46. # xWrite call the [tvfs_callback] makes a copy of the database and WAL
  47. # files set sets $::state to 2. So that the copied files are in the same
  48. # state as the real database and WAL files would be if an application crash
  49. # occurred immediately following the xTruncate().
  50. #
  51. set ::state 0
  52. do_execsql_test 1.$i.1 {
  53. INSERT INTO t1 VALUES(randomblob(10), randomblob(1000));
  54. }
  55. # If a copy was made, open it and run the integrity-check.
  56. #
  57. if {$::state==2} {
  58. sqlite3 db2 xx_test.db
  59. do_test 1.$i.2 { execsql { PRAGMA integrity_check } db2 } "ok"
  60. do_test 1.$i.3 { execsql { SELECT count(*) FROM t1 } db2 } [expr $i-1]
  61. db2 close
  62. }
  63. }
  64. catch { db close }
  65. tvfs delete
  66. #--------------------------------------------------------------------------
  67. #
  68. catch { db close }
  69. forcedelete test.db
  70. do_test 2.1 {
  71. sqlite3 db test.db
  72. execsql {
  73. PRAGMA page_size = 512;
  74. PRAGMA journal_mode = WAL;
  75. PRAGMA wal_autocheckpoint = 128;
  76. CREATE TABLE t1(a PRIMARY KEY, b);
  77. INSERT INTO t1 VALUES(randomblob(25), randomblob(200));
  78. }
  79. for {set i 0} {$i < 1500} {incr i} {
  80. execsql { INSERT INTO t1 VALUES(randomblob(25), randomblob(200)) }
  81. }
  82. db_save
  83. db close
  84. } {}
  85. set nInitialErr [set_test_counter errors]
  86. for {set i 2} {$i<10000 && [set_test_counter errors]==$nInitialErr} {incr i} {
  87. do_test 2.$i.1 {
  88. catch { db close }
  89. db_restore
  90. crashsql -delay 2 -file test.db-wal -seed $i {
  91. SELECT * FROM sqlite_master;
  92. PRAGMA synchronous = full;
  93. PRAGMA wal_checkpoint;
  94. BEGIN;
  95. INSERT INTO t1 VALUES(randomblob(26), randomblob(200));
  96. INSERT INTO t1 VALUES(randomblob(26), randomblob(200));
  97. INSERT INTO t1 VALUES(randomblob(26), randomblob(200));
  98. INSERT INTO t1 VALUES(randomblob(26), randomblob(200));
  99. INSERT INTO t1 VALUES(randomblob(26), randomblob(200));
  100. INSERT INTO t1 VALUES(randomblob(26), randomblob(200));
  101. INSERT INTO t1 VALUES(randomblob(26), randomblob(200));
  102. INSERT INTO t1 VALUES(randomblob(26), randomblob(200));
  103. COMMIT;
  104. }
  105. } {1 {child process exited abnormally}}
  106. do_test 2.$i.2 {
  107. sqlite3 db test.db
  108. execsql { PRAGMA integrity_check }
  109. } {ok}
  110. }
  111. finish_test