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 |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 27 | testfixture $chan "sqlite3_test_control_pending_byte $::sqlite_pending_byte" |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 28 | return $chan |
| 29 | } |
| 30 | |
| 31 | # Execute a command in a child testfixture process, connected by two-way |
| 32 | # channel $chan. Return the result of the command, or an error message. |
| 33 | proc testfixture {chan cmd} { |
| 34 | puts $chan $cmd |
| 35 | puts $chan OVER |
| 36 | set r "" |
| 37 | while { 1 } { |
| 38 | set line [gets $chan] |
| 39 | if { $line == "OVER" } { |
| 40 | return $r |
| 41 | } |
| 42 | if {[eof $chan]} { |
| 43 | return "ERROR: Child process hung up" |
| 44 | } |
| 45 | append r $line |
| 46 | } |
| 47 | } |
| 48 | |
dan | 5e0ce87 | 2010-04-28 17:48:44 +0000 | [diff] [blame] | 49 | proc testfixture_nb_cb {varname chan} { |
dan | f9b7671 | 2010-06-01 14:12:45 +0000 | [diff] [blame^] | 50 | if {[eof $chan]} { |
| 51 | append ::tfnb($chan) "ERROR: Child process hung up" |
| 52 | set line "OVER" |
| 53 | } else { |
| 54 | set line [gets $chan] |
| 55 | } |
| 56 | |
dan | 5e0ce87 | 2010-04-28 17:48:44 +0000 | [diff] [blame] | 57 | if { $line == "OVER" } { |
| 58 | set $varname $::tfnb($chan) |
| 59 | unset ::tfnb($chan) |
| 60 | close $chan |
| 61 | } else { |
| 62 | append ::tfnb($chan) $line |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | proc testfixture_nb {varname cmd} { |
| 67 | set chan [launch_testfixture] |
| 68 | set ::tfnb($chan) "" |
| 69 | fconfigure $chan -blocking 0 -buffering none |
| 70 | puts $chan $cmd |
| 71 | puts $chan OVER |
| 72 | fileevent $chan readable [list testfixture_nb_cb $varname $chan] |
| 73 | return "" |
| 74 | } |
| 75 | |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 76 | # Write the main loop for the child testfixture processes into file |
| 77 | # tf_main.tcl. The parent (this script) interacts with the child processes |
| 78 | # via a two way pipe. The parent writes a script to the stdin of the child |
| 79 | # process, followed by the word "OVER" on a line of its own. The child |
| 80 | # process evaluates the script and writes the results to stdout, followed |
| 81 | # by an "OVER" of its own. |
| 82 | set f [open tf_main.tcl w] |
| 83 | puts $f { |
| 84 | set l [open log w] |
| 85 | set script "" |
| 86 | while {![eof stdin]} { |
| 87 | flush stdout |
| 88 | set line [gets stdin] |
| 89 | puts $l "READ $line" |
| 90 | if { $line == "OVER" } { |
dan | 5e0ce87 | 2010-04-28 17:48:44 +0000 | [diff] [blame] | 91 | set rc [catch {eval $script} result] |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 92 | puts $result |
| 93 | puts $l "WRITE $result" |
| 94 | puts OVER |
| 95 | puts $l "WRITE OVER" |
| 96 | flush stdout |
| 97 | set script "" |
| 98 | } else { |
| 99 | append script $line |
dan | 5e0ce87 | 2010-04-28 17:48:44 +0000 | [diff] [blame] | 100 | append script "\n" |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | close $l |
| 104 | } |
| 105 | close $f |