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