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