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.6 2005/09/17 16:48:19 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 "set sqlite_pending_byte $::sqlite_pending_byte" 92 testfixture $::tf1 { 93 sqlite3 db test.db -key xyzzy 94 db eval {select * from sqlite_master} 95 } 96} {} 97do_test lock2-1.1.1 { 98 execsql {pragma lock_status} 99} {main unlocked temp closed} 100do_test lock2-1.2 { 101 execsql { 102 BEGIN; 103 CREATE TABLE abc(a, b, c); 104 } 105} {} 106do_test lock2-1.3 { 107 testfixture $::tf1 { 108 db eval { 109 BEGIN; 110 SELECT * FROM sqlite_master; 111 } 112 } 113} {} 114do_test lock2-1.4 { 115 testfixture $::tf1 { 116 db eval { 117 CREATE TABLE def(d, e, f) 118 } 119 } 120} {database is locked} 121do_test lock2-1.5 { 122 catchsql { 123 COMMIT; 124 } 125} {1 {database is locked}} 126do_test lock2-1.6 { 127 testfixture $::tf1 { 128 db eval { 129 SELECT * FROM sqlite_master; 130 COMMIT; 131 } 132 } 133} {} 134do_test lock2-1.7 { 135 testfixture $::tf1 { 136 db eval { 137 BEGIN; 138 SELECT * FROM sqlite_master; 139 } 140 } 141} {database is locked} 142do_test lock2-1.8 { 143 catchsql { 144 COMMIT; 145 } 146} {0 {}} 147do_test lock2-1.9 { 148 execsql { 149 SELECT * FROM sqlite_master; 150 } 151} "table abc abc [expr $AUTOVACUUM?3:2] {CREATE TABLE abc(a, b, c)}" 152do_test lock2-1.10 { 153 testfixture $::tf1 { 154 db eval { 155 SELECT * FROM sqlite_master; 156 } 157 } 158} "table abc abc [expr $AUTOVACUUM?3:2] {CREATE TABLE abc(a, b, c)}" 159 160catch {testfixture $::tf1 {db close}} 161catch {close $::tf1} 162 163finish_test 164