xref: /sqlite-3.40.0/test/lock_common.tcl (revision 31f98fc8)
1e264d983Sdan# 2010 April 14
2e264d983Sdan#
3e264d983Sdan# The author disclaims copyright to this source code.  In place of
4e264d983Sdan# a legal notice, here is a blessing:
5e264d983Sdan#
6e264d983Sdan#    May you do good and not evil.
7e264d983Sdan#    May you find forgiveness for yourself and forgive others.
8e264d983Sdan#    May you share freely, never taking more than you give.
9e264d983Sdan#
10e264d983Sdan#***********************************************************************
11e264d983Sdan# This file contains code used by several different test scripts. The
12e264d983Sdan# code in this file allows testfixture to control another process (or
13e264d983Sdan# processes) to test locking.
14e264d983Sdan#
15e264d983Sdan
16e264d983Sdan# Launch another testfixture process to be controlled by this one. A
17e264d983Sdan# channel name is returned that may be passed as the first argument to proc
18e264d983Sdan# 'testfixture' to execute a command. The child testfixture process is shut
19e264d983Sdan# down by closing the channel.
20e264d983Sdanproc launch_testfixture {} {
21e264d983Sdan  set prg [info nameofexec]
22e264d983Sdan  if {$prg eq ""} {
23e264d983Sdan    set prg [file join . testfixture]
24e264d983Sdan  }
25e264d983Sdan  set chan [open "|$prg tf_main.tcl" r+]
26e264d983Sdan  fconfigure $chan -buffering line
27*31f98fc8Sdan  testfixture $chan "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
28e264d983Sdan  return $chan
29e264d983Sdan}
30e264d983Sdan
31e264d983Sdan# Execute a command in a child testfixture process, connected by two-way
32e264d983Sdan# channel $chan. Return the result of the command, or an error message.
33e264d983Sdanproc testfixture {chan cmd} {
34e264d983Sdan  puts $chan $cmd
35e264d983Sdan  puts $chan OVER
36e264d983Sdan  set r ""
37e264d983Sdan  while { 1 } {
38e264d983Sdan    set line [gets $chan]
39e264d983Sdan    if { $line == "OVER" } {
40e264d983Sdan      return $r
41e264d983Sdan    }
42e264d983Sdan    if {[eof $chan]} {
43e264d983Sdan      return "ERROR: Child process hung up"
44e264d983Sdan    }
45e264d983Sdan    append r $line
46e264d983Sdan  }
47e264d983Sdan}
48e264d983Sdan
49e264d983Sdan# Write the main loop for the child testfixture processes into file
50e264d983Sdan# tf_main.tcl. The parent (this script) interacts with the child processes
51e264d983Sdan# via a two way pipe. The parent writes a script to the stdin of the child
52e264d983Sdan# process, followed by the word "OVER" on a line of its own. The child
53e264d983Sdan# process evaluates the script and writes the results to stdout, followed
54e264d983Sdan# by an "OVER" of its own.
55e264d983Sdanset f [open tf_main.tcl w]
56e264d983Sdanputs $f {
57e264d983Sdan  set l [open log w]
58e264d983Sdan  set script ""
59e264d983Sdan  while {![eof stdin]} {
60e264d983Sdan    flush stdout
61e264d983Sdan    set line [gets stdin]
62e264d983Sdan    puts $l "READ $line"
63e264d983Sdan    if { $line == "OVER" } {
64e264d983Sdan      catch {eval $script} result
65e264d983Sdan      puts $result
66e264d983Sdan      puts $l "WRITE $result"
67e264d983Sdan      puts OVER
68e264d983Sdan      puts $l "WRITE OVER"
69e264d983Sdan      flush stdout
70e264d983Sdan      set script ""
71e264d983Sdan    } else {
72e264d983Sdan      append script $line
73e264d983Sdan      append script " ; "
74e264d983Sdan    }
75e264d983Sdan  }
76e264d983Sdan  close $l
77e264d983Sdan}
78e264d983Sdanclose $f
79