danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1 | # 2005 December 30 |
| 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 | # This file implements regression tests for SQLite library. The |
| 12 | # focus of this file is testing the SELECT statement. |
| 13 | # |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame^] | 14 | # $Id: shared.test,v 1.4 2006/01/06 13:00:30 danielk1977 Exp $ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 15 | |
| 16 | set testdir [file dirname $argv0] |
| 17 | source $testdir/tester.tcl |
| 18 | db close |
| 19 | |
| 20 | ifcapable !shared_cache { |
| 21 | finish_test |
| 22 | return |
| 23 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 24 | set ::enable_shared_cache [sqlite3_enable_shared_cache 1] |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 25 | |
| 26 | # Test organization: |
| 27 | # |
| 28 | # shared-1.*: Simple test to verify basic sanity of table level locking when |
| 29 | # two connections share a pager cache. |
| 30 | # shared-2.*: Test that a read transaction can co-exist with a |
| 31 | # write-transaction, including a simple test to ensure the |
| 32 | # external locking protocol is still working. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 33 | # shared-3.*: Simple test of read-uncommitted mode. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 34 | # |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 35 | # shared-4.*: Check that the schema is locked and unlocked correctly. |
| 36 | # |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 37 | |
| 38 | do_test shared-1.1 { |
| 39 | # Open a second database on the file test.db. It should use the same pager |
| 40 | # cache and schema as the original connection. Verify that only 1 file is |
| 41 | # opened. |
| 42 | sqlite3 db2 test.db |
| 43 | sqlite3 db test.db |
| 44 | set ::sqlite_open_file_count |
| 45 | } {1} |
| 46 | do_test shared-1.2 { |
| 47 | # Add a table and a single row of data via the first connection. |
| 48 | # Ensure that the second connection can see them. |
| 49 | execsql { |
| 50 | CREATE TABLE abc(a, b, c); |
| 51 | INSERT INTO abc VALUES(1, 2, 3); |
| 52 | } db |
| 53 | execsql { |
| 54 | SELECT * FROM abc; |
| 55 | } db2 |
| 56 | } {1 2 3} |
| 57 | do_test shared-1.3 { |
| 58 | # Have the first connection begin a transaction and obtain a read-lock |
| 59 | # on table abc. This should not prevent the second connection from |
| 60 | # querying abc. |
| 61 | execsql { |
| 62 | BEGIN; |
| 63 | SELECT * FROM abc; |
| 64 | } |
| 65 | execsql { |
| 66 | SELECT * FROM abc; |
| 67 | } db2 |
| 68 | } {1 2 3} |
| 69 | do_test shared-1.4 { |
| 70 | # Try to insert a row into abc via connection 2. This should fail because |
| 71 | # of the read-lock connection 1 is holding on table abc (obtained in the |
| 72 | # previous test case). |
| 73 | catchsql { |
| 74 | INSERT INTO abc VALUES(4, 5, 6); |
| 75 | } db2 |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame^] | 76 | } {1 {database table is locked}} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 77 | do_test shared-1.5 { |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 78 | # Using connection 2 (the one without the open transaction), try to create |
| 79 | # a new table. This should fail because of the open read transaction |
| 80 | # held by connection 1. |
| 81 | catchsql { |
| 82 | CREATE TABLE def(d, e, f); |
| 83 | } db2 |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame^] | 84 | } {1 {database table is locked}} |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 85 | do_test shared-1.6 { |
| 86 | # Upgrade connection 1's transaction to a write transaction. Create |
| 87 | # a new table - def - and insert a row into it. Because the connection 1 |
| 88 | # transaction modifies the schema, it should not be possible for |
| 89 | # connection 2 to access the database at all until the connection 1 |
| 90 | # has finished the transaction. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 91 | execsql { |
| 92 | CREATE TABLE def(d, e, f); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 93 | INSERT INTO def VALUES('IV', 'V', 'VI'); |
| 94 | } |
| 95 | } {} |
| 96 | do_test shared-1.7 { |
| 97 | # Read from the sqlite_master table with connection 1 (inside the |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 98 | # transaction). Then test that we can not do this with connection 2. This |
| 99 | # is because of the schema-modified lock established by connection 1 |
| 100 | # in the previous test case. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 101 | execsql { |
| 102 | SELECT * FROM sqlite_master; |
| 103 | } |
| 104 | catchsql { |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 105 | SELECT * FROM sqlite_master; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 106 | } db2 |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame^] | 107 | } {1 {database schema is locked: main}} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 108 | do_test shared-1.8 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 109 | # Commit the connection 1 transaction. |
| 110 | execsql { |
| 111 | COMMIT; |
| 112 | } |
| 113 | } {} |
| 114 | |
| 115 | do_test shared-2.1 { |
| 116 | # Open connection db3 to the database. Use a different path to the same |
| 117 | # file so that db3 does *not* share the same pager cache as db and db2 |
| 118 | # (there should be two open file handles). |
| 119 | sqlite3 db3 ./test.db |
| 120 | set ::sqlite_open_file_count |
| 121 | } {2} |
| 122 | do_test shared-2.2 { |
| 123 | # Start read transactions on db and db2 (the shared pager cache). Ensure |
| 124 | # db3 cannot write to the database. |
| 125 | execsql { |
| 126 | BEGIN; |
| 127 | SELECT * FROM abc; |
| 128 | } |
| 129 | execsql { |
| 130 | BEGIN; |
| 131 | SELECT * FROM abc; |
| 132 | } db2 |
| 133 | catchsql { |
| 134 | INSERT INTO abc VALUES(1, 2, 3); |
| 135 | } db2 |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame^] | 136 | } {1 {database table is locked}} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 137 | do_test shared-2.3 { |
| 138 | # Turn db's transaction into a write-transaction. db3 should still be |
| 139 | # able to read from table def (but will not see the new row). Connection |
| 140 | # db2 should not be able to read def (because of the write-lock). |
| 141 | |
| 142 | # Todo: The failed "INSERT INTO abc ..." statement in the above test |
| 143 | # has started a write-transaction on db2 (should this be so?). This |
| 144 | # would prevent connection db from starting a write-transaction. So roll the |
| 145 | # db2 transaction back and replace it with a new read transaction. |
| 146 | execsql { |
| 147 | ROLLBACK; |
| 148 | BEGIN; |
| 149 | SELECT * FROM abc; |
| 150 | } db2 |
| 151 | |
| 152 | execsql { |
| 153 | INSERT INTO def VALUES('VII', 'VIII', 'IX'); |
| 154 | } |
| 155 | concat [ |
| 156 | catchsql { SELECT * FROM def; } db3 |
| 157 | ] [ |
| 158 | catchsql { SELECT * FROM def; } db2 |
| 159 | ] |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame^] | 160 | } {0 {IV V VI} 1 {database table is locked}} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 161 | do_test shared-2.4 { |
| 162 | # Commit the open transaction on db. db2 still holds a read-transaction. |
| 163 | # This should prevent db3 from writing to the database, but not from |
| 164 | # reading. |
| 165 | execsql { |
| 166 | COMMIT; |
| 167 | } |
| 168 | concat [ |
| 169 | catchsql { SELECT * FROM def; } db3 |
| 170 | ] [ |
| 171 | catchsql { INSERT INTO def VALUES('X', 'XI', 'XII'); } db3 |
| 172 | ] |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 173 | } {0 {IV V VI VII VIII IX} 1 {database is locked}} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 174 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 175 | catchsql COMMIT db2 |
| 176 | |
| 177 | do_test shared-3.1.1 { |
| 178 | # This test case starts a linear scan of table 'seq' using a |
| 179 | # read-uncommitted connection. In the middle of the scan, rows are added |
| 180 | # to the end of the seq table (ahead of the current cursor position). |
| 181 | # The uncommitted rows should be included in the results of the scan. |
| 182 | execsql " |
| 183 | CREATE TABLE seq(i, x); |
| 184 | INSERT INTO seq VALUES(1, '[string repeat X 500]'); |
| 185 | INSERT INTO seq VALUES(2, '[string repeat X 500]'); |
| 186 | " |
| 187 | execsql {SELECT * FROM sqlite_master} db2 |
| 188 | execsql {PRAGMA read_uncommitted = 1} db2 |
| 189 | |
| 190 | set ret [list] |
| 191 | db2 eval {SELECT i FROM seq} { |
| 192 | if {$i < 4} { |
| 193 | execsql { |
| 194 | INSERT INTO seq SELECT i + (SELECT max(i) FROM seq), x FROM seq; |
| 195 | } |
| 196 | } |
| 197 | lappend ret $i |
| 198 | } |
| 199 | set ret |
| 200 | } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} |
| 201 | do_test shared-3.1.2 { |
| 202 | # Another linear scan through table seq using a read-uncommitted connection. |
| 203 | # This time, delete each row as it is read. Should not affect the results of |
| 204 | # the scan, but the table should be empty after the scan is concluded |
| 205 | # (test 3.1.3 verifies this). |
| 206 | set ret [list] |
| 207 | db2 eval {SELECT i FROM seq} { |
| 208 | db eval {DELETE FROM seq WHERE i = $i} |
| 209 | lappend ret $i |
| 210 | } |
| 211 | set ret |
| 212 | } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} |
| 213 | do_test shared-3.1.3 { |
| 214 | execsql { |
| 215 | SELECT * FROM seq; |
| 216 | } |
| 217 | } {} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 218 | |
| 219 | catch {db close} |
| 220 | catch {db2 close} |
| 221 | catch {db3 close} |
| 222 | |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 223 | #-------------------------------------------------------------------------- |
| 224 | # Tests shared-4.* test that the schema locking rules are applied |
| 225 | # correctly. i.e.: |
| 226 | # |
| 227 | # 1. All transactions require a read-lock on the schemas of databases they |
| 228 | # access. |
| 229 | # 2. Transactions that modify a database schema require a write-lock on that |
| 230 | # schema. |
| 231 | # 3. It is not possible to compile a statement while another handle has a |
| 232 | # write-lock on the schema. |
| 233 | # |
| 234 | |
| 235 | # Open two database handles db and db2. Each has a single attach database |
| 236 | # (as well as main): |
| 237 | # |
| 238 | # db.main -> ./test.db |
| 239 | # db.test2 -> ./test2.db |
| 240 | # db2.main -> ./test2.db |
| 241 | # db2.test -> ./test.db |
| 242 | # |
| 243 | file delete -force test.db |
| 244 | file delete -force test2.db |
| 245 | file delete -force test2.db-journal |
| 246 | sqlite3 db test.db |
| 247 | sqlite3 db2 test2.db |
| 248 | do_test shared-4.1.1 { |
| 249 | set sqlite_open_file_count |
| 250 | } {2} |
| 251 | do_test shared-4.1.2 { |
| 252 | execsql {ATTACH 'test2.db' AS test2} |
| 253 | set sqlite_open_file_count |
| 254 | } {2} |
| 255 | do_test shared-4.1.3 { |
| 256 | execsql {ATTACH 'test.db' AS test} db2 |
| 257 | set sqlite_open_file_count |
| 258 | } {2} |
| 259 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame^] | 260 | # Sanity check: Create a table in ./test.db via handle db, and test that handle |
| 261 | # db2 can "see" the new table immediately. A handle using a seperate pager |
| 262 | # cache would have to reload the database schema before this were possible. |
| 263 | # |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 264 | do_test shared-4.2.1 { |
| 265 | execsql { |
| 266 | CREATE TABLE abc(a, b, c); |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame^] | 267 | CREATE TABLE def(d, e, f); |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 268 | INSERT INTO abc VALUES('i', 'ii', 'iii'); |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame^] | 269 | INSERT INTO def VALUES('I', 'II', 'III'); |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 270 | } |
| 271 | } {} |
| 272 | do_test shared-4.2.2 { |
| 273 | execsql { |
| 274 | SELECT * FROM test.abc; |
| 275 | } db2 |
| 276 | } {i ii iii} |
| 277 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame^] | 278 | # Open a read-transaction and read from table abc via handle 2. Check that |
| 279 | # handle 1 can read table abc. Check that handle 1 cannot modify table abc |
| 280 | # or the database schema. Then check that handle 1 can modify table def. |
| 281 | # |
| 282 | do_test shared-4.3.1 { |
| 283 | execsql { |
| 284 | BEGIN; |
| 285 | SELECT * FROM test.abc; |
| 286 | } db2 |
| 287 | } {i ii iii} |
| 288 | do_test shared-4.3.2 { |
| 289 | catchsql { |
| 290 | INSERT INTO abc VALUES('iv', 'v', 'vi'); |
| 291 | } |
| 292 | } {1 {database table is locked}} |
| 293 | do_test shared-4.3.3 { |
| 294 | catchsql { |
| 295 | CREATE TABLE ghi(g, h, i); |
| 296 | } |
| 297 | } {1 {database table is locked}} |
| 298 | do_test shared-4.3.3 { |
| 299 | catchsql { |
| 300 | INSERT INTO def VALUES('IV', 'V', 'VI'); |
| 301 | } |
| 302 | } {0 {}} |
| 303 | do_test shared-4.3.4 { |
| 304 | # Cleanup: commit the transaction opened by db2. |
| 305 | execsql { |
| 306 | COMMIT |
| 307 | } db2 |
| 308 | } {} |
| 309 | |
| 310 | # Open a write-transaction using handle 1 and modify the database schema. |
| 311 | # Then try to execute a compiled statement to read from the same |
| 312 | # database via handle 2 (fails to get the lock on sqlite_master). Also |
| 313 | # try to compile a read of the same database using handle 2 (also fails). |
| 314 | # Finally, compile a read of the other database using handle 2. This |
| 315 | # should also fail. |
| 316 | # |
| 317 | do_test shared-4.4.1.2 { |
| 318 | # Sanity check 1: Check that the schema is what we think it is when viewed |
| 319 | # via handle 1. |
| 320 | execsql { |
| 321 | CREATE TABLE test2.ghi(g, h, i); |
| 322 | SELECT 'test.db:'||name FROM sqlite_master |
| 323 | UNION ALL |
| 324 | SELECT 'test2.db:'||name FROM test2.sqlite_master; |
| 325 | } |
| 326 | } {test.db:abc test.db:def test2.db:ghi} |
| 327 | do_test shared-4.4.1.2 { |
| 328 | # Sanity check 2: Check that the schema is what we think it is when viewed |
| 329 | # via handle 2. |
| 330 | execsql { |
| 331 | SELECT 'test2.db:'||name FROM sqlite_master |
| 332 | UNION ALL |
| 333 | SELECT 'test.db:'||name FROM test.sqlite_master; |
| 334 | } db2 |
| 335 | } {test2.db:ghi test.db:abc test.db:def} |
| 336 | |
| 337 | do_test shared-4.4.2 { |
| 338 | set ::DB2 [sqlite3_connection_pointer db2] |
| 339 | set sql {SELECT * FROM abc} |
| 340 | set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY] |
| 341 | execsql { |
| 342 | BEGIN; |
| 343 | CREATE TABLE jkl(j, k, l); |
| 344 | } |
| 345 | sqlite3_step $::STMT1 |
| 346 | } {SQLITE_ERROR} |
| 347 | do_test shared-4.4.3 { |
| 348 | sqlite3_finalize $::STMT1 |
| 349 | } {SQLITE_LOCKED} |
| 350 | do_test shared-4.4.4 { |
| 351 | set rc [catch { |
| 352 | set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY] |
| 353 | } msg] |
| 354 | list $rc $msg |
| 355 | } {1 {(6) database schema is locked: test}} |
| 356 | do_test shared-4.4.5 { |
| 357 | set rc [catch { |
| 358 | set ::STMT1 [sqlite3_prepare $::DB2 "SELECT * FROM ghi" -1 DUMMY] |
| 359 | } msg] |
| 360 | list $rc $msg |
| 361 | } {1 {(6) database schema is locked: test}} |
| 362 | |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 363 | catch {db2 close} |
| 364 | catch {db close} |
| 365 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 366 | finish_test |
| 367 | sqlite3_enable_shared_cache $::enable_shared_cache |
| 368 | |