1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/tkt2854.test Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,149 @@
1.4 +# 2007 December 20
1.5 +#
1.6 +# The author disclaims copyright to this source code. In place of
1.7 +# a legal notice, here is a blessing:
1.8 +#
1.9 +# May you do good and not evil.
1.10 +# May you find forgiveness for yourself and forgive others.
1.11 +# May you share freely, never taking more than you give.
1.12 +#
1.13 +#***********************************************************************
1.14 +#
1.15 +# $Id: tkt2854.test,v 1.3 2008/07/12 14:52:21 drh Exp $
1.16 +
1.17 +set testdir [file dirname $argv0]
1.18 +source $testdir/tester.tcl
1.19 +db close
1.20 +
1.21 +ifcapable !shared_cache {
1.22 + finish_test
1.23 + return
1.24 +}
1.25 +
1.26 +set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
1.27 +
1.28 +# Open 3 database connections. Connection "db" and "db2" share a cache.
1.29 +# Connection "db3" has its own cache.
1.30 +#
1.31 +do_test tkt2854-1.1 {
1.32 + sqlite3 db test.db
1.33 + sqlite3 db2 test.db
1.34 +
1.35 + # This is taken from shared.test. The Windows VFS expands
1.36 + # ./test.db (and test.db) to be the same thing so the path
1.37 + # matches and they share a cache. By changing the case
1.38 + # for Windows platform, we get around this and get a separate
1.39 + # connection.
1.40 + if {$::tcl_platform(platform)=="unix"} {
1.41 + sqlite3 db3 ./test.db
1.42 + } else {
1.43 + sqlite3 db3 TEST.DB
1.44 + }
1.45 +
1.46 + db eval {
1.47 + CREATE TABLE abc(a, b, c);
1.48 + }
1.49 +} {}
1.50 +
1.51 +# Check that an exclusive lock cannot be obtained if some other
1.52 +# shared-cache connection has a read-lock on a table.
1.53 +#
1.54 +do_test tkt2854-1.2 {
1.55 + execsql {
1.56 + BEGIN;
1.57 + SELECT * FROM abc;
1.58 + } db2
1.59 +} {}
1.60 +do_test tkt2854-1.3 {
1.61 + catchsql { BEGIN EXCLUSIVE } db
1.62 +} {1 {database is locked}}
1.63 +do_test tkt2854-1.4 {
1.64 + execsql { SELECT * FROM abc } db3
1.65 +} {}
1.66 +do_test tkt2854-1.5 {
1.67 + catchsql { INSERT INTO abc VALUES(1, 2, 3) } db3
1.68 +} {1 {database is locked}}
1.69 +do_test tkt2854-1.6 {
1.70 + execsql { COMMIT } db2
1.71 +} {}
1.72 +
1.73 +# Check that an exclusive lock prevents other shared-cache users from
1.74 +# starting a transaction.
1.75 +#
1.76 +do_test tkt2854-1.7 {
1.77 + set ::DB2 [sqlite3_connection_pointer db2]
1.78 + set ::STMT1 [sqlite3_prepare $DB2 "SELECT * FROM abc" -1 TAIL]
1.79 + set ::STMT2 [sqlite3_prepare $DB2 "BEGIN EXCLUSIVE" -1 TAIL]
1.80 + set ::STMT3 [sqlite3_prepare $DB2 "BEGIN IMMEDIATE" -1 TAIL]
1.81 + set ::STMT4 [sqlite3_prepare $DB2 "BEGIN" -1 TAIL]
1.82 + set ::STMT5 [sqlite3_prepare $DB2 "COMMIT" -1 TAIL]
1.83 + execsql { BEGIN EXCLUSIVE } db
1.84 +} {}
1.85 +do_test tkt2854-1.8 {
1.86 + catchsql { BEGIN EXCLUSIVE } db2
1.87 +} {1 {database schema is locked: main}}
1.88 +do_test tkt2854-1.9 {
1.89 + catchsql { BEGIN IMMEDIATE } db2
1.90 +} {1 {database schema is locked: main}}
1.91 +do_test tkt2854-1.10 {
1.92 + # This fails because the schema of main cannot be verified.
1.93 + catchsql { BEGIN } db2
1.94 +} {1 {database schema is locked: main}}
1.95 +
1.96 +# Check that an exclusive lock prevents other shared-cache users from
1.97 +# reading the database. Use stored statements so that the error occurs
1.98 +# at the b-tree level, not the schema level.
1.99 +#
1.100 +do_test tkt2854-1.11 {
1.101 + list [sqlite3_step $::STMT1] [sqlite3_finalize $::STMT1]
1.102 +} {SQLITE_ERROR SQLITE_LOCKED}
1.103 +do_test tkt2854-1.12 {
1.104 + list [sqlite3_step $::STMT2] [sqlite3_finalize $::STMT2]
1.105 +} {SQLITE_BUSY SQLITE_BUSY}
1.106 +do_test tkt2854-1.13 {
1.107 + list [sqlite3_step $::STMT3] [sqlite3_finalize $::STMT3]
1.108 +} {SQLITE_BUSY SQLITE_BUSY}
1.109 +do_test tkt2854-1.14 {
1.110 + # A regular "BEGIN" doesn't touch any databases. So it succeeds.
1.111 + list [sqlite3_step $::STMT4] [sqlite3_finalize $::STMT4]
1.112 +} {SQLITE_DONE SQLITE_OK}
1.113 +do_test tkt2854-1.15 {
1.114 + # As does a COMMIT.
1.115 + list [sqlite3_step $::STMT5] [sqlite3_finalize $::STMT5]
1.116 +} {SQLITE_DONE SQLITE_OK}
1.117 +
1.118 +# Try to read the database using connection "db3" (which does not share
1.119 +# a cache with "db"). The database should be locked.
1.120 +do_test tkt2854-1.16 {
1.121 + catchsql { SELECT * FROM abc } db3
1.122 +} {1 {database is locked}}
1.123 +do_test tkt2854-1.17 {
1.124 + execsql { COMMIT } db
1.125 +} {}
1.126 +do_test tkt2854-1.18 {
1.127 + execsql { SELECT * FROM abc } db2
1.128 +} {}
1.129 +
1.130 +# Check that if an attempt to obtain an exclusive lock fails because an
1.131 +# attached db cannot be locked, the internal exclusive flag used by
1.132 +# shared-cache users is correctly cleared.
1.133 +do_test tkt2854-1.19 {
1.134 + file delete -force test2.db test2.db-journal
1.135 + sqlite3 db4 test2.db
1.136 + execsql { CREATE TABLE def(d, e, f) } db4
1.137 + execsql { ATTACH 'test2.db' AS aux } db
1.138 +} {}
1.139 +do_test tkt2854-1.20 {
1.140 + execsql {BEGIN IMMEDIATE} db4
1.141 + catchsql {BEGIN EXCLUSIVE} db
1.142 +} {1 {database is locked}}
1.143 +do_test tkt2854-1.21 {
1.144 + execsql {SELECT * FROM abc} db2
1.145 +} {}
1.146 +
1.147 +db close
1.148 +db2 close
1.149 +db3 close
1.150 +db4 close
1.151 +sqlite3_enable_shared_cache $::enable_shared_cache
1.152 +finish_test