xref: /sqlite-3.40.0/test/lock3.test (revision 37eecdd4)
1# 2001 September 15
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 and the operation of the
13# DEFERRED, IMMEDIATE, and EXCLUSIVE keywords as modifiers to the
14# BEGIN command.
15#
16# $Id: lock3.test,v 1.3 2009/03/24 16:55:44 drh Exp $
17
18
19set testdir [file dirname $argv0]
20source $testdir/tester.tcl
21
22# Establish two connections to the same database.  Put some
23# sample data into the database.
24#
25do_test lock3-1.1 {
26  if {![info exists ::ASYNC]} {
27    file mkdir tempdir/t1/t2/t3
28    sqlite3 db2 ./tempdir/t1//t2/./t3//./../..//./../../tempdir/..//test.db//
29  } else {
30    sqlite3 db2 test.db
31  }
32  execsql {
33    CREATE TABLE t1(a);
34    INSERT INTO t1 VALUES(1);
35  }
36  execsql {
37    SELECT * FROM t1
38  } db2
39} 1
40
41# Get a deferred lock on the database using one connection.  The
42# other connection should still be able to write.
43#
44do_test lock3-2.1 {
45  execsql {BEGIN DEFERRED TRANSACTION}
46  execsql {INSERT INTO t1 VALUES(2)} db2
47  execsql {END TRANSACTION}
48  execsql {SELECT * FROM t1}
49} {1 2}
50
51# Get an immediate lock on the database using one connection.  The
52# other connection should be able to read the database but not write
53# it.
54#
55do_test lock3-3.1 {
56  execsql {BEGIN IMMEDIATE TRANSACTION}
57  catchsql {SELECT * FROM t1} db2
58} {0 {1 2}}
59do_test lock3-3.2 {
60  catchsql {INSERT INTO t1 VALUES(3)} db2
61} {1 {database is locked}}
62do_test lock3-3.3 {
63  execsql {END TRANSACTION}
64} {}
65
66
67# Get an exclusive lock on the database using one connection.  The
68# other connection should be unable to read or write the database.
69#
70do_test lock3-4.1 {
71  execsql {BEGIN EXCLUSIVE TRANSACTION}
72  catchsql {SELECT * FROM t1} db2
73} {1 {database is locked}}
74do_test lock3-4.2 {
75  catchsql {INSERT INTO t1 VALUES(3)} db2
76} {1 {database is locked}}
77do_test lock3-4.3 {
78  execsql {END TRANSACTION}
79} {}
80
81catch {db2 close}
82
83finish_test
84