xref: /sqlite-3.40.0/test/lock2.test (revision a67367e7)
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 between competing processes.
13#
14# $Id: lock2.test,v 1.5 2005/09/17 16:36:57 drh Exp $
15
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Launch another testfixture process to be controlled by this one. A
21# channel name is returned that may be passed as the first argument to proc
22# 'testfixture' to execute a command. The child testfixture process is shut
23# down by closing the channel.
24proc launch_testfixture {} {
25  set chan [open "|[file join . testfixture] tf_main.tcl" r+]
26  fconfigure $chan -buffering line
27  return $chan
28}
29
30# Execute a command in a child testfixture process, connected by two-way
31# channel $chan. Return the result of the command, or an error message.
32proc testfixture {chan cmd} {
33  puts $chan $cmd
34  puts $chan OVER
35  set r ""
36  while { 1 } {
37    set line [gets $chan]
38    if { $line == "OVER" } {
39      return $r
40    }
41    append r $line
42  }
43}
44
45# Write the main loop for the child testfixture processes into file
46# tf_main.tcl. The parent (this script) interacts with the child processes
47# via a two way pipe. The parent writes a script to the stdin of the child
48# process, followed by the word "OVER" on a line of it's own. The child
49# process evaluates the script and writes the results to stdout, followed
50# by an "OVER" of its own.
51set f [open tf_main.tcl w]
52puts $f {
53  set l [open log w]
54  set script ""
55  while {![eof stdin]} {
56    flush stdout
57    set line [gets stdin]
58    puts $l "READ $line"
59    if { $line == "OVER" } {
60      catch {eval $script} result
61      puts $result
62      puts $l "WRITE $result"
63      puts OVER
64      puts $l "WRITE OVER"
65      flush stdout
66      set script ""
67    } else {
68      append script $line
69      append script " ; "
70    }
71  }
72  close $l
73}
74close $f
75
76# Simple locking test case:
77#
78# lock2-1.1: Connect a second process to the database.
79# lock2-1.2: Establish a RESERVED lock with this process.
80# lock2-1.3: Get a SHARED lock with the second process.
81# lock2-1.4: Try for a RESERVED lock with process 2. This fails.
82# lock2-1.5: Try to upgrade the first process to EXCLUSIVE, this fails so
83#            it gets PENDING.
84# lock2-1.6: Release the SHARED lock held by the second process.
85# lock2-1.7: Attempt to reaquire a SHARED lock with the second process.
86#            this fails due to the PENDING lock.
87# lock2-1.8: Ensure the first process can now upgrade to EXCLUSIVE.
88#
89do_test lock2-1.1 {
90  set ::tf1 [launch_testfixture]
91  testfixture $::tf1 {
92    sqlite3 db test.db -key xyzzy
93    db eval {select * from sqlite_master}
94  }
95} {}
96do_test lock2-1.1.1 {
97  execsql {pragma lock_status}
98} {main unlocked temp closed}
99do_test lock2-1.2 {
100  execsql {
101    BEGIN;
102    CREATE TABLE abc(a, b, c);
103  }
104} {}
105do_test lock2-1.3 {
106  testfixture $::tf1 {
107    db eval {
108      BEGIN;
109      SELECT * FROM sqlite_master;
110    }
111  }
112} {}
113do_test lock2-1.4 {
114  testfixture $::tf1 {
115    db eval {
116      CREATE TABLE def(d, e, f)
117    }
118  }
119} {database is locked}
120do_test lock2-1.5 {
121  catchsql {
122    COMMIT;
123  }
124} {1 {database is locked}}
125do_test lock2-1.6 {
126  testfixture $::tf1 {
127    db eval {
128      SELECT * FROM sqlite_master;
129      COMMIT;
130    }
131  }
132} {}
133do_test lock2-1.7 {
134  testfixture $::tf1 {
135    db eval {
136      BEGIN;
137      SELECT * FROM sqlite_master;
138    }
139  }
140} {database is locked}
141do_test lock2-1.8 {
142  catchsql {
143    COMMIT;
144  }
145} {0 {}}
146do_test lock2-1.9 {
147  execsql {
148    SELECT * FROM sqlite_master;
149  }
150} "table abc abc [expr $AUTOVACUUM?3:2] {CREATE TABLE abc(a, b, c)}"
151do_test lock2-1.10 {
152  testfixture $::tf1 {
153    db eval {
154      SELECT * FROM sqlite_master;
155    }
156  }
157} "table abc abc [expr $AUTOVACUUM?3:2] {CREATE TABLE abc(a, b, c)}"
158
159catch {testfixture $::tf1 {db close}}
160catch {close $::tf1}
161
162finish_test
163