gemspec: set licenses= attribute
[rainbows.git] / t / test-lib.sh
blob3471d690aaab08d7abdb0f9c5048a377ee51a900
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 # "wc -l" outputs leading whitespace on *BSDs, filter it out for portability
47 count_lines () {
48 wc -l | tr -d '[:space:]'
51 # "wc -c" outputs leading whitespace on *BSDs, filter it out for portability
52 count_bytes () {
53 wc -c | tr -d '[:space:]'
56 skip_models () {
57 for i in "$@"
59 if test x"$model" != x"$i"
60 then
61 continue
63 t_info "skipping $T since it is not compatible with $model"
64 exit 0
65 done
69 # given a list of variable names, create temporary files and assign
70 # the pathnames to those variables
71 rtmpfiles () {
72 for id in "$@"
74 name=$id
75 _tmp=$t_pfx.$id
76 eval "$id=$_tmp"
78 case $name in
79 *fifo)
80 rm -f $_tmp
81 mkfifo $_tmp
82 T_RM_LIST="$T_RM_LIST $_tmp"
84 *socket)
85 rm -f $_tmp
86 T_RM_LIST="$T_RM_LIST $_tmp"
89 > $_tmp
90 T_OK_RM_LIST="$T_OK_RM_LIST $_tmp"
92 esac
93 done
96 dbgcat () {
97 id=$1
98 eval '_file=$'$id
99 echo "==> $id <=="
100 sed -e "s/^/$id:/" < $_file
103 check_stderr () {
104 set +u
105 _r_err=${1-${r_err}}
106 set -u
107 if grep -i Error $_r_err
108 then
109 die "Errors found in $_r_err"
110 elif grep SIGKILL $_r_err
111 then
112 die "SIGKILL found in $_r_err"
116 # rainbows_setup [ MODEL [ WORKER_CONNECTIONS ] ]
117 rainbows_setup () {
118 eval $(unused_listen)
119 rtmpfiles unicorn_config pid r_err r_out fifo tmp ok
120 cat > $unicorn_config <<EOF
121 listen "$listen"
122 pid "$pid"
123 stderr_path "$r_err"
124 stdout_path "$r_out"
126 after_fork do |server, worker|
127 # test script will block while reading from $fifo,
128 # so notify the script on the first worker we spawn
129 # by opening the FIFO
130 if worker.nr == 0
131 File.open("$fifo", "wb") { |fp| fp.syswrite "START" }
136 # set a higher default for tests since we run heavily-loaded
137 # boxes and sometimes sleep 1s in tests
138 kato=5
139 echo 'Rainbows! do'
140 echo " client_max_body_size nil"
141 if test $# -ge 1
142 then
143 echo " use :$1"
144 test $# -ge 2 && echo " worker_connections $2"
145 if test $# -eq 3
146 then
147 echo " keepalive_timeout $3"
148 else
149 echo " keepalive_timeout $kato"
151 else
152 echo " use :$model"
153 echo " keepalive_timeout $kato"
155 echo end
156 } >> $unicorn_config
159 rainbows_wait_start () {
160 # "cat $fifo" will block until the before_fork hook is called in
161 # the Unicorn config file
162 test xSTART = x"$(cat $fifo)"
163 rainbows_pid=$(cat $pid)
166 wait_for_reload () {
167 case $# in
168 0) err_log=$r_err status=done ;;
169 1) err_log=$1 status=done ;;
170 2) err_log=$1 status=$2 ;;
171 esac
172 while ! egrep '(done|error) reloading' < $err_log >/dev/null
174 sleep 1
175 done
176 grep "$status reloading" $err_log >/dev/null
179 wait_for_reap () {
180 case $# in
181 0) err_log=$r_err ;;
182 1) err_log=$1 ;;
183 esac
185 while ! grep reaped < $err_log >/dev/null
187 sleep 1
188 done
191 rsha1 () {
192 _cmd="$(which sha1sum 2>/dev/null || :)"
193 test -n "$_cmd" || _cmd="$(which openssl 2>/dev/null || :) sha1"
194 test "$_cmd" != " sha1" || _cmd="$(which gsha1sum 2>/dev/null || :)"
196 # last resort, see comments in sha1sum.rb for reasoning
197 test -n "$_cmd" || _cmd=sha1sum.rb
198 expr "$($_cmd)" : '\([a-f0-9]\{40\}\)'
201 req_curl_chunked_upload_err_check () {
202 set +e
203 curl --version 2>/dev/null | awk '$1 == "curl" {
204 split($2, v, /\./)
205 if ((v[1] < 7) || (v[1] == 7 && v[2] < 18))
206 code = 1
208 END { exit(code) }'
209 if test $? -ne 0
210 then
211 t_info "curl >= 7.18.0 required for $T"
212 exit 0
216 check_splice () {
217 case $(uname -s) in
218 Linux) ;;
220 t_info "skipping $T since it's not Linux"
221 exit 0
223 esac
225 # we only allow splice on 2.6.32+
226 min=32 uname_r=$(uname -r)
227 case $uname_r in
228 2.6.*)
229 sub=$(expr "$uname_r" : '2\.6\.\(.*\)$')
230 if test $sub -lt $min
231 then
232 t_info "skipping $T (Linux $(uname_r < 2.6.$min)"
233 exit 0
236 [3-9].*)
237 # OK
240 t_info "skipping $T (Linux $uname_r < 2.6.$min)"
241 exit 0
243 esac
246 check_threaded_app_dispatch () {
247 case $model in
248 ThreadSpawn|ThreadPool) ;;
249 RevThreadSpawn|RevThreadPool) ;;
250 CoolioThreadSpawn|CoolioThreadPool) ;;
251 XEpollThreadSpawn|XEpollThreadPool) ;;
253 t_info "$0 is only compatible with threaded app dispatch"
254 exit 0 ;;
255 esac
258 check_copy_stream () {
259 case $RUBY_VERSION in
260 1.9.*) ;;
262 t_info "skipping $T since it can't IO.copy_stream"
263 exit 0
265 esac
267 case $model in
268 ThreadSpawn|WriterThreadSpawn|ThreadPool|WriterThreadPool|Base) ;;
269 XEpollThreadSpawn|XEpollThreadPool) ;;
271 t_info "skipping $T since it doesn't use copy_stream"
272 exit 0
274 esac
277 case $model in
278 Rev) require_check rev Rev::VERSION ;;
279 Coolio) require_check coolio Coolio::VERSION ;;
280 Revactor) require_check revactor Revactor::VERSION ;;
281 EventMachine) require_check eventmachine EventMachine::VERSION ;;
282 esac