blob: 83d20d634087c593ea28520fa74965a00a4446b2 [file] [log] [blame]
dane264d982010-04-14 18:06:50 +00001# 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.
20proc 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
dan31f98fc2010-04-27 05:42:32 +000027 testfixture $chan "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
dane264d982010-04-14 18:06:50 +000028 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.
dane91a54e2010-06-15 17:44:47 +000033#
dane264d982010-04-14 18:06:50 +000034proc 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" } {
dane91a54e2010-06-15 17:44:47 +000041 set res [lindex $r 1]
42 if { [lindex $r 0] } { error $res }
43 return $res
dane264d982010-04-14 18:06:50 +000044 }
45 if {[eof $chan]} {
46 return "ERROR: Child process hung up"
47 }
48 append r $line
49 }
50}
51
dan5e0ce872010-04-28 17:48:44 +000052proc testfixture_nb_cb {varname chan} {
danf9b76712010-06-01 14:12:45 +000053 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
dan5e0ce872010-04-28 17:48:44 +000060 if { $line == "OVER" } {
dane91a54e2010-06-15 17:44:47 +000061 set $varname [lindex $::tfnb($chan) 1]
dan5e0ce872010-04-28 17:48:44 +000062 unset ::tfnb($chan)
63 close $chan
64 } else {
65 append ::tfnb($chan) $line
66 }
67}
68
69proc 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
dane264d982010-04-14 18:06:50 +000079# 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.
85set f [open tf_main.tcl w]
86puts $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" } {
dan5e0ce872010-04-28 17:48:44 +000094 set rc [catch {eval $script} result]
dane91a54e2010-06-15 17:44:47 +000095 puts [list $rc $result]
96 puts $l "WRITE [list $rc $result]"
dane264d982010-04-14 18:06:50 +000097 puts OVER
98 puts $l "WRITE OVER"
99 flush stdout
100 set script ""
101 } else {
102 append script $line
dan5e0ce872010-04-28 17:48:44 +0000103 append script "\n"
dane264d982010-04-14 18:06:50 +0000104 }
105 }
106 close $l
107}
108close $f