rbucrash.test 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # 2014 October 22
  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. source [file join [file dirname [info script]] rbu_common.tcl]
  13. if_no_rbu_support { finish_test ; return }
  14. set ::testprefix rbucrash
  15. # Set up a target database and an rbu update database. The target
  16. # db is the usual "test.db", the rbu db is "test.db2".
  17. #
  18. forcedelete test.db2
  19. do_execsql_test 1.0 {
  20. CREATE TABLE t1(a, b, c, PRIMARY KEY(a), UNIQUE(b));
  21. INSERT INTO t1 VALUES(1, 2, 3);
  22. INSERT INTO t1 VALUES(4, 5, 6);
  23. INSERT INTO t1 VALUES(7, 8, 9);
  24. ATTACH 'test.db2' AS rbu;
  25. CREATE TABLE rbu.data_t1(a, b, c, rbu_control);
  26. INSERT INTO data_t1 VALUES(10, 11, 12, 0);
  27. INSERT INTO data_t1 VALUES(13, 14, 15, 0);
  28. INSERT INTO data_t1 VALUES(4, NULL, NULL, 1);
  29. INSERT INTO data_t1 VALUES(1, NULL, 100, '..x');
  30. }
  31. db_save_and_close
  32. # Determine the number of steps in applying the rbu update to the test
  33. # target database created above. Set $::rbu_num_steps accordingly
  34. #
  35. # Check that the same number of steps are required to apply the rbu
  36. # update using many calls to sqlite3rbu_step() on a single rbu handle
  37. # as required to apply it using a series of rbu handles, on each of
  38. # which sqlite3rbu_step() is called once.
  39. #
  40. do_test 1.1 {
  41. db_restore
  42. sqlite3rbu rbu test.db test.db2
  43. set nStep 0
  44. while {[rbu step]=="SQLITE_OK"} { incr nStep }
  45. rbu close
  46. } {SQLITE_DONE}
  47. set rbu_num_steps $nStep
  48. do_test 1.2 {
  49. db_restore
  50. set nStep 0
  51. while {1} {
  52. sqlite3rbu rbu test.db test.db2
  53. rbu step
  54. if {[rbu close]=="SQLITE_DONE"} break
  55. incr nStep
  56. }
  57. set nStep
  58. } $rbu_num_steps
  59. # Run one or more tests using the target (test.db) and rbu (test.db2)
  60. # databases created above. As follows:
  61. #
  62. # 1. This process starts the rbu update and calls sqlite3rbu_step()
  63. # $nPre times. Then closes the rbu update handle.
  64. #
  65. # 2. A second process resumes the rbu update and attempts to call
  66. # sqlite3rbu_step() $nStep times before closing the handle. A
  67. # crash is simulated during each xSync() of file test.db2.
  68. #
  69. # 3. This process attempts to resume the rbu update from whatever
  70. # state it was left in by step (2). Test that it is successful
  71. # in doing so and that the final target database is as expected.
  72. #
  73. # In total (nSync+1) tests are run, where nSync is the number of times
  74. # xSync() is called on test.db2.
  75. #
  76. proc do_rbu_crash_test {tn nPre nStep} {
  77. set script [subst -nocommands {
  78. sqlite3rbu rbu test.db file:test.db2?vfs=crash
  79. set i 0
  80. while {[set i] < $nStep} {
  81. if {[rbu step]!="SQLITE_OK"} break
  82. incr i
  83. }
  84. rbu close
  85. }]
  86. set bDone 0
  87. for {set iDelay 1} {$bDone==0} {incr iDelay} {
  88. forcedelete test.db2 test.db2-journal test.db test.db-oal test.db-wal
  89. db_restore
  90. if {$nPre>0} {
  91. sqlite3rbu rbu test.db file:test.db2
  92. set i 0
  93. for {set i 0} {$i < $nPre} {incr i} {
  94. if {[rbu step]!="SQLITE_OK"} break
  95. }
  96. rbu close
  97. }
  98. set res [
  99. crashsql -file test.db2 -delay $iDelay -tclbody $script -opendb {} {}
  100. ]
  101. set bDone 1
  102. if {$res == "1 {child process exited abnormally}"} {
  103. set bDone 0
  104. } elseif {$res != "0 {}"} {
  105. error "unexected catchsql result: $res"
  106. }
  107. sqlite3rbu rbu test.db test.db2
  108. while {[rbu step]=="SQLITE_OK"} {}
  109. rbu close
  110. sqlite3 db test.db
  111. do_execsql_test $tn.delay=$iDelay {
  112. SELECT * FROM t1;
  113. PRAGMA integrity_check;
  114. } {1 2 100 7 8 9 10 11 12 13 14 15 ok}
  115. db close
  116. }
  117. }
  118. for {set nPre 0} {$nPre < $rbu_num_steps} {incr nPre} {
  119. for {set is 1} {$is <= ($rbu_num_steps - $nPre)} {incr is} {
  120. do_rbu_crash_test 2.pre=$nPre.step=$is $nPre $is
  121. }
  122. }
  123. finish_test