blob: acb517447ef7850860902fa817ecb77db805793a [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.
dan8311c472010-08-19 11:05:53 +000068proc launch_testfixture {{prg ""}} {
dan47ee3862010-06-22 15:18:44 +000069 write_main_loop
dan3d579052010-08-20 12:31:30 +000070 if {$prg eq ""} { set prg [file join . [info nameofexec]] }
dan8311c472010-08-19 11:05:53 +000071 if {$prg eq ""} { set prg [file join . testfixture] }
dan3d579052010-08-20 12:31:30 +000072 if {[file tail $prg]==$prg} { set prg [file join . $prg] }
dane264d982010-04-14 18:06:50 +000073 set chan [open "|$prg tf_main.tcl" r+]
74 fconfigure $chan -buffering line
dan8311c472010-08-19 11:05:53 +000075 set rc [catch {
76 testfixture $chan "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
77 }]
78 if {$rc} {
79 testfixture $chan "set ::sqlite_pending_byte $::sqlite_pending_byte"
80 }
dane264d982010-04-14 18:06:50 +000081 return $chan
82}
83
84# Execute a command in a child testfixture process, connected by two-way
85# channel $chan. Return the result of the command, or an error message.
dane91a54e2010-06-15 17:44:47 +000086#
dane264d982010-04-14 18:06:50 +000087proc testfixture {chan cmd} {
88 puts $chan $cmd
89 puts $chan OVER
90 set r ""
91 while { 1 } {
92 set line [gets $chan]
93 if { $line == "OVER" } {
dane91a54e2010-06-15 17:44:47 +000094 set res [lindex $r 1]
95 if { [lindex $r 0] } { error $res }
96 return $res
dane264d982010-04-14 18:06:50 +000097 }
98 if {[eof $chan]} {
99 return "ERROR: Child process hung up"
100 }
101 append r $line
102 }
103}
104
dan5e0ce872010-04-28 17:48:44 +0000105proc testfixture_nb_cb {varname chan} {
danf9b76712010-06-01 14:12:45 +0000106 if {[eof $chan]} {
107 append ::tfnb($chan) "ERROR: Child process hung up"
108 set line "OVER"
109 } else {
110 set line [gets $chan]
111 }
112
dan5e0ce872010-04-28 17:48:44 +0000113 if { $line == "OVER" } {
dane91a54e2010-06-15 17:44:47 +0000114 set $varname [lindex $::tfnb($chan) 1]
dan5e0ce872010-04-28 17:48:44 +0000115 unset ::tfnb($chan)
116 close $chan
117 } else {
118 append ::tfnb($chan) $line
119 }
120}
121
122proc testfixture_nb {varname cmd} {
123 set chan [launch_testfixture]
124 set ::tfnb($chan) ""
125 fconfigure $chan -blocking 0 -buffering none
126 puts $chan $cmd
127 puts $chan OVER
128 fileevent $chan readable [list testfixture_nb_cb $varname $chan]
129 return ""
130}
131
dane264d982010-04-14 18:06:50 +0000132# Write the main loop for the child testfixture processes into file
133# tf_main.tcl. The parent (this script) interacts with the child processes
134# via a two way pipe. The parent writes a script to the stdin of the child
135# process, followed by the word "OVER" on a line of its own. The child
136# process evaluates the script and writes the results to stdout, followed
137# by an "OVER" of its own.
dan47ee3862010-06-22 15:18:44 +0000138#
139set main_loop_written 0
140proc write_main_loop {} {
141 if {$::main_loop_written} return
142 set wrapper ""
143 if {[sqlite3 -has-codec] && [info exists ::do_not_use_codec]==0} {
144 set wrapper "
145 rename sqlite3 sqlite_orig
146 proc sqlite3 {args} {[info body sqlite3]}
147 "
dane264d982010-04-14 18:06:50 +0000148 }
dan47ee3862010-06-22 15:18:44 +0000149
150 set fd [open tf_main.tcl w]
151 puts $fd [string map [list %WRAPPER% $wrapper] {
152 %WRAPPER%
153 set script ""
154 while {![eof stdin]} {
155 flush stdout
156 set line [gets stdin]
157 if { $line == "OVER" } {
158 set rc [catch {eval $script} result]
159 puts [list $rc $result]
160 puts OVER
161 flush stdout
162 set script ""
163 } else {
164 append script $line
165 append script "\n"
166 }
167 }
168 }]
169 close $fd
170 set main_loop_written 1
dane264d982010-04-14 18:06:50 +0000171}
dan47ee3862010-06-22 15:18:44 +0000172