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