lock_common.tcl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. proc do_multiclient_test {varname script} {
  16. foreach code [list {
  17. if {[info exists ::G(valgrind)]} { db close ; continue }
  18. set ::code2_chan [launch_testfixture]
  19. set ::code3_chan [launch_testfixture]
  20. proc code2 {tcl} { testfixture $::code2_chan $tcl }
  21. proc code3 {tcl} { testfixture $::code3_chan $tcl }
  22. set tn 1
  23. } {
  24. proc code2 {tcl} { uplevel #0 $tcl }
  25. proc code3 {tcl} { uplevel #0 $tcl }
  26. set tn 2
  27. }] {
  28. faultsim_delete_and_reopen
  29. proc code1 {tcl} { uplevel #0 $tcl }
  30. # Open connections [db2] and [db3]. Depending on which iteration this
  31. # is, the connections may be created in this interpreter, or in
  32. # interpreters running in other OS processes. As such, the [db2] and [db3]
  33. # commands should only be accessed within [code2] and [code3] blocks,
  34. # respectively.
  35. #
  36. eval $code
  37. code2 { sqlite3 db2 test.db }
  38. code3 { sqlite3 db3 test.db }
  39. # Shorthand commands. Execute SQL using database connection [db2] or
  40. # [db3]. Return the results.
  41. #
  42. proc sql1 {sql} { db eval $sql }
  43. proc sql2 {sql} { code2 [list db2 eval $sql] }
  44. proc sql3 {sql} { code3 [list db3 eval $sql] }
  45. proc csql1 {sql} { list [catch { sql1 $sql } msg] $msg }
  46. proc csql2 {sql} { list [catch { sql2 $sql } msg] $msg }
  47. proc csql3 {sql} { list [catch { sql3 $sql } msg] $msg }
  48. uplevel set $varname $tn
  49. uplevel $script
  50. catch { code2 { db2 close } }
  51. catch { code3 { db3 close } }
  52. catch { close $::code2_chan }
  53. catch { close $::code3_chan }
  54. catch { db close }
  55. }
  56. }
  57. # Launch another testfixture process to be controlled by this one. A
  58. # channel name is returned that may be passed as the first argument to proc
  59. # 'testfixture' to execute a command. The child testfixture process is shut
  60. # down by closing the channel.
  61. proc launch_testfixture {{prg ""}} {
  62. write_main_loop
  63. if {$prg eq ""} { set prg [info nameofexec] }
  64. if {$prg eq ""} { set prg testfixture }
  65. if {[file tail $prg]==$prg} { set prg [file join . $prg] }
  66. set chan [open "|$prg tf_main.tcl" r+]
  67. fconfigure $chan -buffering line
  68. set rc [catch {
  69. testfixture $chan "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
  70. }]
  71. if {$rc} {
  72. testfixture $chan "set ::sqlite_pending_byte $::sqlite_pending_byte"
  73. }
  74. return $chan
  75. }
  76. # Execute a command in a child testfixture process, connected by two-way
  77. # channel $chan. Return the result of the command, or an error message.
  78. #
  79. proc testfixture {chan cmd} {
  80. puts $chan $cmd
  81. puts $chan OVER
  82. set r ""
  83. while { 1 } {
  84. set line [gets $chan]
  85. if { $line == "OVER" } {
  86. set res [lindex $r 1]
  87. if { [lindex $r 0] } { error $res }
  88. return $res
  89. }
  90. if {[eof $chan]} {
  91. return "ERROR: Child process hung up"
  92. }
  93. append r $line
  94. }
  95. }
  96. proc testfixture_nb_cb {varname chan} {
  97. if {[eof $chan]} {
  98. append ::tfnb($chan) "ERROR: Child process hung up"
  99. set line "OVER"
  100. } else {
  101. set line [gets $chan]
  102. }
  103. if { $line == "OVER" } {
  104. set $varname [lindex $::tfnb($chan) 1]
  105. unset ::tfnb($chan)
  106. close $chan
  107. } else {
  108. append ::tfnb($chan) $line
  109. }
  110. }
  111. proc testfixture_nb {varname cmd} {
  112. set chan [launch_testfixture]
  113. set ::tfnb($chan) ""
  114. fconfigure $chan -blocking 0 -buffering none
  115. puts $chan $cmd
  116. puts $chan OVER
  117. fileevent $chan readable [list testfixture_nb_cb $varname $chan]
  118. return ""
  119. }
  120. # Write the main loop for the child testfixture processes into file
  121. # tf_main.tcl. The parent (this script) interacts with the child processes
  122. # via a two way pipe. The parent writes a script to the stdin of the child
  123. # process, followed by the word "OVER" on a line of its own. The child
  124. # process evaluates the script and writes the results to stdout, followed
  125. # by an "OVER" of its own.
  126. #
  127. set main_loop_written 0
  128. proc write_main_loop {} {
  129. if {$::main_loop_written} return
  130. set wrapper ""
  131. if {[sqlite3 -has-codec] && [info exists ::do_not_use_codec]==0} {
  132. set wrapper "
  133. rename sqlite3 sqlite_orig
  134. proc sqlite3 {args} {[info body sqlite3]}
  135. "
  136. }
  137. set fd [open tf_main.tcl w]
  138. puts $fd [string map [list %WRAPPER% $wrapper] {
  139. %WRAPPER%
  140. set script ""
  141. while {![eof stdin]} {
  142. flush stdout
  143. set line [gets stdin]
  144. if { $line == "OVER" } {
  145. set rc [catch {eval $script} result]
  146. puts [list $rc $result]
  147. puts OVER
  148. flush stdout
  149. set script ""
  150. } else {
  151. append script $line
  152. append script "\n"
  153. }
  154. }
  155. }]
  156. close $fd
  157. set main_loop_written 1
  158. }