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