8921da60df5cd8c99a3495683c2f6ef739be7c10
[rainbows.git] / t / test-lib.sh
blob8921da60df5cd8c99a3495683c2f6ef739be7c10
1 #!/bin/sh
2 # Copyright (c) 2009 Rainbows! developers
3 . ./my-tap-lib.sh
5 set +u
7 # sometimes we rely on http_proxy to avoid wasting bandwidth with Isolate
8 # and multiple Ruby versions
9 NO_PROXY=${UNICORN_TEST_ADDR-127.0.0.1}
10 export NO_PROXY
12 if test -z "$model"
13 then
14 # defaulting to Base would unfortunately fail some concurrency tests
15 model=ThreadSpawn
16 t_info "model undefined, defaulting to $model"
19 set -e
20 RUBY="${RUBY-ruby}"
21 RUBY_VERSION=${RUBY_VERSION-$($RUBY -e 'puts RUBY_VERSION')}
22 t_pfx=$PWD/trash/$model.$T-$RUBY_ENGINE-$RUBY_VERSION
23 set -u
25 PATH=$PWD/bin:$PATH
26 export PATH
28 test -x $PWD/bin/unused_listen || die "must be run in 't' directory"
30 # requires $1 and prints out the value of $2
31 require_check () {
32 lib=$1
33 const=$2
34 if ! $RUBY -r$lib -e "puts $const" >/dev/null 2>&1
35 then
36 t_info "skipping $T since we don't have $lib"
37 exit 0
41 # "date +%s" is not in POSIX, but in GNU, and FreeBSD 9.0 (possibly earlier)
42 unix_time () {
43 $RUBY -e 'puts Time.now.to_i'
46 skip_models () {
47 for i in "$@"
49 if test x"$model" != x"$i"
50 then
51 continue
53 t_info "skipping $T since it is not compatible with $model"
54 exit 0
55 done
59 # given a list of variable names, create temporary files and assign
60 # the pathnames to those variables
61 rtmpfiles () {
62 for id in "$@"
64 name=$id
65 _tmp=$t_pfx.$id
66 eval "$id=$_tmp"
68 case $name in
69 *fifo)
70 rm -f $_tmp
71 mkfifo $_tmp
72 T_RM_LIST="$T_RM_LIST $_tmp"
74 *socket)
75 rm -f $_tmp
76 T_RM_LIST="$T_RM_LIST $_tmp"
79 > $_tmp
80 T_OK_RM_LIST="$T_OK_RM_LIST $_tmp"
82 esac
83 done
86 dbgcat () {
87 id=$1
88 eval '_file=$'$id
89 echo "==> $id <=="
90 sed -e "s/^/$id:/" < $_file
93 check_stderr () {
94 set +u
95 _r_err=${1-${r_err}}
96 set -u
97 if grep -i Error $_r_err
98 then
99 die "Errors found in $_r_err"
100 elif grep SIGKILL $_r_err
101 then
102 die "SIGKILL found in $_r_err"
106 # rainbows_setup [ MODEL [ WORKER_CONNECTIONS ] ]
107 rainbows_setup () {
108 eval $(unused_listen)
109 rtmpfiles unicorn_config pid r_err r_out fifo tmp ok
110 cat > $unicorn_config <<EOF
111 listen "$listen"
112 pid "$pid"
113 stderr_path "$r_err"
114 stdout_path "$r_out"
116 after_fork do |server, worker|
117 # test script will block while reading from $fifo,
118 # so notify the script on the first worker we spawn
119 # by opening the FIFO
120 if worker.nr == 0
121 File.open("$fifo", "wb") { |fp| fp.syswrite "START" }
126 # set a higher default for tests since we run heavily-loaded
127 # boxes and sometimes sleep 1s in tests
128 kato=5
129 echo 'Rainbows! do'
130 echo " client_max_body_size nil"
131 if test $# -ge 1
132 then
133 echo " use :$1"
134 test $# -ge 2 && echo " worker_connections $2"
135 if test $# -eq 3
136 then
137 echo " keepalive_timeout $3"
138 else
139 echo " keepalive_timeout $kato"
141 else
142 echo " use :$model"
143 echo " keepalive_timeout $kato"
145 echo end
146 } >> $unicorn_config
149 rainbows_wait_start () {
150 # "cat $fifo" will block until the before_fork hook is called in
151 # the Unicorn config file
152 test xSTART = x"$(cat $fifo)"
153 rainbows_pid=$(cat $pid)
156 wait_for_reload () {
157 case $# in
158 0) err_log=$r_err status=done ;;
159 1) err_log=$1 status=done ;;
160 2) err_log=$1 status=$2 ;;
161 esac
162 while ! egrep '(done|error) reloading' < $err_log >/dev/null
164 sleep 1
165 done
166 grep "$status reloading" $err_log >/dev/null
169 wait_for_reap () {
170 case $# in
171 0) err_log=$r_err ;;
172 1) err_log=$1 ;;
173 esac
175 while ! grep reaped < $err_log >/dev/null
177 sleep 1
178 done
181 rsha1 () {
182 _cmd="$(which sha1sum 2>/dev/null || :)"
183 test -n "$_cmd" || _cmd="$(which openssl 2>/dev/null || :) sha1"
184 test "$_cmd" != " sha1" || _cmd="$(which gsha1sum 2>/dev/null || :)"
186 # last resort, see comments in sha1sum.rb for reasoning
187 test -n "$_cmd" || _cmd=sha1sum.rb
188 expr "$($_cmd)" : '\([a-f0-9]\{40\}\)'
191 req_curl_chunked_upload_err_check () {
192 set +e
193 curl --version 2>/dev/null | awk '$1 == "curl" {
194 split($2, v, /\./)
195 if ((v[1] < 7) || (v[1] == 7 && v[2] < 18))
196 code = 1
198 END { exit(code) }'
199 if test $? -ne 0
200 then
201 t_info "curl >= 7.18.0 required for $T"
202 exit 0
206 check_splice () {
207 case $(uname -s) in
208 Linux) ;;
210 t_info "skipping $T since it's not Linux"
211 exit 0
213 esac
215 # we only allow splice on 2.6.32+
216 min=32 uname_r=$(uname -r)
217 case $uname_r in
218 2.6.*)
219 sub=$(expr "$uname_r" : '2\.6\.\(.*\)$')
220 if test $sub -lt $min
221 then
222 t_info "skipping $T (Linux $(uname_r < 2.6.$min)"
223 exit 0
226 [3-9].*)
227 # OK
230 t_info "skipping $T (Linux $uname_r < 2.6.$min)"
231 exit 0
233 esac
236 check_threaded_app_dispatch () {
237 case $model in
238 ThreadSpawn|ThreadPool) ;;
239 RevThreadSpawn|RevThreadPool) ;;
240 CoolioThreadSpawn|CoolioThreadPool) ;;
241 XEpollThreadSpawn|XEpollThreadPool) ;;
243 t_info "$0 is only compatible with threaded app dispatch"
244 exit 0 ;;
245 esac
248 check_copy_stream () {
249 case $RUBY_VERSION in
250 1.9.*) ;;
252 t_info "skipping $T since it can't IO.copy_stream"
253 exit 0
255 esac
257 case $model in
258 ThreadSpawn|WriterThreadSpawn|ThreadPool|WriterThreadPool|Base) ;;
259 XEpollThreadSpawn|XEpollThreadPool) ;;
261 t_info "skipping $T since it doesn't use copy_stream"
262 exit 0
264 esac
267 case $model in
268 Rev) require_check rev Rev::VERSION ;;
269 Coolio) require_check coolio Coolio::VERSION ;;
270 Revactor) require_check revactor Revactor::VERSION ;;
271 EventMachine) require_check eventmachine EventMachine::VERSION ;;
272 esac