test-lib: remove ancient wait_for_pid() function
[rainbows.git] / t / test-lib.sh
blob5cf5201050f1a60599a8a51dc2e58fb3dda63bfa
1 #!/bin/sh
2 # Copyright (c) 2009 Rainbows! developers
3 . ./my-tap-lib.sh
5 set +u
6 if test -z "$model"
7 then
8 # defaulting to Base would unfortunately fail some concurrency tests
9 model=ThreadSpawn
10 t_info "model undefined, defaulting to $model"
13 set -e
14 RUBY="${RUBY-ruby}"
15 RUBY_VERSION=${RUBY_VERSION-$($RUBY -e 'puts RUBY_VERSION')}
16 t_pfx=$PWD/trash/$model.$T-$RUBY_VERSION
17 set -u
19 PATH=$PWD/bin:$PATH
20 export PATH
22 test -x $PWD/bin/unused_listen || die "must be run in 't' directory"
24 # requires $1 and prints out the value of $2
25 require_check () {
26 lib=$1
27 const=$2
28 if ! $RUBY -r$lib -e "puts $const" >/dev/null 2>&1
29 then
30 t_info "skipping $T since we don't have $lib"
31 exit 0
35 skip_models () {
36 for i in "$@"
38 if test x"$model" != x"$i"
39 then
40 continue
42 t_info "skipping $T since it is not compatible with $model"
43 exit 0
44 done
48 # given a list of variable names, create temporary files and assign
49 # the pathnames to those variables
50 rtmpfiles () {
51 for id in "$@"
53 name=$id
54 _tmp=$t_pfx.$id
55 eval "$id=$_tmp"
57 case $name in
58 *fifo)
59 rm -f $_tmp
60 mkfifo $_tmp
61 T_RM_LIST="$T_RM_LIST $_tmp"
63 *socket)
64 rm -f $_tmp
65 T_RM_LIST="$T_RM_LIST $_tmp"
68 > $_tmp
69 T_OK_RM_LIST="$T_OK_RM_LIST $_tmp"
71 esac
72 done
75 dbgcat () {
76 id=$1
77 eval '_file=$'$id
78 echo "==> $id <=="
79 sed -e "s/^/$id:/" < $_file
82 check_stderr () {
83 set +u
84 _r_err=${1-${r_err}}
85 set -u
86 if grep -i Error $_r_err
87 then
88 die "Errors found in $_r_err"
89 elif grep SIGKILL $_r_err
90 then
91 die "SIGKILL found in $_r_err"
95 # rainbows_setup [ MODEL [ WORKER_CONNECTIONS ] ]
96 rainbows_setup () {
97 eval $(unused_listen)
98 rtmpfiles unicorn_config pid r_err r_out fifo tmp ok
99 cat > $unicorn_config <<EOF
100 listen "$listen"
101 pid "$pid"
102 stderr_path "$r_err"
103 stdout_path "$r_out"
105 after_fork do |server, worker|
106 # test script will block while reading from $fifo,
107 # so notify the script on the first worker we spawn
108 # by opening the FIFO
109 if worker.nr == 0
110 File.open("$fifo", "wb") { |fp| fp.syswrite "START" }
115 # set a higher default for tests since we run heavily-loaded
116 # boxes and sometimes sleep 1s in tests
117 kato=5
118 echo 'Rainbows! do'
119 echo " client_max_body_size nil"
120 if test $# -ge 1
121 then
122 echo " use :$1"
123 test $# -eq 2 && echo " worker_connections $2"
124 if test $# -eq 3
125 then
126 echo " keepalive_timeout $3"
127 else
128 echo " keepalive_timeout $kato"
130 else
131 echo " use :$model"
132 echo " keepalive_timeout $kato"
134 echo end
135 } >> $unicorn_config
138 rainbows_wait_start () {
139 # "cat $fifo" will block until the before_fork hook is called in
140 # the Unicorn config file
141 test xSTART = x"$(cat $fifo)"
142 rainbows_pid=$(cat $pid)
145 rsha1 () {
146 _cmd="$(which sha1sum 2>/dev/null || :)"
147 test -n "$_cmd" || _cmd="$(which openssl 2>/dev/null || :) sha1"
148 test "$_cmd" != " sha1" || _cmd="$(which gsha1sum 2>/dev/null || :)"
150 # last resort, see comments in sha1sum.rb for reasoning
151 test -n "$_cmd" || _cmd=sha1sum.rb
152 expr "$($_cmd)" : '\([a-f0-9]\{40\}\)'
155 req_curl_chunked_upload_err_check () {
156 set +e
157 curl --version 2>/dev/null | awk '$1 == "curl" {
158 split($2, v, /\./)
159 if ((v[1] < 7) || (v[1] == 7 && v[2] < 18))
160 code = 1
162 END { exit(code) }'
163 if test $? -ne 0
164 then
165 t_info "curl >= 7.18.0 required for $T"
166 exit 0
170 case $model in
171 Rev) require_check rev Rev::VERSION ;;
172 Revactor) require_check revactor Revactor::VERSION ;;
173 EventMachine) require_check eventmachine EventMachine::VERSION ;;
174 esac