blob: b0b1c74fbec6186ec5277efabc3a79e7c30fa761 [file] [log] [blame]
drhbb5f18d2007-04-06 18:23:17 +00001# 2007 April 6
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 script is database locks.
13#
drh310bd202009-05-06 00:52:40 +000014# $Id: lock4.test,v 1.10 2009/05/06 00:52:41 drh Exp $
drhbb5f18d2007-04-06 18:23:17 +000015
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
dan68928b62010-06-22 13:46:43 +000020do_not_use_codec
21
drhbb5f18d2007-04-06 18:23:17 +000022# Initialize the test.db database so that it is non-empty
23#
24do_test lock4-1.1 {
drh1e9daa62007-04-06 21:42:22 +000025 db eval {
26 PRAGMA auto_vacuum=OFF;
27 CREATE TABLE t1(x);
28 }
mistachkinfda06be2011-08-02 00:57:34 +000029 forcedelete test2.db test2.db-journal
drhbb5f18d2007-04-06 18:23:17 +000030 sqlite3 db2 test2.db
drh1e9daa62007-04-06 21:42:22 +000031 db2 eval {
32 PRAGMA auto_vacuum=OFF;
33 CREATE TABLE t2(x)
34 }
drhbb5f18d2007-04-06 18:23:17 +000035 db2 close
36 list [file size test.db] [file size test2.db]
37} {2048 2048}
38
39# Create a script to drive a separate process that will
40#
41# 1. Create a second database test2.db
42# 2. Get an exclusive lock on test2.db
43# 3. Add an entry to test.db in table t1, waiting as necessary.
44# 4. Commit the change to test2.db.
45#
46# Meanwhile, this process will:
47#
48# A. Get an exclusive lock on test.db
49# B. Attempt to read from test2.db but get an SQLITE_BUSY error.
50# C. Commit the changes to test.db thus alloing the other process
51# to continue.
52#
53do_test lock4-1.2 {
danielk1977d5fe8d62008-03-14 08:57:41 +000054
55 # Create a script for the second process to run.
56 #
drhbb5f18d2007-04-06 18:23:17 +000057 set out [open test2-script.tcl w]
drhc7a3bb92009-02-05 16:31:45 +000058 puts $out "sqlite3_test_control_pending_byte [set sqlite_pending_byte]"
drhbb5f18d2007-04-06 18:23:17 +000059 puts $out {
60 sqlite3 db2 test2.db
61 db2 eval {
62 BEGIN;
63 INSERT INTO t2 VALUES(2);
64 }
65 sqlite3 db test.db
66 db timeout 1000000
67 db eval {
68 INSERT INTO t1 VALUES(2);
69 }
danielk19774ffb7b92008-03-13 04:53:52 +000070 db close
drhbb5f18d2007-04-06 18:23:17 +000071 db2 eval COMMIT
72 exit
73 }
74 close $out
danielk1977d5fe8d62008-03-14 08:57:41 +000075
76 # Begin a transaction on test.db.
drhbb5f18d2007-04-06 18:23:17 +000077 db eval {
danielk1977d5fe8d62008-03-14 08:57:41 +000078 BEGIN EXCLUSIVE;
drhbb5f18d2007-04-06 18:23:17 +000079 INSERT INTO t1 VALUES(1);
80 }
danielk1977d5fe8d62008-03-14 08:57:41 +000081
82 # Kick off the second process.
drhbb5f18d2007-04-06 18:23:17 +000083 exec [info nameofexec] ./test2-script.tcl &
danielk1977d5fe8d62008-03-14 08:57:41 +000084
85 # Wait until the second process has started its transaction on test2.db.
drhbb5f18d2007-04-06 18:23:17 +000086 while {![file exists test2.db-journal]} {
87 after 10
88 }
danielk1977d5fe8d62008-03-14 08:57:41 +000089
90 # Try to write to test2.db. We are locked out.
drhbb5f18d2007-04-06 18:23:17 +000091 sqlite3 db2 test2.db
92 catchsql {
93 INSERT INTO t2 VALUES(1)
94 } db2
95} {1 {database is locked}}
96do_test lock4-1.3 {
97 db eval {
98 COMMIT;
99 }
drhbb5f18d2007-04-06 18:23:17 +0000100 while {[file exists test2.db-journal]} {
101 after 10
102 }
danielk1977d5fe8d62008-03-14 08:57:41 +0000103 # The other process has committed its transaction on test2.db by
104 # deleting the journal file. But it might retain the lock for a
105 # fraction longer
106 #
drh310bd202009-05-06 00:52:40 +0000107 after 25
drhbb5f18d2007-04-06 18:23:17 +0000108 db2 eval {
109 SELECT * FROM t2
110 }
111} {2}
112
113
114do_test lock4-999.1 {
115 rename db2 {}
116} {}
117
118finish_test