| 1 |
# 2013 May 14 |
| 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 |
# Test some specific circumstances to do with shared cache mode. |
| 13 |
# |
| 14 |
|
| 15 |
|
| 16 |
set testdir [file dirname $argv0] |
| 17 |
source $testdir/tester.tcl |
| 18 |
if {[run_thread_tests]==0} { finish_test ; return } |
| 19 |
db close |
| 20 |
set ::testprefix sharedA |
| 21 |
|
| 22 |
ifcapable !shared_cache { |
| 23 |
finish_test |
| 24 |
return |
| 25 |
} |
| 26 |
|
| 27 |
if {[atomic_batch_write test.db]} { |
| 28 |
finish_test |
| 29 |
return |
| 30 |
} |
| 31 |
|
| 32 |
set ::enable_shared_cache [sqlite3_enable_shared_cache 1] |
| 33 |
|
| 34 |
#------------------------------------------------------------------------- |
| 35 |
# |
| 36 |
do_test 0.1 { |
| 37 |
sqlite3 db1 test.db |
| 38 |
sqlite3 db2 test.db |
| 39 |
|
| 40 |
db1 eval { |
| 41 |
CREATE TABLE t1(x); |
| 42 |
INSERT INTO t1 VALUES(randomblob(100)); |
| 43 |
INSERT INTO t1 SELECT randomblob(100) FROM t1; |
| 44 |
INSERT INTO t1 SELECT randomblob(100) FROM t1; |
| 45 |
INSERT INTO t1 SELECT randomblob(100) FROM t1; |
| 46 |
INSERT INTO t1 SELECT randomblob(100) FROM t1; |
| 47 |
INSERT INTO t1 SELECT randomblob(100) FROM t1; |
| 48 |
INSERT INTO t1 SELECT randomblob(100) FROM t1; |
| 49 |
CREATE INDEX i1 ON t1(x); |
| 50 |
} |
| 51 |
|
| 52 |
db1 eval { |
| 53 |
BEGIN; |
| 54 |
DROP INDEX i1; |
| 55 |
} |
| 56 |
|
| 57 |
db2 close |
| 58 |
|
| 59 |
db1 eval { |
| 60 |
INSERT INTO t1 SELECT randomblob(100) FROM t1; |
| 61 |
ROLLBACK; |
| 62 |
PRAGMA integrity_check; |
| 63 |
} |
| 64 |
} {ok} |
| 65 |
|
| 66 |
db1 close |
| 67 |
forcedelete test.db |
| 68 |
|
| 69 |
|
| 70 |
#------------------------------------------------------------------------- |
| 71 |
# |
| 72 |
do_test 1.1 { |
| 73 |
sqlite3 db1 test.db |
| 74 |
sqlite3 db2 test.db |
| 75 |
db2 eval { |
| 76 |
CREATE TABLE t1(x); |
| 77 |
INSERT INTO t1 VALUES(123); |
| 78 |
} |
| 79 |
db1 eval { |
| 80 |
SELECT * FROM t1; |
| 81 |
CREATE INDEX i1 ON t1(x); |
| 82 |
} |
| 83 |
} {123} |
| 84 |
|
| 85 |
do_test 1.2 { |
| 86 |
db2 eval { SELECT * FROM t1 ORDER BY x; } |
| 87 |
|
| 88 |
db1 eval { |
| 89 |
BEGIN; DROP INDEX i1; |
| 90 |
} |
| 91 |
db1 close |
| 92 |
|
| 93 |
db2 eval { SELECT * FROM t1 ORDER BY x; } |
| 94 |
} {123} |
| 95 |
|
| 96 |
do_test 1.3 { |
| 97 |
db2 close |
| 98 |
} {} |
| 99 |
|
| 100 |
#------------------------------------------------------------------------- |
| 101 |
# |
| 102 |
# sqlite3RollbackAll() loops through all attached b-trees and rolls |
| 103 |
# back each one separately. Then if the SQLITE_InternChanges flag is |
| 104 |
# set, it resets the schema. Both of the above steps must be done |
| 105 |
# while holding a mutex, otherwise another thread might slip in and |
| 106 |
# try to use the new schema with the old data. |
| 107 |
# |
| 108 |
# The following sequence of tests attempt to verify that the actions |
| 109 |
# taken by sqlite3RollbackAll() are thread-atomic (that they cannot be |
| 110 |
# interrupted by a separate thread.) |
| 111 |
# |
| 112 |
# Note that a TCL interpreter can only be used within the thread in which |
| 113 |
# it was originally created (because it uses thread-local-storage). |
| 114 |
# The tvfs callbacks must therefore only run on the main thread. |
| 115 |
# There is some trickery in the read_callback procedure to ensure that |
| 116 |
# this is the case. |
| 117 |
# |
| 118 |
testvfs tvfs |
| 119 |
|
| 120 |
# Set up two databases and two database connections. |
| 121 |
# |
| 122 |
# db1: main(test.db), two(test2.db) |
| 123 |
# db2: main(test.db) |
| 124 |
# |
| 125 |
# The cache for test.db is shared between db1 and db2. |
| 126 |
# |
| 127 |
do_test 2.1 { |
| 128 |
forcedelete test.db test.db2 |
| 129 |
sqlite3 db1 test.db -vfs tvfs |
| 130 |
db1 eval { ATTACH 'test.db2' AS two } |
| 131 |
|
| 132 |
db1 eval { |
| 133 |
CREATE TABLE t1(x); |
| 134 |
INSERT INTO t1 VALUES(1); |
| 135 |
INSERT INTO t1 VALUES(2); |
| 136 |
INSERT INTO t1 VALUES(3); |
| 137 |
CREATE TABLE two.t2(x); |
| 138 |
INSERT INTO t2 SELECT * FROM t1; |
| 139 |
} |
| 140 |
|
| 141 |
sqlite3 db2 test.db -vfs tvfs |
| 142 |
db2 eval { SELECT * FROM t1 } |
| 143 |
} {1 2 3} |
| 144 |
|
| 145 |
# Create a prepared statement on db2 that will attempt a schema change |
| 146 |
# in test.db. Meanwhile, start a transaction on db1 that changes |
| 147 |
# the schema of test.db and that creates a rollback journal on test2.db |
| 148 |
# |
| 149 |
do_test 2.2 { |
| 150 |
set ::STMT [sqlite3_prepare db2 "CREATE INDEX i1 ON t1(x)" -1 tail] |
| 151 |
db1 eval { |
| 152 |
BEGIN; |
| 153 |
CREATE INDEX i1 ON t1(x); |
| 154 |
INSERT INTO t2 VALUES('value!'); |
| 155 |
} |
| 156 |
} {} |
| 157 |
|
| 158 |
# Set up a callback that will cause db2 to try to execute its |
| 159 |
# schema change when db1 accesses the journal file of test2.db. |
| 160 |
# |
| 161 |
# This callback will be invoked after the content of test.db has |
| 162 |
# be rolled back but before the schema has been reset. If the |
| 163 |
# sqlite3RollbackAll() operation is not thread-atomic, then the |
| 164 |
# db2 statement in the callback will see old content with the newer |
| 165 |
# schema, which is wrong. |
| 166 |
# |
| 167 |
tvfs filter xRead |
| 168 |
tvfs script read_callback |
| 169 |
unset -nocomplain ::some_time_laster |
| 170 |
unset -nocomplain ::thread_result |
| 171 |
proc read_callback {call file args} { |
| 172 |
if {[string match *test.db2-journal $file]} { |
| 173 |
tvfs filter {} ;# Ensure that tvfs callbacks to do run on the |
| 174 |
# child thread |
| 175 |
sqlthread spawn ::thread_result [subst -nocommands { |
| 176 |
sqlite3_step $::STMT |
| 177 |
set rc [sqlite3_finalize $::STMT] |
| 178 |
}] |
| 179 |
after 1000 { set ::some_time_later 1 } |
| 180 |
vwait ::some_time_later |
| 181 |
} |
| 182 |
} |
| 183 |
do_test 2.3 { db1 eval ROLLBACK } {} |
| 184 |
|
| 185 |
# Verify that the db2 statement invoked by the callback detected the |
| 186 |
# schema change. |
| 187 |
# |
| 188 |
if {[info exists ::thread_result]==0} { vwait ::thread_result } |
| 189 |
do_test 2.4 { |
| 190 |
list $::thread_result [sqlite3_errmsg db2] |
| 191 |
} {SQLITE_SCHEMA {database schema has changed}} |
| 192 |
|
| 193 |
db1 close |
| 194 |
db2 close |
| 195 |
tvfs delete |
| 196 |
|
| 197 |
sqlite3_enable_shared_cache $::enable_shared_cache |
| 198 |
finish_test |