1e264d983Sdan# 2010 April 14 2e264d983Sdan# 3e264d983Sdan# The author disclaims copyright to this source code. In place of 4e264d983Sdan# a legal notice, here is a blessing: 5e264d983Sdan# 6e264d983Sdan# May you do good and not evil. 7e264d983Sdan# May you find forgiveness for yourself and forgive others. 8e264d983Sdan# May you share freely, never taking more than you give. 9e264d983Sdan# 10e264d983Sdan#*********************************************************************** 11e264d983Sdan# This file contains code used by several different test scripts. The 12e264d983Sdan# code in this file allows testfixture to control another process (or 13e264d983Sdan# processes) to test locking. 14e264d983Sdan# 15e264d983Sdan 16e264d983Sdan# Launch another testfixture process to be controlled by this one. A 17e264d983Sdan# channel name is returned that may be passed as the first argument to proc 18e264d983Sdan# 'testfixture' to execute a command. The child testfixture process is shut 19e264d983Sdan# down by closing the channel. 20e264d983Sdanproc launch_testfixture {} { 21e264d983Sdan set prg [info nameofexec] 22e264d983Sdan if {$prg eq ""} { 23e264d983Sdan set prg [file join . testfixture] 24e264d983Sdan } 25e264d983Sdan set chan [open "|$prg tf_main.tcl" r+] 26e264d983Sdan fconfigure $chan -buffering line 2731f98fc8Sdan testfixture $chan "sqlite3_test_control_pending_byte $::sqlite_pending_byte" 28e264d983Sdan return $chan 29e264d983Sdan} 30e264d983Sdan 31e264d983Sdan# Execute a command in a child testfixture process, connected by two-way 32e264d983Sdan# channel $chan. Return the result of the command, or an error message. 33e264d983Sdanproc testfixture {chan cmd} { 34e264d983Sdan puts $chan $cmd 35e264d983Sdan puts $chan OVER 36e264d983Sdan set r "" 37e264d983Sdan while { 1 } { 38e264d983Sdan set line [gets $chan] 39e264d983Sdan if { $line == "OVER" } { 40e264d983Sdan return $r 41e264d983Sdan } 42e264d983Sdan if {[eof $chan]} { 43e264d983Sdan return "ERROR: Child process hung up" 44e264d983Sdan } 45e264d983Sdan append r $line 46e264d983Sdan } 47e264d983Sdan} 48e264d983Sdan 49*5e0ce87aSdanproc testfixture_nb_cb {varname chan} { 50*5e0ce87aSdan set line [gets $chan] 51*5e0ce87aSdan if { $line == "OVER" } { 52*5e0ce87aSdan set $varname $::tfnb($chan) 53*5e0ce87aSdan unset ::tfnb($chan) 54*5e0ce87aSdan close $chan 55*5e0ce87aSdan } else { 56*5e0ce87aSdan append ::tfnb($chan) $line 57*5e0ce87aSdan } 58*5e0ce87aSdan} 59*5e0ce87aSdan 60*5e0ce87aSdanproc testfixture_nb {varname cmd} { 61*5e0ce87aSdan set chan [launch_testfixture] 62*5e0ce87aSdan set ::tfnb($chan) "" 63*5e0ce87aSdan fconfigure $chan -blocking 0 -buffering none 64*5e0ce87aSdan puts $chan $cmd 65*5e0ce87aSdan puts $chan OVER 66*5e0ce87aSdan fileevent $chan readable [list testfixture_nb_cb $varname $chan] 67*5e0ce87aSdan return "" 68*5e0ce87aSdan} 69*5e0ce87aSdan 70e264d983Sdan# Write the main loop for the child testfixture processes into file 71e264d983Sdan# tf_main.tcl. The parent (this script) interacts with the child processes 72e264d983Sdan# via a two way pipe. The parent writes a script to the stdin of the child 73e264d983Sdan# process, followed by the word "OVER" on a line of its own. The child 74e264d983Sdan# process evaluates the script and writes the results to stdout, followed 75e264d983Sdan# by an "OVER" of its own. 76e264d983Sdanset f [open tf_main.tcl w] 77e264d983Sdanputs $f { 78e264d983Sdan set l [open log w] 79e264d983Sdan set script "" 80e264d983Sdan while {![eof stdin]} { 81e264d983Sdan flush stdout 82e264d983Sdan set line [gets stdin] 83e264d983Sdan puts $l "READ $line" 84e264d983Sdan if { $line == "OVER" } { 85*5e0ce87aSdan set rc [catch {eval $script} result] 86e264d983Sdan puts $result 87e264d983Sdan puts $l "WRITE $result" 88e264d983Sdan puts OVER 89e264d983Sdan puts $l "WRITE OVER" 90e264d983Sdan flush stdout 91e264d983Sdan set script "" 92e264d983Sdan } else { 93e264d983Sdan append script $line 94*5e0ce87aSdan append script "\n" 95e264d983Sdan } 96e264d983Sdan } 97e264d983Sdan close $l 98e264d983Sdan} 99e264d983Sdanclose $f 100