blob: 0683bbd389b04577b4c2a77eb9065be844506369 [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} {
danf9b76712010-06-01 14:12:45 +000050 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
dan5e0ce872010-04-28 17:48:44 +000057 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
66proc 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
dane264d982010-04-14 18:06:50 +000076# 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.
82set f [open tf_main.tcl w]
83puts $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" } {
dan5e0ce872010-04-28 17:48:44 +000091 set rc [catch {eval $script} result]
dane264d982010-04-14 18:06:50 +000092 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
dan5e0ce872010-04-28 17:48:44 +0000100 append script "\n"
dane264d982010-04-14 18:06:50 +0000101 }
102 }
103 close $l
104}
105close $f