1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1999,2008 Oracle.  All rights reserved.
4#
5# $Id: conscript.tcl,v 12.6 2008/01/08 20:58:53 bostic Exp $
6#
7# Script for DB_CONSUME test (test070.tcl).
8# Usage: conscript dir file runtype nitems outputfile tnum args
9# dir: DBHOME directory
10# file: db file on which to operate
11# runtype: PRODUCE, CONSUME or WAIT -- which am I?
12# nitems: number of items to put or get
13# outputfile: where to log consumer results
14# tnum: test number
15
16proc consumescript_produce { db_cmd nitems tnum args } {
17	source ./include.tcl
18	global mydata
19
20	set pid [pid]
21	puts "\tTest$tnum: Producer $pid starting, producing $nitems items."
22
23	set db [eval $db_cmd]
24	error_check_good db_open:$pid [is_valid_db $db] TRUE
25
26	set oret -1
27	set ret 0
28	for { set ndx 0 } { $ndx < $nitems } { incr ndx } {
29		set oret $ret
30		if { 0xffffffff > 0 && $oret > 0x7fffffff } {
31			incr oret [expr 0 - 0x100000000]
32		}
33		set ret [$db put -append [chop_data q $mydata]]
34		error_check_good db_put \
35		    [expr $oret > $ret ? \
36		    ($oret > 0x7fffffff && $ret < 0x7fffffff) : 1] 1
37
38	}
39
40	set ret [catch {$db close} res]
41	error_check_good db_close:$pid $ret 0
42	puts "\t\tTest$tnum: Producer $pid finished."
43}
44
45proc consumescript_consume { db_cmd nitems tnum outputfile mode args } {
46	source ./include.tcl
47	global mydata
48	set pid [pid]
49	puts "\tTest$tnum: Consumer $pid starting, seeking $nitems items."
50
51	set db [eval $db_cmd]
52	error_check_good db_open:$pid [is_valid_db $db] TRUE
53
54	set oid [open $outputfile a]
55
56	for { set ndx 0 } { $ndx < $nitems } { } {
57		set ret [$db get $mode]
58		if { [llength $ret] > 0 } {
59			error_check_good correct_data:$pid \
60				[lindex [lindex $ret 0] 1] [pad_data q $mydata]
61			set rno [lindex [lindex $ret 0] 0]
62			puts $oid $rno
63			incr ndx
64		} else {
65			# No data to consume;  wait.
66		}
67	}
68
69	error_check_good output_close:$pid [close $oid] ""
70
71	set ret [catch {$db close} res]
72	error_check_good db_close:$pid $ret 0
73	puts "\t\tTest$tnum: Consumer $pid finished."
74}
75
76source ./include.tcl
77source $test_path/test.tcl
78
79# Verify usage
80if { $argc < 6 } {
81	puts stderr "FAIL:[timestamp] Usage: $usage"
82	exit
83}
84
85set usage "conscript.tcl dir file runtype nitems outputfile tnum"
86
87# Initialize arguments
88set dir [lindex $argv 0]
89set file [lindex $argv 1]
90set runtype [lindex $argv 2]
91set nitems [lindex $argv 3]
92set outputfile [lindex $argv 4]
93set tnum [lindex $argv 5]
94# args is the string "{ -len 20 -pad 0}", so we need to extract the
95# " -len 20 -pad 0" part.
96set args [lindex [lrange $argv 6 end] 0]
97
98set mydata "consumer data"
99
100# Open env
101set dbenv [berkdb_env -home $dir ]
102error_check_good db_env_create [is_valid_env $dbenv] TRUE
103
104# Figure out db opening command.
105set db_cmd [concat {berkdb_open -create -mode 0644 -queue -env}\
106	$dbenv $args $file]
107
108# Invoke consumescript_produce or consumescript_consume based on $runtype
109if { $runtype == "PRODUCE" } {
110	# Producers have nothing to log;  make sure outputfile is null.
111	error_check_good no_producer_outputfile $outputfile ""
112	consumescript_produce $db_cmd $nitems $tnum $args
113} elseif { $runtype == "CONSUME" } {
114	consumescript_consume $db_cmd $nitems $tnum $outputfile -consume $args
115} elseif { $runtype == "WAIT" } {
116	consumescript_consume $db_cmd $nitems $tnum $outputfile -consume_wait \
117		$args
118} else {
119	error_check_good bad_args $runtype \
120	    "either PRODUCE, CONSUME, or WAIT"
121}
122error_check_good env_close [$dbenv close] 0
123exit
124