dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 1 | # 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 | |
dan | a4a9095 | 2010-06-15 19:07:42 +0000 | [diff] [blame] | 16 | proc 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 | |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 62 | # 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. |
| 66 | proc launch_testfixture {} { |
dan | 47ee386 | 2010-06-22 15:18:44 +0000 | [diff] [blame^] | 67 | write_main_loop |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 68 | set prg [info nameofexec] |
| 69 | if {$prg eq ""} { |
| 70 | set prg [file join . testfixture] |
| 71 | } |
| 72 | set chan [open "|$prg tf_main.tcl" r+] |
| 73 | fconfigure $chan -buffering line |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 74 | testfixture $chan "sqlite3_test_control_pending_byte $::sqlite_pending_byte" |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 75 | return $chan |
| 76 | } |
| 77 | |
| 78 | # Execute a command in a child testfixture process, connected by two-way |
| 79 | # channel $chan. Return the result of the command, or an error message. |
dan | e91a54e | 2010-06-15 17:44:47 +0000 | [diff] [blame] | 80 | # |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 81 | proc testfixture {chan cmd} { |
| 82 | puts $chan $cmd |
| 83 | puts $chan OVER |
| 84 | set r "" |
| 85 | while { 1 } { |
| 86 | set line [gets $chan] |
| 87 | if { $line == "OVER" } { |
dan | e91a54e | 2010-06-15 17:44:47 +0000 | [diff] [blame] | 88 | set res [lindex $r 1] |
| 89 | if { [lindex $r 0] } { error $res } |
| 90 | return $res |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 91 | } |
| 92 | if {[eof $chan]} { |
| 93 | return "ERROR: Child process hung up" |
| 94 | } |
| 95 | append r $line |
| 96 | } |
| 97 | } |
| 98 | |
dan | 5e0ce87 | 2010-04-28 17:48:44 +0000 | [diff] [blame] | 99 | proc testfixture_nb_cb {varname chan} { |
dan | f9b7671 | 2010-06-01 14:12:45 +0000 | [diff] [blame] | 100 | if {[eof $chan]} { |
| 101 | append ::tfnb($chan) "ERROR: Child process hung up" |
| 102 | set line "OVER" |
| 103 | } else { |
| 104 | set line [gets $chan] |
| 105 | } |
| 106 | |
dan | 5e0ce87 | 2010-04-28 17:48:44 +0000 | [diff] [blame] | 107 | if { $line == "OVER" } { |
dan | e91a54e | 2010-06-15 17:44:47 +0000 | [diff] [blame] | 108 | set $varname [lindex $::tfnb($chan) 1] |
dan | 5e0ce87 | 2010-04-28 17:48:44 +0000 | [diff] [blame] | 109 | unset ::tfnb($chan) |
| 110 | close $chan |
| 111 | } else { |
| 112 | append ::tfnb($chan) $line |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | proc testfixture_nb {varname cmd} { |
| 117 | set chan [launch_testfixture] |
| 118 | set ::tfnb($chan) "" |
| 119 | fconfigure $chan -blocking 0 -buffering none |
| 120 | puts $chan $cmd |
| 121 | puts $chan OVER |
| 122 | fileevent $chan readable [list testfixture_nb_cb $varname $chan] |
| 123 | return "" |
| 124 | } |
| 125 | |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 126 | # Write the main loop for the child testfixture processes into file |
| 127 | # tf_main.tcl. The parent (this script) interacts with the child processes |
| 128 | # via a two way pipe. The parent writes a script to the stdin of the child |
| 129 | # process, followed by the word "OVER" on a line of its own. The child |
| 130 | # process evaluates the script and writes the results to stdout, followed |
| 131 | # by an "OVER" of its own. |
dan | 47ee386 | 2010-06-22 15:18:44 +0000 | [diff] [blame^] | 132 | # |
| 133 | set main_loop_written 0 |
| 134 | proc write_main_loop {} { |
| 135 | if {$::main_loop_written} return |
| 136 | set wrapper "" |
| 137 | if {[sqlite3 -has-codec] && [info exists ::do_not_use_codec]==0} { |
| 138 | set wrapper " |
| 139 | rename sqlite3 sqlite_orig |
| 140 | proc sqlite3 {args} {[info body sqlite3]} |
| 141 | " |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 142 | } |
dan | 47ee386 | 2010-06-22 15:18:44 +0000 | [diff] [blame^] | 143 | |
| 144 | set fd [open tf_main.tcl w] |
| 145 | puts $fd [string map [list %WRAPPER% $wrapper] { |
| 146 | %WRAPPER% |
| 147 | set script "" |
| 148 | while {![eof stdin]} { |
| 149 | flush stdout |
| 150 | set line [gets stdin] |
| 151 | if { $line == "OVER" } { |
| 152 | set rc [catch {eval $script} result] |
| 153 | puts [list $rc $result] |
| 154 | puts OVER |
| 155 | flush stdout |
| 156 | set script "" |
| 157 | } else { |
| 158 | append script $line |
| 159 | append script "\n" |
| 160 | } |
| 161 | } |
| 162 | }] |
| 163 | close $fd |
| 164 | set main_loop_written 1 |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 165 | } |
dan | 47ee386 | 2010-06-22 15:18:44 +0000 | [diff] [blame^] | 166 | |