Files
sqlite/test/shared.test
T
danielk1977 da18423620 Add the shared schema/pager modifications. Very few tests so far. (CVS 2859)
FossilOrigin-Name: deeda0dc06c1595aedd8d06a0c4e88a8abf78cf7
2006-01-05 11:34:32 +00:00

224 lines
6.5 KiB
Plaintext

# 2005 December 30
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. The
# focus of this file is testing the SELECT statement.
#
# $Id: shared.test,v 1.2 2006/01/05 11:34:34 danielk1977 Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
db close
ifcapable !shared_cache {
finish_test
return
}
set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
# Test organization:
#
# shared-1.*: Simple test to verify basic sanity of table level locking when
# two connections share a pager cache.
# shared-2.*: Test that a read transaction can co-exist with a
# write-transaction, including a simple test to ensure the
# external locking protocol is still working.
# shared-3.*: Simple test of read-uncommitted mode.
#
do_test shared-1.1 {
# Open a second database on the file test.db. It should use the same pager
# cache and schema as the original connection. Verify that only 1 file is
# opened.
sqlite3 db2 test.db
sqlite3 db test.db
set ::sqlite_open_file_count
} {1}
do_test shared-1.2 {
# Add a table and a single row of data via the first connection.
# Ensure that the second connection can see them.
execsql {
CREATE TABLE abc(a, b, c);
INSERT INTO abc VALUES(1, 2, 3);
} db
execsql {
SELECT * FROM abc;
} db2
} {1 2 3}
do_test shared-1.3 {
# Have the first connection begin a transaction and obtain a read-lock
# on table abc. This should not prevent the second connection from
# querying abc.
execsql {
BEGIN;
SELECT * FROM abc;
}
execsql {
SELECT * FROM abc;
} db2
} {1 2 3}
do_test shared-1.4 {
# Try to insert a row into abc via connection 2. This should fail because
# of the read-lock connection 1 is holding on table abc (obtained in the
# previous test case).
catchsql {
INSERT INTO abc VALUES(4, 5, 6);
} db2
} {1 {database is locked}}
do_test shared-1.5 {
# Using connection 2 (the one without the open transaction), try to create
# a new table. This should fail because of the open read transaction
# held by connection 1.
catchsql {
CREATE TABLE def(d, e, f);
} db2
} {1 {database is locked}}
do_test shared-1.6 {
# Upgrade connection 1's transaction to a write transaction. Create
# a new table - def - and insert a row into it. Because the connection 1
# transaction modifies the schema, it should not be possible for
# connection 2 to access the database at all until the connection 1
# has finished the transaction.
execsql {
CREATE TABLE def(d, e, f);
INSERT INTO def VALUES('IV', 'V', 'VI');
}
} {}
do_test shared-1.7 {
# Read from the sqlite_master table with connection 1 (inside the
# transaction). Then test that we can not do this with connection 2. This
# is because of the schema-modified lock established by connection 1
# in the previous test case.
execsql {
SELECT * FROM sqlite_master;
}
catchsql {
SELECT * FROM sqlite_master;
} db2
} {1 {database is locked}}
do_test shared-1.8 {
# Commit the connection 1 transaction.
execsql {
COMMIT;
}
} {}
do_test shared-2.1 {
# Open connection db3 to the database. Use a different path to the same
# file so that db3 does *not* share the same pager cache as db and db2
# (there should be two open file handles).
sqlite3 db3 ./test.db
set ::sqlite_open_file_count
} {2}
do_test shared-2.2 {
# Start read transactions on db and db2 (the shared pager cache). Ensure
# db3 cannot write to the database.
execsql {
BEGIN;
SELECT * FROM abc;
}
execsql {
BEGIN;
SELECT * FROM abc;
} db2
catchsql {
INSERT INTO abc VALUES(1, 2, 3);
} db2
} {1 {database is locked}}
do_test shared-2.3 {
# Turn db's transaction into a write-transaction. db3 should still be
# able to read from table def (but will not see the new row). Connection
# db2 should not be able to read def (because of the write-lock).
# Todo: The failed "INSERT INTO abc ..." statement in the above test
# has started a write-transaction on db2 (should this be so?). This
# would prevent connection db from starting a write-transaction. So roll the
# db2 transaction back and replace it with a new read transaction.
execsql {
ROLLBACK;
BEGIN;
SELECT * FROM abc;
} db2
execsql {
INSERT INTO def VALUES('VII', 'VIII', 'IX');
}
concat [
catchsql { SELECT * FROM def; } db3
] [
catchsql { SELECT * FROM def; } db2
]
} {0 {IV V VI} 1 {database is locked}}
do_test shared-2.4 {
# Commit the open transaction on db. db2 still holds a read-transaction.
# This should prevent db3 from writing to the database, but not from
# reading.
execsql {
COMMIT;
}
concat [
catchsql { SELECT * FROM def; } db3
] [
catchsql { INSERT INTO def VALUES('X', 'XI', 'XII'); } db3
]
} {0 {IV V VI VII VIII IX} 1 {database is locked}}
catchsql COMMIT db2
do_test shared-3.1.1 {
# This test case starts a linear scan of table 'seq' using a
# read-uncommitted connection. In the middle of the scan, rows are added
# to the end of the seq table (ahead of the current cursor position).
# The uncommitted rows should be included in the results of the scan.
execsql "
CREATE TABLE seq(i, x);
INSERT INTO seq VALUES(1, '[string repeat X 500]');
INSERT INTO seq VALUES(2, '[string repeat X 500]');
"
execsql {SELECT * FROM sqlite_master} db2
execsql {PRAGMA read_uncommitted = 1} db2
set ret [list]
db2 eval {SELECT i FROM seq} {
if {$i < 4} {
execsql {
INSERT INTO seq SELECT i + (SELECT max(i) FROM seq), x FROM seq;
}
}
lappend ret $i
}
set ret
} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
do_test shared-3.1.2 {
# Another linear scan through table seq using a read-uncommitted connection.
# This time, delete each row as it is read. Should not affect the results of
# the scan, but the table should be empty after the scan is concluded
# (test 3.1.3 verifies this).
set ret [list]
db2 eval {SELECT i FROM seq} {
db eval {DELETE FROM seq WHERE i = $i}
lappend ret $i
}
set ret
} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
do_test shared-3.1.3 {
execsql {
SELECT * FROM seq;
}
} {}
catch {db close}
catch {db2 close}
catch {db3 close}
finish_test
sqlite3_enable_shared_cache $::enable_shared_cache