| 1 |
# 2015 February 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 |
|
| 13 |
source [file join [file dirname [info script]] rbu_common.tcl] |
| 14 |
if_no_rbu_support { finish_test ; return } |
| 15 |
source $testdir/lock_common.tcl |
| 16 |
set ::testprefix rbu12 |
| 17 |
|
| 18 |
set setup_sql { |
| 19 |
DROP TABLE IF EXISTS xx; |
| 20 |
DROP TABLE IF EXISTS xy; |
| 21 |
CREATE TABLE xx(a, b, c PRIMARY KEY); |
| 22 |
INSERT INTO xx VALUES(1, 2, 3); |
| 23 |
CREATE TABLE xy(a, b, c PRIMARY KEY); |
| 24 |
|
| 25 |
ATTACH 'rbu.db' AS rbu; |
| 26 |
DROP TABLE IF EXISTS data_xx; |
| 27 |
CREATE TABLE rbu.data_xx(a, b, c, rbu_control); |
| 28 |
INSERT INTO data_xx VALUES(4, 5, 6, 0); |
| 29 |
INSERT INTO data_xx VALUES(7, 8, 9, 0); |
| 30 |
CREATE TABLE rbu.data_xy(a, b, c, rbu_control); |
| 31 |
INSERT INTO data_xy VALUES(10, 11, 12, 0); |
| 32 |
DETACH rbu; |
| 33 |
} |
| 34 |
|
| 35 |
do_multiclient_test tn { |
| 36 |
|
| 37 |
# Initialize a target (test.db) and rbu (rbu.db) database. |
| 38 |
# |
| 39 |
forcedelete rbu.db |
| 40 |
sql1 $setup_sql |
| 41 |
|
| 42 |
# Using connection 2, open a read transaction on the target database. |
| 43 |
# RBU will still be able to generate "test.db-oal", but it will not be |
| 44 |
# able to rename it to "test.db-wal". |
| 45 |
# |
| 46 |
do_test 1.$tn.1 { |
| 47 |
sql2 { BEGIN; SELECT * FROM xx; } |
| 48 |
} {1 2 3} |
| 49 |
do_test 1.$tn.2 { |
| 50 |
sqlite3rbu rbu test.db rbu.db |
| 51 |
while 1 { |
| 52 |
set res [rbu step] |
| 53 |
if {$res!="SQLITE_OK"} break |
| 54 |
} |
| 55 |
set res |
| 56 |
} {SQLITE_BUSY} |
| 57 |
|
| 58 |
do_test 1.$tn.3 { sql2 { SELECT * FROM xx; } } {1 2 3} |
| 59 |
do_test 1.$tn.4 { sql2 { SELECT * FROM xy; } } {} |
| 60 |
do_test 1.$tn.5 { |
| 61 |
list [file exists test.db-wal] [file exists test.db-oal] |
| 62 |
} {0 1} |
| 63 |
do_test 1.$tn.6 { sql2 COMMIT } {} |
| 64 |
|
| 65 |
# The rbu object that hit the SQLITE_BUSY error above cannot be reused. |
| 66 |
# It is stuck in a permanent SQLITE_BUSY state at this point. |
| 67 |
# |
| 68 |
do_test 1.$tn.7 { rbu step } {SQLITE_BUSY} |
| 69 |
do_test 1.$tn.8 { |
| 70 |
list [catch { rbu close } msg] $msg |
| 71 |
} {1 SQLITE_BUSY} |
| 72 |
|
| 73 |
do_test 1.$tn.9.1 { sql2 { BEGIN EXCLUSIVE } } {} |
| 74 |
do_test 1.$tn.9.2 { |
| 75 |
sqlite3rbu rbu test.db rbu.db |
| 76 |
rbu step |
| 77 |
} {SQLITE_BUSY} |
| 78 |
do_test 1.$tn.9.3 { |
| 79 |
list [catch { rbu close } msg] $msg |
| 80 |
} {1 {SQLITE_BUSY - database is locked}} |
| 81 |
do_test 1.$tn.9.4 { sql2 COMMIT } {} |
| 82 |
|
| 83 |
sqlite3rbu rbu test.db rbu.db |
| 84 |
do_test 1.$tn.10.1 { sql2 { BEGIN EXCLUSIVE } } {} |
| 85 |
do_test 1.$tn.10.2 { |
| 86 |
rbu step |
| 87 |
} {SQLITE_BUSY} |
| 88 |
do_test 1.$tn.10.3 { |
| 89 |
list [catch { rbu close } msg] $msg |
| 90 |
} {1 SQLITE_BUSY} |
| 91 |
do_test 1.$tn.10.4 { sql2 COMMIT } {} |
| 92 |
|
| 93 |
# A new rbu object can finish the work though. |
| 94 |
# |
| 95 |
do_test 1.$tn.11 { |
| 96 |
sqlite3rbu rbu test.db rbu.db |
| 97 |
rbu step |
| 98 |
} {SQLITE_OK} |
| 99 |
do_test 1.$tn.12 { |
| 100 |
list [file exists test.db-wal] [file exists test.db-oal] |
| 101 |
} {1 0} |
| 102 |
do_test 1.$tn.13 { |
| 103 |
while 1 { |
| 104 |
set res [rbu step] |
| 105 |
if {$res!="SQLITE_OK"} break |
| 106 |
} |
| 107 |
set res |
| 108 |
} {SQLITE_DONE} |
| 109 |
|
| 110 |
do_test 1.$tn.14 { |
| 111 |
rbu close |
| 112 |
} {SQLITE_DONE} |
| 113 |
} |
| 114 |
|
| 115 |
do_multiclient_test tn { |
| 116 |
|
| 117 |
# Initialize a target (test.db) and rbu (rbu.db) database. |
| 118 |
# |
| 119 |
forcedelete rbu.db |
| 120 |
sql1 $setup_sql |
| 121 |
|
| 122 |
do_test 2.$tn.1 { |
| 123 |
sqlite3rbu rbu test.db rbu.db |
| 124 |
while {[file exists test.db-wal]==0} { |
| 125 |
if {[rbu step]!="SQLITE_OK"} {error "problem here...."} |
| 126 |
} |
| 127 |
rbu close |
| 128 |
} {SQLITE_OK} |
| 129 |
|
| 130 |
|
| 131 |
do_test 2.$tn.2 { sql2 { BEGIN IMMEDIATE } } {} |
| 132 |
|
| 133 |
do_test 2.$tn.3 { |
| 134 |
sqlite3rbu rbu test.db rbu.db |
| 135 |
rbu step |
| 136 |
} {SQLITE_BUSY} |
| 137 |
|
| 138 |
do_test 2.$tn.4 { list [catch { rbu close } msg] $msg } {1 SQLITE_BUSY} |
| 139 |
|
| 140 |
do_test 2.$tn.5 { |
| 141 |
sql2 { SELECT * FROM xx ; COMMIT } |
| 142 |
} {1 2 3 4 5 6 7 8 9} |
| 143 |
|
| 144 |
do_test 2.$tn.6 { |
| 145 |
sqlite3rbu rbu test.db rbu.db |
| 146 |
rbu step |
| 147 |
rbu close |
| 148 |
} {SQLITE_OK} |
| 149 |
|
| 150 |
do_test 2.$tn.7 { sql2 { BEGIN EXCLUSIVE } } {} |
| 151 |
|
| 152 |
do_test 2.$tn.8 { |
| 153 |
sqlite3rbu rbu test.db rbu.db |
| 154 |
rbu step |
| 155 |
} {SQLITE_BUSY} |
| 156 |
do_test 2.$tn.9 { list [catch { rbu close } msg] $msg } {1 SQLITE_BUSY} |
| 157 |
do_test 2.$tn.10 { |
| 158 |
sql2 { SELECT * FROM xx ; COMMIT } |
| 159 |
} {1 2 3 4 5 6 7 8 9} |
| 160 |
|
| 161 |
do_test 2.$tn.11 { |
| 162 |
sqlite3rbu rbu test.db rbu.db |
| 163 |
while {[rbu step]=="SQLITE_OK"} {} |
| 164 |
rbu close |
| 165 |
} {SQLITE_DONE} |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
#------------------------------------------------------------------------- |
| 170 |
# Test that "PRAGMA data_version" works when an RBU client writes the |
| 171 |
# database. |
| 172 |
# |
| 173 |
do_multiclient_test tn { |
| 174 |
|
| 175 |
# Initialize a target (test.db) and rbu (rbu.db) database. |
| 176 |
# |
| 177 |
forcedelete rbu.db |
| 178 |
sql1 $setup_sql |
| 179 |
|
| 180 |
# Check the initial database contains table "xx" with a single row. |
| 181 |
# Also save the current values of "PRAGMA data-version" for [db1] |
| 182 |
# and [db2]. |
| 183 |
# |
| 184 |
do_test 2.$tn.1 { |
| 185 |
list [sql1 { SELECT count(*) FROM xx }] [sql2 { SELECT count(*) FROM xx }] |
| 186 |
} {1 1} |
| 187 |
set V1 [sql1 {PRAGMA data_version}] |
| 188 |
set V2 [sql2 {PRAGMA data_version}] |
| 189 |
|
| 190 |
# Check the values of data-version have not magically changed. |
| 191 |
# |
| 192 |
do_test 2.$tn.2 { |
| 193 |
list [sql1 {PRAGMA data_version}] [sql2 {PRAGMA data_version}] |
| 194 |
} [list $V1 $V2] |
| 195 |
|
| 196 |
# Start stepping the RBU. From the point of view of [db1] and [db2], the |
| 197 |
# data-version values remain unchanged until the database contents are |
| 198 |
# modified. At which point the values are incremented. |
| 199 |
# |
| 200 |
sqlite3rbu rbu test.db rbu.db |
| 201 |
set x 0 |
| 202 |
while {[db one {SELECT count(*) FROM xx}]==1} { |
| 203 |
do_test 2.$tn.3.[incr x] { |
| 204 |
list [sql1 {PRAGMA data_version}] [sql2 {PRAGMA data_version}] |
| 205 |
} [list $V1 $V2] |
| 206 |
rbu step |
| 207 |
} |
| 208 |
do_test 2.$tn.5.1 { expr {$V1 < [sql1 {PRAGMA data_version}]} } 1 |
| 209 |
do_test 2.$tn.5.2 { expr {$V2 < [sql2 {PRAGMA data_version}]} } 1 |
| 210 |
|
| 211 |
# Check the db contents is as expected. |
| 212 |
# |
| 213 |
do_test 2.$tn.4 { |
| 214 |
list [sql1 {SELECT count(*) FROM xx}] [sql2 {SELECT count(*) FROM xx}] |
| 215 |
} {3 3} |
| 216 |
|
| 217 |
set V1 [sql1 {PRAGMA data_version}] |
| 218 |
set V2 [sql2 {PRAGMA data_version}] |
| 219 |
|
| 220 |
# Finish applying the RBU (i.e. do the incremental checkpoint). Check that |
| 221 |
# this does not cause the data-version values to change. |
| 222 |
# |
| 223 |
while {[rbu step]=="SQLITE_OK"} { } |
| 224 |
rbu close |
| 225 |
|
| 226 |
do_test 2.$tn.6 { |
| 227 |
list [sql1 {PRAGMA data_version}] [sql2 {PRAGMA data_version}] |
| 228 |
} [list $V1 $V2] |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
finish_test |