xref: /sqlite-3.40.0/test/lock4.test (revision 4ffb7b9a)
1# 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#
14# $Id: lock4.test,v 1.7 2008/03/13 04:53:53 danielk1977 Exp $
15
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Initialize the test.db database so that it is non-empty
21#
22do_test lock4-1.1 {
23  db eval {
24     PRAGMA auto_vacuum=OFF;
25     CREATE TABLE t1(x);
26  }
27  file delete -force test2.db test2.db-journal
28  sqlite3 db2 test2.db
29  db2 eval {
30     PRAGMA auto_vacuum=OFF;
31     CREATE TABLE t2(x)
32  }
33  db2 close
34  list [file size test.db] [file size test2.db]
35} {2048 2048}
36
37# Create a script to drive a separate process that will
38#
39#     1.  Create a second database test2.db
40#     2.  Get an exclusive lock on test2.db
41#     3.  Add an entry to test.db in table t1, waiting as necessary.
42#     4.  Commit the change to test2.db.
43#
44# Meanwhile, this process will:
45#
46#     A.  Get an exclusive lock on test.db
47#     B.  Attempt to read from test2.db but get an SQLITE_BUSY error.
48#     C.  Commit the changes to test.db thus alloing the other process
49#         to continue.
50#
51do_test lock4-1.2 {
52  set out [open test2-script.tcl w]
53  puts $out "set sqlite_pending_byte [set sqlite_pending_byte]"
54  puts $out {
55     sqlite3 db2 test2.db
56     db2 eval {
57        BEGIN;
58        INSERT INTO t2 VALUES(2);
59     }
60     sqlite3 db test.db
61     db timeout 1000000
62     db eval {
63        INSERT INTO t1 VALUES(2);
64     }
65     db close
66     db2 eval COMMIT
67     exit
68  }
69  close $out
70  db eval {
71     BEGIN;
72     INSERT INTO t1 VALUES(1);
73  }
74  exec [info nameofexec] ./test2-script.tcl &
75  while {![file exists test2.db-journal]} {
76    after 10
77  }
78  sqlite3 db2 test2.db
79  catchsql {
80    INSERT INTO t2 VALUES(1)
81  } db2
82} {1 {database is locked}}
83do_test lock4-1.3 {
84  db eval {
85     COMMIT;
86  }
87  while {[file exists test2.db-journal]} {
88    after 10
89  }
90  db2 eval {
91     SELECT * FROM t2
92  }
93} {2}
94
95
96do_test lock4-999.1 {
97  rename db2 {}
98} {}
99
100finish_test
101