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 495e0ce87aSdanproc testfixture_nb_cb {varname chan} { 50*f9b76710Sdan if {[eof $chan]} { 51*f9b76710Sdan append ::tfnb($chan) "ERROR: Child process hung up" 52*f9b76710Sdan set line "OVER" 53*f9b76710Sdan } else { 545e0ce87aSdan set line [gets $chan] 55*f9b76710Sdan } 56*f9b76710Sdan 575e0ce87aSdan if { $line == "OVER" } { 585e0ce87aSdan set $varname $::tfnb($chan) 595e0ce87aSdan unset ::tfnb($chan) 605e0ce87aSdan close $chan 615e0ce87aSdan } else { 625e0ce87aSdan append ::tfnb($chan) $line 635e0ce87aSdan } 645e0ce87aSdan} 655e0ce87aSdan 665e0ce87aSdanproc testfixture_nb {varname cmd} { 675e0ce87aSdan set chan [launch_testfixture] 685e0ce87aSdan set ::tfnb($chan) "" 695e0ce87aSdan fconfigure $chan -blocking 0 -buffering none 705e0ce87aSdan puts $chan $cmd 715e0ce87aSdan puts $chan OVER 725e0ce87aSdan fileevent $chan readable [list testfixture_nb_cb $varname $chan] 735e0ce87aSdan return "" 745e0ce87aSdan} 755e0ce87aSdan 76e264d983Sdan# Write the main loop for the child testfixture processes into file 77e264d983Sdan# tf_main.tcl. The parent (this script) interacts with the child processes 78e264d983Sdan# via a two way pipe. The parent writes a script to the stdin of the child 79e264d983Sdan# process, followed by the word "OVER" on a line of its own. The child 80e264d983Sdan# process evaluates the script and writes the results to stdout, followed 81e264d983Sdan# by an "OVER" of its own. 82e264d983Sdanset f [open tf_main.tcl w] 83e264d983Sdanputs $f { 84e264d983Sdan set l [open log w] 85e264d983Sdan set script "" 86e264d983Sdan while {![eof stdin]} { 87e264d983Sdan flush stdout 88e264d983Sdan set line [gets stdin] 89e264d983Sdan puts $l "READ $line" 90e264d983Sdan if { $line == "OVER" } { 915e0ce87aSdan set rc [catch {eval $script} result] 92e264d983Sdan puts $result 93e264d983Sdan puts $l "WRITE $result" 94e264d983Sdan puts OVER 95e264d983Sdan puts $l "WRITE OVER" 96e264d983Sdan flush stdout 97e264d983Sdan set script "" 98e264d983Sdan } else { 99e264d983Sdan append script $line 1005e0ce87aSdan append script "\n" 101e264d983Sdan } 102e264d983Sdan } 103e264d983Sdan close $l 104e264d983Sdan} 105e264d983Sdanclose $f 106