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 source [file join [file dirname [info script]] rbu_common.tcl]
14 if_no_rbu_support { finish_test ; return }
15 set ::testprefix rbucrash
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".
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);
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');
37 # Determine the number of steps in applying the rbu update to the test
38 # target database created above. Set $::rbu_num_steps accordingly
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.
47 sqlite3rbu rbu test.db test.db2
49 while {[rbu step]=="SQLITE_OK"} { incr nStep }
52 set rbu_num_steps $nStep
57 sqlite3rbu rbu test.db test.db2
59 if {[rbu close]=="SQLITE_DONE"} break
66 # Run one or more tests using the target (test.db) and rbu (test.db2)
67 # databases created above. As follows:
69 # 1. This process starts the rbu update and calls sqlite3rbu_step()
70 # $nPre times. Then closes the rbu update handle.
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.
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.
80 # In total (nSync+1) tests are run, where nSync is the number of times
81 # xSync() is called on test.db2.
83 proc do_rbu_crash_test {tn nPre nStep} {
85 set script [subst -nocommands {
86 sqlite3rbu rbu test.db file:test.db2?vfs=crash
88 while {[set i] < $nStep} {
89 if {[rbu step]!="SQLITE_OK"} break
96 for {set iDelay 1} {$bDone==0} {incr iDelay} {
97 forcedelete test.db2 test.db2-journal test.db test.db-oal test.db-wal
101 sqlite3rbu rbu test.db file:test.db2
103 for {set i 0} {$i < $nPre} {incr i} {
104 if {[rbu step]!="SQLITE_OK"} break
110 crashsql -file test.db2 -delay $iDelay -tclbody $script -opendb {} {}
114 if {$res == "1 {child process exited abnormally}"} {
116 } elseif {$res != "0 {}"} {
117 error "unexected catchsql result: $res"
120 sqlite3rbu rbu test.db test.db2
121 while {[rbu step]=="SQLITE_OK"} {}
125 do_execsql_test $tn.delay=$iDelay {
127 PRAGMA integrity_check;
128 } {1 2 100 7 8 9 10 11 12 13 14 15 ok}
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