Add -N or --no-default-middleware option.
[rainbows.git] / t / test-lib.sh
blob55deb4180f3c504b5ac8aa43ee0b889f488dc926
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 skip_models () {
42 for i in "$@"
44 if test x"$model" != x"$i"
45 then
46 continue
48 t_info "skipping $T since it is not compatible with $model"
49 exit 0
50 done
54 # given a list of variable names, create temporary files and assign
55 # the pathnames to those variables
56 rtmpfiles () {
57 for id in "$@"
59 name=$id
60 _tmp=$t_pfx.$id
61 eval "$id=$_tmp"
63 case $name in
64 *fifo)
65 rm -f $_tmp
66 mkfifo $_tmp
67 T_RM_LIST="$T_RM_LIST $_tmp"
69 *socket)
70 rm -f $_tmp
71 T_RM_LIST="$T_RM_LIST $_tmp"
74 > $_tmp
75 T_OK_RM_LIST="$T_OK_RM_LIST $_tmp"
77 esac
78 done
81 dbgcat () {
82 id=$1
83 eval '_file=$'$id
84 echo "==> $id <=="
85 sed -e "s/^/$id:/" < $_file
88 check_stderr () {
89 set +u
90 _r_err=${1-${r_err}}
91 set -u
92 if grep -i Error $_r_err
93 then
94 die "Errors found in $_r_err"
95 elif grep SIGKILL $_r_err
96 then
97 die "SIGKILL found in $_r_err"
101 # rainbows_setup [ MODEL [ WORKER_CONNECTIONS ] ]
102 rainbows_setup () {
103 eval $(unused_listen)
104 rtmpfiles unicorn_config pid r_err r_out fifo tmp ok
105 cat > $unicorn_config <<EOF
106 listen "$listen"
107 pid "$pid"
108 stderr_path "$r_err"
109 stdout_path "$r_out"
111 after_fork do |server, worker|
112 # test script will block while reading from $fifo,
113 # so notify the script on the first worker we spawn
114 # by opening the FIFO
115 if worker.nr == 0
116 File.open("$fifo", "wb") { |fp| fp.syswrite "START" }
121 # set a higher default for tests since we run heavily-loaded
122 # boxes and sometimes sleep 1s in tests
123 kato=5
124 echo 'Rainbows! do'
125 echo " client_max_body_size nil"
126 if test $# -ge 1
127 then
128 echo " use :$1"
129 test $# -ge 2 && echo " worker_connections $2"
130 if test $# -eq 3
131 then
132 echo " keepalive_timeout $3"
133 else
134 echo " keepalive_timeout $kato"
136 else
137 echo " use :$model"
138 echo " keepalive_timeout $kato"
140 echo end
141 } >> $unicorn_config
144 rainbows_wait_start () {
145 # "cat $fifo" will block until the before_fork hook is called in
146 # the Unicorn config file
147 test xSTART = x"$(cat $fifo)"
148 rainbows_pid=$(cat $pid)
151 wait_for_reload () {
152 case $# in
153 0) err_log=$r_err status=done ;;
154 1) err_log=$1 status=done ;;
155 2) err_log=$1 status=$2 ;;
156 esac
157 while ! egrep '(done|error) reloading' < $err_log >/dev/null
159 sleep 1
160 done
161 grep "$status reloading" $err_log >/dev/null
164 wait_for_reap () {
165 case $# in
166 0) err_log=$r_err ;;
167 1) err_log=$1 ;;
168 esac
170 while ! grep reaped < $err_log >/dev/null
172 sleep 1
173 done
176 rsha1 () {
177 _cmd="$(which sha1sum 2>/dev/null || :)"
178 test -n "$_cmd" || _cmd="$(which openssl 2>/dev/null || :) sha1"
179 test "$_cmd" != " sha1" || _cmd="$(which gsha1sum 2>/dev/null || :)"
181 # last resort, see comments in sha1sum.rb for reasoning
182 test -n "$_cmd" || _cmd=sha1sum.rb
183 expr "$($_cmd)" : '\([a-f0-9]\{40\}\)'
186 req_curl_chunked_upload_err_check () {
187 set +e
188 curl --version 2>/dev/null | awk '$1 == "curl" {
189 split($2, v, /\./)
190 if ((v[1] < 7) || (v[1] == 7 && v[2] < 18))
191 code = 1
193 END { exit(code) }'
194 if test $? -ne 0
195 then
196 t_info "curl >= 7.18.0 required for $T"
197 exit 0
201 check_splice () {
202 case $(uname -s) in
203 Linux) ;;
205 t_info "skipping $T since it's not Linux"
206 exit 0
208 esac
210 # we only allow splice on 2.6.32+
211 min=32 uname_r=$(uname -r)
212 case $uname_r in
213 2.6.*)
214 sub=$(expr "$uname_r" : '2\.6\.\(.*\)$')
215 if test $sub -lt $min
216 then
217 t_info "skipping $T (Linux $(uname_r < 2.6.$min)"
218 exit 0
221 [3-9].*)
222 # OK
225 t_info "skipping $T (Linux $uname_r < 2.6.$min)"
226 exit 0
228 esac
231 check_threaded_app_dispatch () {
232 case $model in
233 ThreadSpawn|ThreadPool) ;;
234 RevThreadSpawn|RevThreadPool) ;;
235 CoolioThreadSpawn|CoolioThreadPool) ;;
236 XEpollThreadSpawn|XEpollThreadPool) ;;
238 t_info "$0 is only compatible with threaded app dispatch"
239 exit 0 ;;
240 esac
243 check_copy_stream () {
244 case $RUBY_VERSION in
245 1.9.*) ;;
247 t_info "skipping $T since it can't IO.copy_stream"
248 exit 0
250 esac
252 case $model in
253 ThreadSpawn|WriterThreadSpawn|ThreadPool|WriterThreadPool|Base) ;;
254 XEpollThreadSpawn|XEpollThreadPool) ;;
256 t_info "skipping $T since it doesn't use copy_stream"
257 exit 0
259 esac
262 case $model in
263 Rev) require_check rev Rev::VERSION ;;
264 Coolio) require_check coolio Coolio::VERSION ;;
265 Revactor) require_check revactor Revactor::VERSION ;;
266 EventMachine) require_check eventmachine EventMachine::VERSION ;;
267 esac