2008-07-30 13:15:43 +00:00
|
|
|
# 2007 April 6
|
|
|
|
#
|
|
|
|
# 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 script is database locks.
|
|
|
|
#
|
2009-05-08 09:27:02 +00:00
|
|
|
# $Id: lock4.test,v 1.10 2009/05/06 00:52:41 drh Exp $
|
2008-07-30 13:15:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
set testdir [file dirname $argv0]
|
|
|
|
source $testdir/tester.tcl
|
|
|
|
|
2010-07-22 13:16:22 +00:00
|
|
|
do_not_use_codec
|
|
|
|
|
2008-07-30 13:15:43 +00:00
|
|
|
# Initialize the test.db database so that it is non-empty
|
|
|
|
#
|
|
|
|
do_test lock4-1.1 {
|
|
|
|
db eval {
|
|
|
|
PRAGMA auto_vacuum=OFF;
|
|
|
|
CREATE TABLE t1(x);
|
|
|
|
}
|
2011-12-26 19:52:17 +00:00
|
|
|
forcedelete test2.db test2.db-journal
|
2008-07-30 13:15:43 +00:00
|
|
|
sqlite3 db2 test2.db
|
|
|
|
db2 eval {
|
|
|
|
PRAGMA auto_vacuum=OFF;
|
|
|
|
CREATE TABLE t2(x)
|
|
|
|
}
|
|
|
|
db2 close
|
|
|
|
list [file size test.db] [file size test2.db]
|
|
|
|
} {2048 2048}
|
|
|
|
|
|
|
|
# Create a script to drive a separate process that will
|
|
|
|
#
|
|
|
|
# 1. Create a second database test2.db
|
|
|
|
# 2. Get an exclusive lock on test2.db
|
|
|
|
# 3. Add an entry to test.db in table t1, waiting as necessary.
|
|
|
|
# 4. Commit the change to test2.db.
|
|
|
|
#
|
|
|
|
# Meanwhile, this process will:
|
|
|
|
#
|
|
|
|
# A. Get an exclusive lock on test.db
|
|
|
|
# B. Attempt to read from test2.db but get an SQLITE_BUSY error.
|
|
|
|
# C. Commit the changes to test.db thus alloing the other process
|
|
|
|
# to continue.
|
|
|
|
#
|
|
|
|
do_test lock4-1.2 {
|
|
|
|
|
|
|
|
# Create a script for the second process to run.
|
|
|
|
#
|
|
|
|
set out [open test2-script.tcl w]
|
2009-02-20 20:33:35 +00:00
|
|
|
puts $out "sqlite3_test_control_pending_byte [set sqlite_pending_byte]"
|
2008-07-30 13:15:43 +00:00
|
|
|
puts $out {
|
|
|
|
sqlite3 db2 test2.db
|
|
|
|
db2 eval {
|
|
|
|
BEGIN;
|
|
|
|
INSERT INTO t2 VALUES(2);
|
|
|
|
}
|
|
|
|
sqlite3 db test.db
|
|
|
|
db timeout 1000000
|
|
|
|
db eval {
|
|
|
|
INSERT INTO t1 VALUES(2);
|
|
|
|
}
|
|
|
|
db close
|
|
|
|
db2 eval COMMIT
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
close $out
|
|
|
|
|
|
|
|
# Begin a transaction on test.db.
|
|
|
|
db eval {
|
|
|
|
BEGIN EXCLUSIVE;
|
|
|
|
INSERT INTO t1 VALUES(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Kick off the second process.
|
|
|
|
exec [info nameofexec] ./test2-script.tcl &
|
|
|
|
|
|
|
|
# Wait until the second process has started its transaction on test2.db.
|
|
|
|
while {![file exists test2.db-journal]} {
|
|
|
|
after 10
|
|
|
|
}
|
|
|
|
|
|
|
|
# Try to write to test2.db. We are locked out.
|
|
|
|
sqlite3 db2 test2.db
|
|
|
|
catchsql {
|
|
|
|
INSERT INTO t2 VALUES(1)
|
|
|
|
} db2
|
|
|
|
} {1 {database is locked}}
|
|
|
|
do_test lock4-1.3 {
|
|
|
|
db eval {
|
|
|
|
COMMIT;
|
|
|
|
}
|
|
|
|
while {[file exists test2.db-journal]} {
|
|
|
|
after 10
|
|
|
|
}
|
|
|
|
# The other process has committed its transaction on test2.db by
|
|
|
|
# deleting the journal file. But it might retain the lock for a
|
|
|
|
# fraction longer
|
|
|
|
#
|
2009-05-08 09:27:02 +00:00
|
|
|
after 25
|
2008-07-30 13:15:43 +00:00
|
|
|
db2 eval {
|
|
|
|
SELECT * FROM t2
|
|
|
|
}
|
|
|
|
} {2}
|
|
|
|
|
|
|
|
|
|
|
|
do_test lock4-999.1 {
|
|
|
|
rename db2 {}
|
|
|
|
} {}
|
|
|
|
|
|
|
|
finish_test
|