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 |
dan | 5653e4d | 2010-08-12 11:25:47 +0000 | [diff] [blame] | 30 | |
| 31 | proc code1 {tcl} { uplevel #0 $tcl } |
dan | a4a9095 | 2010-06-15 19:07:42 +0000 | [diff] [blame] | 32 | |
| 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 | |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 64 | # 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. |
dan | 8311c47 | 2010-08-19 11:05:53 +0000 | [diff] [blame] | 68 | proc launch_testfixture {{prg ""}} { |
dan | 47ee386 | 2010-06-22 15:18:44 +0000 | [diff] [blame] | 69 | write_main_loop |
dan | 3d57905 | 2010-08-20 12:31:30 +0000 | [diff] [blame^] | 70 | if {$prg eq ""} { set prg [file join . [info nameofexec]] } |
dan | 8311c47 | 2010-08-19 11:05:53 +0000 | [diff] [blame] | 71 | if {$prg eq ""} { set prg [file join . testfixture] } |
dan | 3d57905 | 2010-08-20 12:31:30 +0000 | [diff] [blame^] | 72 | if {[file tail $prg]==$prg} { set prg [file join . $prg] } |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 73 | set chan [open "|$prg tf_main.tcl" r+] |
| 74 | fconfigure $chan -buffering line |
dan | 8311c47 | 2010-08-19 11:05:53 +0000 | [diff] [blame] | 75 | 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 | } |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 81 | 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. |
dan | e91a54e | 2010-06-15 17:44:47 +0000 | [diff] [blame] | 86 | # |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 87 | proc 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" } { |
dan | e91a54e | 2010-06-15 17:44:47 +0000 | [diff] [blame] | 94 | set res [lindex $r 1] |
| 95 | if { [lindex $r 0] } { error $res } |
| 96 | return $res |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 97 | } |
| 98 | if {[eof $chan]} { |
| 99 | return "ERROR: Child process hung up" |
| 100 | } |
| 101 | append r $line |
| 102 | } |
| 103 | } |
| 104 | |
dan | 5e0ce87 | 2010-04-28 17:48:44 +0000 | [diff] [blame] | 105 | proc testfixture_nb_cb {varname chan} { |
dan | f9b7671 | 2010-06-01 14:12:45 +0000 | [diff] [blame] | 106 | 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 | |
dan | 5e0ce87 | 2010-04-28 17:48:44 +0000 | [diff] [blame] | 113 | if { $line == "OVER" } { |
dan | e91a54e | 2010-06-15 17:44:47 +0000 | [diff] [blame] | 114 | set $varname [lindex $::tfnb($chan) 1] |
dan | 5e0ce87 | 2010-04-28 17:48:44 +0000 | [diff] [blame] | 115 | unset ::tfnb($chan) |
| 116 | close $chan |
| 117 | } else { |
| 118 | append ::tfnb($chan) $line |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | proc 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 | |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 132 | # 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. |
dan | 47ee386 | 2010-06-22 15:18:44 +0000 | [diff] [blame] | 138 | # |
| 139 | set main_loop_written 0 |
| 140 | proc 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 | " |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 148 | } |
dan | 47ee386 | 2010-06-22 15:18:44 +0000 | [diff] [blame] | 149 | |
| 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 |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 171 | } |
dan | 47ee386 | 2010-06-22 15:18:44 +0000 | [diff] [blame] | 172 | |