blob: b2ff76fdb2f04680643e570748cd6600387192ff [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
dana4a90952010-06-15 19:07:42 +000016proc do_multiclient_test {varname script} {
17
18 foreach code [list {
19 set ::code2_chan [launch_testfixture]
20 set ::code3_chan [launch_testfixture]
21 proc code2 {tcl} { testfixture $::code2_chan $tcl }
22 proc code3 {tcl} { testfixture $::code3_chan $tcl }
23 set tn 1
24 } {
25 proc code2 {tcl} { uplevel #0 $tcl }
26 proc code3 {tcl} { uplevel #0 $tcl }
27 set tn 2
28 }] {
29 faultsim_delete_and_reopen
dan5653e4d2010-08-12 11:25:47 +000030
31 proc code1 {tcl} { uplevel #0 $tcl }
dana4a90952010-06-15 19:07:42 +000032
33 # Open connections [db2] and [db3]. Depending on which iteration this
34 # is, the connections may be created in this interpreter, or in
35 # interpreters running in other OS processes. As such, the [db2] and [db3]
36 # commands should only be accessed within [code2] and [code3] blocks,
37 # respectively.
38 #
39 eval $code
40 code2 { sqlite3 db2 test.db }
41 code3 { sqlite3 db3 test.db }
42
43 # Shorthand commands. Execute SQL using database connection [db2] or
44 # [db3]. Return the results.
45 #
46 proc sql1 {sql} { db eval $sql }
47 proc sql2 {sql} { code2 [list db2 eval $sql] }
48 proc sql3 {sql} { code3 [list db3 eval $sql] }
49
50 proc csql1 {sql} { list [catch { sql1 $sql } msg] $msg }
51 proc csql2 {sql} { list [catch { sql2 $sql } msg] $msg }
52 proc csql3 {sql} { list [catch { sql3 $sql } msg] $msg }
53
54 uplevel set $varname $tn
55 uplevel $script
56
57 code2 { db2 close }
58 code3 { db3 close }
59 catch { close $::code2_chan }
60 catch { close $::code3_chan }
61 }
62}
63
dane264d982010-04-14 18:06:50 +000064# Launch another testfixture process to be controlled by this one. A
65# channel name is returned that may be passed as the first argument to proc
66# 'testfixture' to execute a command. The child testfixture process is shut
67# down by closing the channel.
68proc launch_testfixture {} {
dan47ee3862010-06-22 15:18:44 +000069 write_main_loop
dane264d982010-04-14 18:06:50 +000070 set prg [info nameofexec]
71 if {$prg eq ""} {
72 set prg [file join . testfixture]
73 }
74 set chan [open "|$prg tf_main.tcl" r+]
75 fconfigure $chan -buffering line
dan31f98fc2010-04-27 05:42:32 +000076 testfixture $chan "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
dane264d982010-04-14 18:06:50 +000077 return $chan
78}
79
80# Execute a command in a child testfixture process, connected by two-way
81# channel $chan. Return the result of the command, or an error message.
dane91a54e2010-06-15 17:44:47 +000082#
dane264d982010-04-14 18:06:50 +000083proc testfixture {chan cmd} {
84 puts $chan $cmd
85 puts $chan OVER
86 set r ""
87 while { 1 } {
88 set line [gets $chan]
89 if { $line == "OVER" } {
dane91a54e2010-06-15 17:44:47 +000090 set res [lindex $r 1]
91 if { [lindex $r 0] } { error $res }
92 return $res
dane264d982010-04-14 18:06:50 +000093 }
94 if {[eof $chan]} {
95 return "ERROR: Child process hung up"
96 }
97 append r $line
98 }
99}
100
dan5e0ce872010-04-28 17:48:44 +0000101proc testfixture_nb_cb {varname chan} {
danf9b76712010-06-01 14:12:45 +0000102 if {[eof $chan]} {
103 append ::tfnb($chan) "ERROR: Child process hung up"
104 set line "OVER"
105 } else {
106 set line [gets $chan]
107 }
108
dan5e0ce872010-04-28 17:48:44 +0000109 if { $line == "OVER" } {
dane91a54e2010-06-15 17:44:47 +0000110 set $varname [lindex $::tfnb($chan) 1]
dan5e0ce872010-04-28 17:48:44 +0000111 unset ::tfnb($chan)
112 close $chan
113 } else {
114 append ::tfnb($chan) $line
115 }
116}
117
118proc testfixture_nb {varname cmd} {
119 set chan [launch_testfixture]
120 set ::tfnb($chan) ""
121 fconfigure $chan -blocking 0 -buffering none
122 puts $chan $cmd
123 puts $chan OVER
124 fileevent $chan readable [list testfixture_nb_cb $varname $chan]
125 return ""
126}
127
dane264d982010-04-14 18:06:50 +0000128# Write the main loop for the child testfixture processes into file
129# tf_main.tcl. The parent (this script) interacts with the child processes
130# via a two way pipe. The parent writes a script to the stdin of the child
131# process, followed by the word "OVER" on a line of its own. The child
132# process evaluates the script and writes the results to stdout, followed
133# by an "OVER" of its own.
dan47ee3862010-06-22 15:18:44 +0000134#
135set main_loop_written 0
136proc write_main_loop {} {
137 if {$::main_loop_written} return
138 set wrapper ""
139 if {[sqlite3 -has-codec] && [info exists ::do_not_use_codec]==0} {
140 set wrapper "
141 rename sqlite3 sqlite_orig
142 proc sqlite3 {args} {[info body sqlite3]}
143 "
dane264d982010-04-14 18:06:50 +0000144 }
dan47ee3862010-06-22 15:18:44 +0000145
146 set fd [open tf_main.tcl w]
147 puts $fd [string map [list %WRAPPER% $wrapper] {
148 %WRAPPER%
149 set script ""
150 while {![eof stdin]} {
151 flush stdout
152 set line [gets stdin]
153 if { $line == "OVER" } {
154 set rc [catch {eval $script} result]
155 puts [list $rc $result]
156 puts OVER
157 flush stdout
158 set script ""
159 } else {
160 append script $line
161 append script "\n"
162 }
163 }
164 }]
165 close $fd
166 set main_loop_written 1
dane264d982010-04-14 18:06:50 +0000167}
dan47ee3862010-06-22 15:18:44 +0000168