3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
13 if {![info exists testdir]} {
14 set testdir [file join [file dirname [info script]] .. .. test]
16 source $testdir/tester.tcl
17 set ::testprefix rbucrash
20 forcedelete test.db-oal rbu.db
25 # Set up a target database and an rbu update database. The target
26 # db is the usual "test.db", the rbu db is "test.db2".
30 CREATE TABLE t1(a, b, c, PRIMARY KEY(a), UNIQUE(b));
31 INSERT INTO t1 VALUES(1, 2, 3);
32 INSERT INTO t1 VALUES(4, 5, 6);
33 INSERT INTO t1 VALUES(7, 8, 9);
35 ATTACH 'test.db2' AS rbu;
36 CREATE TABLE rbu.data_t1(a, b, c, rbu_control);
37 INSERT INTO data_t1 VALUES(10, 11, 12, 0);
38 INSERT INTO data_t1 VALUES(13, 14, 15, 0);
39 INSERT INTO data_t1 VALUES(4, NULL, NULL, 1);
40 INSERT INTO data_t1 VALUES(1, NULL, 100, '..x');
45 # Determine the number of steps in applying the rbu update to the test
46 # target database created above. Set $::rbu_num_steps accordingly
48 # Check that the same number of steps are required to apply the rbu
49 # update using many calls to sqlite3rbu_step() on a single rbu handle
50 # as required to apply it using a series of rbu handles, on each of
51 # which sqlite3rbu_step() is called once.
55 sqlite3rbu rbu test.db test.db2
57 while {[rbu step]=="SQLITE_OK"} { incr nStep }
60 set rbu_num_steps $nStep
65 sqlite3rbu rbu test.db test.db2
67 if {[rbu close]=="SQLITE_DONE"} break
74 # Run one or more tests using the target (test.db) and rbu (test.db2)
75 # databases created above. As follows:
77 # 1. This process starts the rbu update and calls sqlite3rbu_step()
78 # $nPre times. Then closes the rbu update handle.
80 # 2. A second process resumes the rbu update and attempts to call
81 # sqlite3rbu_step() $nStep times before closing the handle. A
82 # crash is simulated during each xSync() of file test.db2.
84 # 3. This process attempts to resume the rbu update from whatever
85 # state it was left in by step (2). Test that it is successful
86 # in doing so and that the final target database is as expected.
88 # In total (nSync+1) tests are run, where nSync is the number of times
89 # xSync() is called on test.db2.
91 proc do_rbu_crash_test {tn nPre nStep} {
93 set script [subst -nocommands {
94 sqlite3rbu rbu test.db file:test.db2?vfs=crash
96 while {[set i] < $nStep} {
97 if {[rbu step]!="SQLITE_OK"} break
104 for {set iDelay 1} {$bDone==0} {incr iDelay} {
105 forcedelete test.db2 test.db2-journal test.db test.db-oal test.db-wal
109 sqlite3rbu rbu test.db file:test.db2
111 for {set i 0} {$i < $nPre} {incr i} {
112 if {[rbu step]!="SQLITE_OK"} break
118 crashsql -file test.db2 -delay $iDelay -tclbody $script -opendb {} {}
122 if {$res == "1 {child process exited abnormally}"} {
124 } elseif {$res != "0 {}"} {
125 error "unexected catchsql result: $res"
128 sqlite3rbu rbu test.db test.db2
129 while {[rbu step]=="SQLITE_OK"} {}
133 do_execsql_test $tn.delay=$iDelay {
135 PRAGMA integrity_check;
136 } {1 2 100 7 8 9 10 11 12 13 14 15 ok}
141 for {set nPre 0} {$nPre < $rbu_num_steps} {incr nPre} {
142 for {set is 1} {$is <= ($rbu_num_steps - $nPre)} {incr is} {
143 do_rbu_crash_test 2.pre=$nPre.step=$is $nPre $is