xref: /sqlite-3.40.0/test/lock3.test (revision 94dfe476)
1684917c2Sdrh# 2001 September 15
2684917c2Sdrh#
3684917c2Sdrh# The author disclaims copyright to this source code.  In place of
4684917c2Sdrh# a legal notice, here is a blessing:
5684917c2Sdrh#
6684917c2Sdrh#    May you do good and not evil.
7684917c2Sdrh#    May you find forgiveness for yourself and forgive others.
8684917c2Sdrh#    May you share freely, never taking more than you give.
9684917c2Sdrh#
10684917c2Sdrh#***********************************************************************
11684917c2Sdrh# This file implements regression tests for SQLite library.  The
12684917c2Sdrh# focus of this script is database locks and the operation of the
13684917c2Sdrh# DEFERRED, IMMEDIATE, and EXCLUSIVE keywords as modifiers to the
14684917c2Sdrh# BEGIN command.
15684917c2Sdrh#
16*94dfe476Sdrh# $Id: lock3.test,v 1.4 2009/03/28 15:04:24 drh Exp $
17684917c2Sdrh
18684917c2Sdrh
19684917c2Sdrhset testdir [file dirname $argv0]
20684917c2Sdrhsource $testdir/tester.tcl
21684917c2Sdrh
22684917c2Sdrh# Establish two connections to the same database.  Put some
23684917c2Sdrh# sample data into the database.
24684917c2Sdrh#
25684917c2Sdrhdo_test lock3-1.1 {
26107886abSdrh  file mkdir tempdir/t1/t2/t3
27107886abSdrh  sqlite3 db2 ./tempdir/t1//t2/./t3//./../..//./../../tempdir/..//test.db//
28684917c2Sdrh  execsql {
29684917c2Sdrh    CREATE TABLE t1(a);
30684917c2Sdrh    INSERT INTO t1 VALUES(1);
31684917c2Sdrh  }
32684917c2Sdrh  execsql {
33684917c2Sdrh    SELECT * FROM t1
34684917c2Sdrh  } db2
35684917c2Sdrh} 1
36684917c2Sdrh
37684917c2Sdrh# Get a deferred lock on the database using one connection.  The
38684917c2Sdrh# other connection should still be able to write.
39684917c2Sdrh#
40684917c2Sdrhdo_test lock3-2.1 {
41684917c2Sdrh  execsql {BEGIN DEFERRED TRANSACTION}
42684917c2Sdrh  execsql {INSERT INTO t1 VALUES(2)} db2
43684917c2Sdrh  execsql {END TRANSACTION}
44684917c2Sdrh  execsql {SELECT * FROM t1}
45684917c2Sdrh} {1 2}
46684917c2Sdrh
47684917c2Sdrh# Get an immediate lock on the database using one connection.  The
48684917c2Sdrh# other connection should be able to read the database but not write
49684917c2Sdrh# it.
50684917c2Sdrh#
51684917c2Sdrhdo_test lock3-3.1 {
52684917c2Sdrh  execsql {BEGIN IMMEDIATE TRANSACTION}
53684917c2Sdrh  catchsql {SELECT * FROM t1} db2
54684917c2Sdrh} {0 {1 2}}
55684917c2Sdrhdo_test lock3-3.2 {
56684917c2Sdrh  catchsql {INSERT INTO t1 VALUES(3)} db2
57684917c2Sdrh} {1 {database is locked}}
58684917c2Sdrhdo_test lock3-3.3 {
59684917c2Sdrh  execsql {END TRANSACTION}
60684917c2Sdrh} {}
61684917c2Sdrh
62684917c2Sdrh
63684917c2Sdrh# Get an exclusive lock on the database using one connection.  The
64684917c2Sdrh# other connection should be unable to read or write the database.
65684917c2Sdrh#
66684917c2Sdrhdo_test lock3-4.1 {
67684917c2Sdrh  execsql {BEGIN EXCLUSIVE TRANSACTION}
68684917c2Sdrh  catchsql {SELECT * FROM t1} db2
69684917c2Sdrh} {1 {database is locked}}
70684917c2Sdrhdo_test lock3-4.2 {
71684917c2Sdrh  catchsql {INSERT INTO t1 VALUES(3)} db2
72684917c2Sdrh} {1 {database is locked}}
73684917c2Sdrhdo_test lock3-4.3 {
74684917c2Sdrh  execsql {END TRANSACTION}
75684917c2Sdrh} {}
76684917c2Sdrh
77684917c2Sdrhcatch {db2 close}
78684917c2Sdrh
79684917c2Sdrhfinish_test
80