tests: port all existing tests to TAP library
[rainbows.git] / t / test-lib.sh
blob5e279a1f25e55ac246c05178b4bc935235fd48d1
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 case $T in
9 t1???-thread-pool-*.sh) model=ThreadPool ;;
10 t2???-thread-spawn-*.sh) model=ThreadSpawn ;;
11 t3???-revactor-*.sh) model=Revactor ;;
12 t4???-rev-*.sh) model=Rev ;;
13 *) model=Base ;;
14 esac
17 set -e
18 RUBY="${RUBY-ruby}"
19 RUBY_VERSION=${RUBY_VERSION-$($RUBY -e 'puts RUBY_VERSION')}
20 t_pfx=$PWD/trash/$T-$RUBY_VERSION
21 set -u
23 PATH=$PWD/bin:$PATH
24 export PATH
26 test -x $PWD/bin/unused_listen || die "must be run in 't' directory"
28 wait_for_pid () {
29 path="$1"
30 nr=30
31 while ! test -s "$path" && test $nr -gt 0
33 nr=$(($nr - 1))
34 sleep 1
35 done
38 # requires $1 and prints out the value of $2
39 require_check () {
40 lib=$1
41 const=$2
42 if ! $RUBY -r$lib -e "puts $const" >/dev/null 2>&1
43 then
44 t_info "skipping $T since we don't have $lib"
45 exit 0
49 # given a list of variable names, create temporary files and assign
50 # the pathnames to those variables
51 rtmpfiles () {
52 for id in "$@"
54 name=$id
55 _tmp=$t_pfx.$id
56 eval "$id=$_tmp"
58 case $name in
59 *fifo)
60 rm -f $_tmp
61 mkfifo $_tmp
62 T_RM_LIST="$T_RM_LIST $_tmp"
65 > $_tmp
66 T_OK_RM_LIST="$T_OK_RM_LIST $_tmp"
68 esac
69 done
72 dbgcat () {
73 id=$1
74 eval '_file=$'$id
75 echo "==> $id <=="
76 sed -e "s/^/$id:/" < $_file
79 check_stderr () {
80 set +u
81 _r_err=${1-${r_err}}
82 set -u
83 if grep Error $_r_err
84 then
85 die "Errors found in $_r_err"
86 elif grep SIGKILL $_r_err
87 then
88 die "SIGKILL found in $_r_err"
92 # rainbows_setup [ MODEL [ WORKER_CONNECTIONS ] ]
93 rainbows_setup () {
94 eval $(unused_listen)
95 rtmpfiles unicorn_config pid r_err r_out fifo tmp ok
96 cat > $unicorn_config <<EOF
97 listen "$listen"
98 pid "$pid"
99 stderr_path "$r_err"
100 stdout_path "$r_out"
102 # close my-tap-lib.sh FDs
103 unless ENV['UNICORN_FD']
104 IO.for_fd(3).close rescue nil
105 IO.for_fd(4).close rescue nil
108 before_fork do |server, worker|
109 # test script will block while reading from $fifo,
110 # so notify the script on the first worker we spawn
111 # by opening the FIFO
112 if worker.nr == 0
113 File.open("$fifo", "wb").close
118 if test $# -ge 1
119 then
120 echo 'Rainbows! do'
121 echo " use :$1"
122 test $# -eq 2 && echo " worker_connections $2"
123 echo end
124 else
125 echo "Rainbows! { use :$model }"
127 } >> $unicorn_config
130 rainbows_wait_start () {
131 # "cat $fifo" will block until the before_fork hook is called in
132 # the Unicorn config file
133 test x = x"$(cat $fifo)"
134 rainbows_pid=$(cat $pid)
137 case $model in
138 Rev) require_check rev Rev::VERSION ;;
139 Revactor) require_check revactor Revactor::VERSION ;;
140 esac