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