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