2010-04-14 22:06:50 +04:00
|
|
|
# 2010 April 14
|
|
|
|
#
|
|
|
|
# The author disclaims copyright to this source code. In place of
|
|
|
|
# a legal notice, here is a blessing:
|
|
|
|
#
|
|
|
|
# May you do good and not evil.
|
|
|
|
# May you find forgiveness for yourself and forgive others.
|
|
|
|
# May you share freely, never taking more than you give.
|
|
|
|
#
|
|
|
|
#***********************************************************************
|
|
|
|
# This file contains code used by several different test scripts. The
|
|
|
|
# code in this file allows testfixture to control another process (or
|
|
|
|
# processes) to test locking.
|
|
|
|
#
|
|
|
|
|
2010-06-15 23:07:42 +04:00
|
|
|
proc do_multiclient_test {varname script} {
|
|
|
|
|
|
|
|
foreach code [list {
|
2010-12-03 13:32:06 +03:00
|
|
|
if {[info exists ::G(valgrind)]} { db close ; continue }
|
2010-06-15 23:07:42 +04:00
|
|
|
set ::code2_chan [launch_testfixture]
|
|
|
|
set ::code3_chan [launch_testfixture]
|
|
|
|
proc code2 {tcl} { testfixture $::code2_chan $tcl }
|
|
|
|
proc code3 {tcl} { testfixture $::code3_chan $tcl }
|
|
|
|
set tn 1
|
|
|
|
} {
|
|
|
|
proc code2 {tcl} { uplevel #0 $tcl }
|
|
|
|
proc code3 {tcl} { uplevel #0 $tcl }
|
|
|
|
set tn 2
|
|
|
|
}] {
|
|
|
|
faultsim_delete_and_reopen
|
2010-08-12 15:25:47 +04:00
|
|
|
|
|
|
|
proc code1 {tcl} { uplevel #0 $tcl }
|
2010-06-15 23:07:42 +04:00
|
|
|
|
|
|
|
# Open connections [db2] and [db3]. Depending on which iteration this
|
|
|
|
# is, the connections may be created in this interpreter, or in
|
|
|
|
# interpreters running in other OS processes. As such, the [db2] and [db3]
|
|
|
|
# commands should only be accessed within [code2] and [code3] blocks,
|
|
|
|
# respectively.
|
|
|
|
#
|
|
|
|
eval $code
|
|
|
|
code2 { sqlite3 db2 test.db }
|
|
|
|
code3 { sqlite3 db3 test.db }
|
|
|
|
|
|
|
|
# Shorthand commands. Execute SQL using database connection [db2] or
|
|
|
|
# [db3]. Return the results.
|
|
|
|
#
|
|
|
|
proc sql1 {sql} { db eval $sql }
|
|
|
|
proc sql2 {sql} { code2 [list db2 eval $sql] }
|
|
|
|
proc sql3 {sql} { code3 [list db3 eval $sql] }
|
|
|
|
|
|
|
|
proc csql1 {sql} { list [catch { sql1 $sql } msg] $msg }
|
|
|
|
proc csql2 {sql} { list [catch { sql2 $sql } msg] $msg }
|
|
|
|
proc csql3 {sql} { list [catch { sql3 $sql } msg] $msg }
|
|
|
|
|
|
|
|
uplevel set $varname $tn
|
|
|
|
uplevel $script
|
|
|
|
|
2011-05-10 21:31:29 +04:00
|
|
|
catch { code2 { db2 close } }
|
|
|
|
catch { code3 { db3 close } }
|
2010-06-15 23:07:42 +04:00
|
|
|
catch { close $::code2_chan }
|
|
|
|
catch { close $::code3_chan }
|
2010-09-15 15:42:04 +04:00
|
|
|
catch { db close }
|
2010-06-15 23:07:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-14 22:06:50 +04:00
|
|
|
# Launch another testfixture process to be controlled by this one. A
|
|
|
|
# channel name is returned that may be passed as the first argument to proc
|
|
|
|
# 'testfixture' to execute a command. The child testfixture process is shut
|
|
|
|
# down by closing the channel.
|
2010-08-19 15:05:53 +04:00
|
|
|
proc launch_testfixture {{prg ""}} {
|
2010-06-22 19:18:44 +04:00
|
|
|
write_main_loop
|
2010-08-20 16:43:01 +04:00
|
|
|
if {$prg eq ""} { set prg [info nameofexec] }
|
|
|
|
if {$prg eq ""} { set prg testfixture }
|
2010-08-20 16:31:30 +04:00
|
|
|
if {[file tail $prg]==$prg} { set prg [file join . $prg] }
|
2010-04-14 22:06:50 +04:00
|
|
|
set chan [open "|$prg tf_main.tcl" r+]
|
|
|
|
fconfigure $chan -buffering line
|
2010-08-19 15:05:53 +04:00
|
|
|
set rc [catch {
|
|
|
|
testfixture $chan "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
|
|
|
|
}]
|
|
|
|
if {$rc} {
|
|
|
|
testfixture $chan "set ::sqlite_pending_byte $::sqlite_pending_byte"
|
|
|
|
}
|
2010-04-14 22:06:50 +04:00
|
|
|
return $chan
|
|
|
|
}
|
|
|
|
|
|
|
|
# Execute a command in a child testfixture process, connected by two-way
|
|
|
|
# channel $chan. Return the result of the command, or an error message.
|
2010-06-15 21:44:47 +04:00
|
|
|
#
|
2010-04-14 22:06:50 +04:00
|
|
|
proc testfixture {chan cmd} {
|
|
|
|
puts $chan $cmd
|
|
|
|
puts $chan OVER
|
|
|
|
set r ""
|
|
|
|
while { 1 } {
|
|
|
|
set line [gets $chan]
|
|
|
|
if { $line == "OVER" } {
|
2010-06-15 21:44:47 +04:00
|
|
|
set res [lindex $r 1]
|
|
|
|
if { [lindex $r 0] } { error $res }
|
|
|
|
return $res
|
2010-04-14 22:06:50 +04:00
|
|
|
}
|
|
|
|
if {[eof $chan]} {
|
|
|
|
return "ERROR: Child process hung up"
|
|
|
|
}
|
|
|
|
append r $line
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-28 21:48:44 +04:00
|
|
|
proc testfixture_nb_cb {varname chan} {
|
2010-06-01 18:12:45 +04:00
|
|
|
if {[eof $chan]} {
|
|
|
|
append ::tfnb($chan) "ERROR: Child process hung up"
|
|
|
|
set line "OVER"
|
|
|
|
} else {
|
|
|
|
set line [gets $chan]
|
|
|
|
}
|
|
|
|
|
2010-04-28 21:48:44 +04:00
|
|
|
if { $line == "OVER" } {
|
2010-06-15 21:44:47 +04:00
|
|
|
set $varname [lindex $::tfnb($chan) 1]
|
2010-04-28 21:48:44 +04:00
|
|
|
unset ::tfnb($chan)
|
|
|
|
close $chan
|
|
|
|
} else {
|
|
|
|
append ::tfnb($chan) $line
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc testfixture_nb {varname cmd} {
|
|
|
|
set chan [launch_testfixture]
|
|
|
|
set ::tfnb($chan) ""
|
|
|
|
fconfigure $chan -blocking 0 -buffering none
|
|
|
|
puts $chan $cmd
|
|
|
|
puts $chan OVER
|
|
|
|
fileevent $chan readable [list testfixture_nb_cb $varname $chan]
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2010-04-14 22:06:50 +04:00
|
|
|
# Write the main loop for the child testfixture processes into file
|
|
|
|
# tf_main.tcl. The parent (this script) interacts with the child processes
|
|
|
|
# via a two way pipe. The parent writes a script to the stdin of the child
|
|
|
|
# process, followed by the word "OVER" on a line of its own. The child
|
|
|
|
# process evaluates the script and writes the results to stdout, followed
|
|
|
|
# by an "OVER" of its own.
|
2010-06-22 19:18:44 +04:00
|
|
|
#
|
|
|
|
set main_loop_written 0
|
|
|
|
proc write_main_loop {} {
|
|
|
|
if {$::main_loop_written} return
|
|
|
|
set wrapper ""
|
|
|
|
if {[sqlite3 -has-codec] && [info exists ::do_not_use_codec]==0} {
|
|
|
|
set wrapper "
|
|
|
|
rename sqlite3 sqlite_orig
|
|
|
|
proc sqlite3 {args} {[info body sqlite3]}
|
|
|
|
"
|
|
|
|
}
|
|
|
|
|
|
|
|
set fd [open tf_main.tcl w]
|
|
|
|
puts $fd [string map [list %WRAPPER% $wrapper] {
|
|
|
|
%WRAPPER%
|
|
|
|
set script ""
|
|
|
|
while {![eof stdin]} {
|
2010-04-14 22:06:50 +04:00
|
|
|
flush stdout
|
2010-06-22 19:18:44 +04:00
|
|
|
set line [gets stdin]
|
|
|
|
if { $line == "OVER" } {
|
|
|
|
set rc [catch {eval $script} result]
|
|
|
|
puts [list $rc $result]
|
|
|
|
puts OVER
|
|
|
|
flush stdout
|
|
|
|
set script ""
|
|
|
|
} else {
|
|
|
|
append script $line
|
|
|
|
append script "\n"
|
|
|
|
}
|
2010-04-14 22:06:50 +04:00
|
|
|
}
|
2010-06-22 19:18:44 +04:00
|
|
|
}]
|
|
|
|
close $fd
|
|
|
|
set main_loop_written 1
|
2010-04-14 22:06:50 +04:00
|
|
|
}
|
2010-06-22 19:18:44 +04:00
|
|
|
|