blob: 31b1463e5e41f8a0f96ffebd4e7ab600a565161a [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
30
31 # Open connections [db2] and [db3]. Depending on which iteration this
32 # is, the connections may be created in this interpreter, or in
33 # interpreters running in other OS processes. As such, the [db2] and [db3]
34 # commands should only be accessed within [code2] and [code3] blocks,
35 # respectively.
36 #
37 eval $code
38 code2 { sqlite3 db2 test.db }
39 code3 { sqlite3 db3 test.db }
40
41 # Shorthand commands. Execute SQL using database connection [db2] or
42 # [db3]. Return the results.
43 #
44 proc sql1 {sql} { db eval $sql }
45 proc sql2 {sql} { code2 [list db2 eval $sql] }
46 proc sql3 {sql} { code3 [list db3 eval $sql] }
47
48 proc csql1 {sql} { list [catch { sql1 $sql } msg] $msg }
49 proc csql2 {sql} { list [catch { sql2 $sql } msg] $msg }
50 proc csql3 {sql} { list [catch { sql3 $sql } msg] $msg }
51
52 uplevel set $varname $tn
53 uplevel $script
54
55 code2 { db2 close }
56 code3 { db3 close }
57 catch { close $::code2_chan }
58 catch { close $::code3_chan }
59 }
60}
61
dane264d982010-04-14 18:06:50 +000062# Launch another testfixture process to be controlled by this one. A
63# channel name is returned that may be passed as the first argument to proc
64# 'testfixture' to execute a command. The child testfixture process is shut
65# down by closing the channel.
66proc launch_testfixture {} {
67 set prg [info nameofexec]
68 if {$prg eq ""} {
69 set prg [file join . testfixture]
70 }
71 set chan [open "|$prg tf_main.tcl" r+]
72 fconfigure $chan -buffering line
dan31f98fc2010-04-27 05:42:32 +000073 testfixture $chan "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
dane264d982010-04-14 18:06:50 +000074 return $chan
75}
76
77# Execute a command in a child testfixture process, connected by two-way
78# channel $chan. Return the result of the command, or an error message.
dane91a54e2010-06-15 17:44:47 +000079#
dane264d982010-04-14 18:06:50 +000080proc testfixture {chan cmd} {
81 puts $chan $cmd
82 puts $chan OVER
83 set r ""
84 while { 1 } {
85 set line [gets $chan]
86 if { $line == "OVER" } {
dane91a54e2010-06-15 17:44:47 +000087 set res [lindex $r 1]
88 if { [lindex $r 0] } { error $res }
89 return $res
dane264d982010-04-14 18:06:50 +000090 }
91 if {[eof $chan]} {
92 return "ERROR: Child process hung up"
93 }
94 append r $line
95 }
96}
97
dan5e0ce872010-04-28 17:48:44 +000098proc testfixture_nb_cb {varname chan} {
danf9b76712010-06-01 14:12:45 +000099 if {[eof $chan]} {
100 append ::tfnb($chan) "ERROR: Child process hung up"
101 set line "OVER"
102 } else {
103 set line [gets $chan]
104 }
105
dan5e0ce872010-04-28 17:48:44 +0000106 if { $line == "OVER" } {
dane91a54e2010-06-15 17:44:47 +0000107 set $varname [lindex $::tfnb($chan) 1]
dan5e0ce872010-04-28 17:48:44 +0000108 unset ::tfnb($chan)
109 close $chan
110 } else {
111 append ::tfnb($chan) $line
112 }
113}
114
115proc testfixture_nb {varname cmd} {
116 set chan [launch_testfixture]
117 set ::tfnb($chan) ""
118 fconfigure $chan -blocking 0 -buffering none
119 puts $chan $cmd
120 puts $chan OVER
121 fileevent $chan readable [list testfixture_nb_cb $varname $chan]
122 return ""
123}
124
dane264d982010-04-14 18:06:50 +0000125# Write the main loop for the child testfixture processes into file
126# tf_main.tcl. The parent (this script) interacts with the child processes
127# via a two way pipe. The parent writes a script to the stdin of the child
128# process, followed by the word "OVER" on a line of its own. The child
129# process evaluates the script and writes the results to stdout, followed
130# by an "OVER" of its own.
131set f [open tf_main.tcl w]
132puts $f {
133 set l [open log w]
134 set script ""
135 while {![eof stdin]} {
136 flush stdout
137 set line [gets stdin]
138 puts $l "READ $line"
139 if { $line == "OVER" } {
dan5e0ce872010-04-28 17:48:44 +0000140 set rc [catch {eval $script} result]
dane91a54e2010-06-15 17:44:47 +0000141 puts [list $rc $result]
142 puts $l "WRITE [list $rc $result]"
dane264d982010-04-14 18:06:50 +0000143 puts OVER
144 puts $l "WRITE OVER"
145 flush stdout
146 set script ""
147 } else {
148 append script $line
dan5e0ce872010-04-28 17:48:44 +0000149 append script "\n"
dane264d982010-04-14 18:06:50 +0000150 }
151 }
152 close $l
153}
154close $f