dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame^] | 1 | # 2010 April 14 |
| 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 contains code used by several different test scripts. The |
| 12 | # code in this file allows testfixture to control another process (or |
| 13 | # processes) to test locking. |
| 14 | # |
| 15 | |
| 16 | # Launch another testfixture process to be controlled by this one. A |
| 17 | # channel name is returned that may be passed as the first argument to proc |
| 18 | # 'testfixture' to execute a command. The child testfixture process is shut |
| 19 | # down by closing the channel. |
| 20 | proc launch_testfixture {} { |
| 21 | set prg [info nameofexec] |
| 22 | if {$prg eq ""} { |
| 23 | set prg [file join . testfixture] |
| 24 | } |
| 25 | set chan [open "|$prg 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. |
| 32 | proc 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 | if {[eof $chan]} { |
| 42 | return "ERROR: Child process hung up" |
| 43 | } |
| 44 | append r $line |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | # Write the main loop for the child testfixture processes into file |
| 49 | # tf_main.tcl. The parent (this script) interacts with the child processes |
| 50 | # via a two way pipe. The parent writes a script to the stdin of the child |
| 51 | # process, followed by the word "OVER" on a line of its own. The child |
| 52 | # process evaluates the script and writes the results to stdout, followed |
| 53 | # by an "OVER" of its own. |
| 54 | set f [open tf_main.tcl w] |
| 55 | puts $f { |
| 56 | set l [open log w] |
| 57 | set script "" |
| 58 | while {![eof stdin]} { |
| 59 | flush stdout |
| 60 | set line [gets stdin] |
| 61 | puts $l "READ $line" |
| 62 | if { $line == "OVER" } { |
| 63 | catch {eval $script} result |
| 64 | puts $result |
| 65 | puts $l "WRITE $result" |
| 66 | puts OVER |
| 67 | puts $l "WRITE OVER" |
| 68 | flush stdout |
| 69 | set script "" |
| 70 | } else { |
| 71 | append script $line |
| 72 | append script " ; " |
| 73 | } |
| 74 | } |
| 75 | close $l |
| 76 | } |
| 77 | close $f |