blob: b2e4184cc4542d6849f2f457303ed0959fcd5b82 [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.
33proc 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
dan5e0ce872010-04-28 17:48:44 +000049proc testfixture_nb_cb {varname chan} {
50 set line [gets $chan]
51 if { $line == "OVER" } {
52 set $varname $::tfnb($chan)
53 unset ::tfnb($chan)
54 close $chan
55 } else {
56 append ::tfnb($chan) $line
57 }
58}
59
60proc testfixture_nb {varname cmd} {
61 set chan [launch_testfixture]
62 set ::tfnb($chan) ""
63 fconfigure $chan -blocking 0 -buffering none
64 puts $chan $cmd
65 puts $chan OVER
66 fileevent $chan readable [list testfixture_nb_cb $varname $chan]
67 return ""
68}
69
dane264d982010-04-14 18:06:50 +000070# Write the main loop for the child testfixture processes into file
71# tf_main.tcl. The parent (this script) interacts with the child processes
72# via a two way pipe. The parent writes a script to the stdin of the child
73# process, followed by the word "OVER" on a line of its own. The child
74# process evaluates the script and writes the results to stdout, followed
75# by an "OVER" of its own.
76set f [open tf_main.tcl w]
77puts $f {
78 set l [open log w]
79 set script ""
80 while {![eof stdin]} {
81 flush stdout
82 set line [gets stdin]
83 puts $l "READ $line"
84 if { $line == "OVER" } {
dan5e0ce872010-04-28 17:48:44 +000085 set rc [catch {eval $script} result]
dane264d982010-04-14 18:06:50 +000086 puts $result
87 puts $l "WRITE $result"
88 puts OVER
89 puts $l "WRITE OVER"
90 flush stdout
91 set script ""
92 } else {
93 append script $line
dan5e0ce872010-04-28 17:48:44 +000094 append script "\n"
dane264d982010-04-14 18:06:50 +000095 }
96 }
97 close $l
98}
99close $f