sl@0: # 2007 December 20
sl@0: #
sl@0: # The author disclaims copyright to this source code.  In place of
sl@0: # a legal notice, here is a blessing:
sl@0: #
sl@0: #    May you do good and not evil.
sl@0: #    May you find forgiveness for yourself and forgive others.
sl@0: #    May you share freely, never taking more than you give.
sl@0: #
sl@0: #***********************************************************************
sl@0: #
sl@0: # $Id: tkt2854.test,v 1.3 2008/07/12 14:52:21 drh Exp $
sl@0: 
sl@0: set testdir [file dirname $argv0]
sl@0: source $testdir/tester.tcl
sl@0: db close
sl@0: 
sl@0: ifcapable !shared_cache {
sl@0:   finish_test
sl@0:   return
sl@0: }
sl@0: 
sl@0: set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
sl@0: 
sl@0: # Open 3 database connections. Connection "db" and "db2" share a cache.
sl@0: # Connection "db3" has its own cache.
sl@0: #
sl@0: do_test tkt2854-1.1 {
sl@0:   sqlite3 db test.db
sl@0:   sqlite3 db2 test.db
sl@0: 
sl@0:   # This is taken from shared.test.  The Windows VFS expands 
sl@0:   # ./test.db (and test.db) to be the same thing so the path
sl@0:   # matches and they share a cache.  By changing the case 
sl@0:   # for Windows platform, we get around this and get a separate
sl@0:   # connection.
sl@0:   if {$::tcl_platform(platform)=="unix"} {
sl@0:     sqlite3 db3 ./test.db
sl@0:   } else {
sl@0:     sqlite3 db3 TEST.DB
sl@0:   }
sl@0: 
sl@0:   db eval {
sl@0:     CREATE TABLE abc(a, b, c);
sl@0:   }
sl@0: } {}
sl@0: 
sl@0: # Check that an exclusive lock cannot be obtained if some other 
sl@0: # shared-cache connection has a read-lock on a table.
sl@0: #
sl@0: do_test tkt2854-1.2 {
sl@0:   execsql { 
sl@0:     BEGIN;
sl@0:     SELECT * FROM abc;
sl@0:   } db2
sl@0: } {}
sl@0: do_test tkt2854-1.3 {
sl@0:   catchsql { BEGIN EXCLUSIVE } db
sl@0: } {1 {database is locked}}
sl@0: do_test tkt2854-1.4 {
sl@0:   execsql { SELECT * FROM abc } db3
sl@0: } {}
sl@0: do_test tkt2854-1.5 {
sl@0:   catchsql { INSERT INTO abc VALUES(1, 2, 3) } db3
sl@0: } {1 {database is locked}}
sl@0: do_test tkt2854-1.6 {
sl@0:   execsql { COMMIT } db2
sl@0: } {}
sl@0: 
sl@0: # Check that an exclusive lock prevents other shared-cache users from
sl@0: # starting a transaction.
sl@0: #
sl@0: do_test tkt2854-1.7 {
sl@0:   set ::DB2 [sqlite3_connection_pointer db2]
sl@0:   set ::STMT1 [sqlite3_prepare $DB2 "SELECT * FROM abc" -1 TAIL]
sl@0:   set ::STMT2 [sqlite3_prepare $DB2 "BEGIN EXCLUSIVE" -1 TAIL]
sl@0:   set ::STMT3 [sqlite3_prepare $DB2 "BEGIN IMMEDIATE" -1 TAIL]
sl@0:   set ::STMT4 [sqlite3_prepare $DB2 "BEGIN" -1 TAIL]
sl@0:   set ::STMT5 [sqlite3_prepare $DB2 "COMMIT" -1 TAIL]
sl@0:   execsql { BEGIN EXCLUSIVE } db
sl@0: } {}
sl@0: do_test tkt2854-1.8 {
sl@0:   catchsql { BEGIN EXCLUSIVE } db2
sl@0: } {1 {database schema is locked: main}}
sl@0: do_test tkt2854-1.9 {
sl@0:   catchsql { BEGIN IMMEDIATE } db2
sl@0: } {1 {database schema is locked: main}}
sl@0: do_test tkt2854-1.10 {
sl@0:   # This fails because the schema of main cannot be verified.
sl@0:   catchsql { BEGIN } db2
sl@0: } {1 {database schema is locked: main}}
sl@0: 
sl@0: # Check that an exclusive lock prevents other shared-cache users from
sl@0: # reading the database. Use stored statements so that the error occurs
sl@0: # at the b-tree level, not the schema level.
sl@0: #
sl@0: do_test tkt2854-1.11 {
sl@0:   list [sqlite3_step $::STMT1] [sqlite3_finalize $::STMT1]
sl@0: } {SQLITE_ERROR SQLITE_LOCKED}
sl@0: do_test tkt2854-1.12 {
sl@0:   list [sqlite3_step $::STMT2] [sqlite3_finalize $::STMT2]
sl@0: } {SQLITE_BUSY SQLITE_BUSY}
sl@0: do_test tkt2854-1.13 {
sl@0:   list [sqlite3_step $::STMT3] [sqlite3_finalize $::STMT3]
sl@0: } {SQLITE_BUSY SQLITE_BUSY}
sl@0: do_test tkt2854-1.14 {
sl@0:   # A regular "BEGIN" doesn't touch any databases. So it succeeds.
sl@0:   list [sqlite3_step $::STMT4] [sqlite3_finalize $::STMT4]
sl@0: } {SQLITE_DONE SQLITE_OK}
sl@0: do_test tkt2854-1.15 {
sl@0:   # As does a COMMIT.
sl@0:   list [sqlite3_step $::STMT5] [sqlite3_finalize $::STMT5]
sl@0: } {SQLITE_DONE SQLITE_OK}
sl@0: 
sl@0: # Try to read the database using connection "db3" (which does not share
sl@0: # a cache with "db"). The database should be locked.
sl@0: do_test tkt2854-1.16 {
sl@0:   catchsql { SELECT * FROM abc } db3
sl@0: } {1 {database is locked}}
sl@0: do_test tkt2854-1.17 {
sl@0:   execsql { COMMIT } db
sl@0: } {}
sl@0: do_test tkt2854-1.18 {
sl@0:   execsql { SELECT * FROM abc } db2
sl@0: } {}
sl@0: 
sl@0: # Check that if an attempt to obtain an exclusive lock fails because an
sl@0: # attached db cannot be locked, the internal exclusive flag used by
sl@0: # shared-cache users is correctly cleared.
sl@0: do_test tkt2854-1.19 {
sl@0:   file delete -force test2.db test2.db-journal
sl@0:   sqlite3 db4 test2.db
sl@0:   execsql { CREATE TABLE def(d, e, f) } db4
sl@0:   execsql { ATTACH 'test2.db' AS aux } db
sl@0: } {}
sl@0: do_test tkt2854-1.20 {
sl@0:   execsql {BEGIN IMMEDIATE} db4
sl@0:   catchsql {BEGIN EXCLUSIVE} db
sl@0: } {1 {database is locked}}
sl@0: do_test tkt2854-1.21 {
sl@0:   execsql {SELECT * FROM abc} db2
sl@0: } {}
sl@0: 
sl@0: db close
sl@0: db2 close
sl@0: db3 close
sl@0: db4 close
sl@0: sqlite3_enable_shared_cache $::enable_shared_cache
sl@0: finish_test