tests: gracefully exit if EventMachine is not available
[rainbows.git] / t / test-lib.sh
blobe03a956923dedd13c2b619aa52d7a5d0010ad22a
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 wait_for_pid () {
25 path="$1"
26 nr=30
27 while ! test -s "$path" && test $nr -gt 0
29 nr=$(($nr - 1))
30 sleep 1
31 done
34 # requires $1 and prints out the value of $2
35 require_check () {
36 lib=$1
37 const=$2
38 if ! $RUBY -r$lib -e "puts $const" >/dev/null 2>&1
39 then
40 t_info "skipping $T since we don't have $lib"
41 exit 0
45 # given a list of variable names, create temporary files and assign
46 # the pathnames to those variables
47 rtmpfiles () {
48 for id in "$@"
50 name=$id
51 _tmp=$t_pfx.$id
52 eval "$id=$_tmp"
54 case $name in
55 *fifo)
56 rm -f $_tmp
57 mkfifo $_tmp
58 T_RM_LIST="$T_RM_LIST $_tmp"
61 > $_tmp
62 T_OK_RM_LIST="$T_OK_RM_LIST $_tmp"
64 esac
65 done
68 dbgcat () {
69 id=$1
70 eval '_file=$'$id
71 echo "==> $id <=="
72 sed -e "s/^/$id:/" < $_file
75 check_stderr () {
76 set +u
77 _r_err=${1-${r_err}}
78 set -u
79 if grep Error $_r_err
80 then
81 die "Errors found in $_r_err"
82 elif grep SIGKILL $_r_err
83 then
84 die "SIGKILL found in $_r_err"
88 # rainbows_setup [ MODEL [ WORKER_CONNECTIONS ] ]
89 rainbows_setup () {
90 eval $(unused_listen)
91 rtmpfiles unicorn_config pid r_err r_out fifo tmp ok
92 cat > $unicorn_config <<EOF
93 listen "$listen"
94 pid "$pid"
95 stderr_path "$r_err"
96 stdout_path "$r_out"
98 # close my-tap-lib.sh FDs
99 unless ENV['UNICORN_FD']
100 IO.for_fd(3).close rescue nil
101 IO.for_fd(4).close rescue nil
104 before_fork do |server, worker|
105 # test script will block while reading from $fifo,
106 # so notify the script on the first worker we spawn
107 # by opening the FIFO
108 if worker.nr == 0
109 File.open("$fifo", "wb").close
114 if test $# -ge 1
115 then
116 echo 'Rainbows! do'
117 echo " use :$1"
118 test $# -eq 2 && echo " worker_connections $2"
119 echo end
120 else
121 echo "Rainbows! { use :$model }"
123 } >> $unicorn_config
126 rainbows_wait_start () {
127 # "cat $fifo" will block until the before_fork hook is called in
128 # the Unicorn config file
129 test x = x"$(cat $fifo)"
130 rainbows_pid=$(cat $pid)
133 case $model in
134 Rev) require_check rev Rev::VERSION ;;
135 Revactor) require_check revactor Revactor::VERSION ;;
136 EventMachine) require_check eventmachine EventMachine::VERSION ;;
137 esac