support Unicorn 0.96.0+ ready_pipe daemonization
[zbatery.git] / t / test-lib.sh
blob7ea07e7961d8198d01951f37f13c818b326225d8
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"
60 *socket)
61 rm -f $_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 -i 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 # zbatery_setup [ MODEL [ WORKER_CONNECTIONS ] ]
93 zbatery_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 after_fork do |server, worker|
103 # test script will block while reading from $fifo,
104 # so notify the script on the first worker we spawn
105 # by opening the FIFO
106 if worker.nr == 0
107 File.open("$fifo", "wb") { |fp| fp.syswrite "START" }
112 # set a higher default for tests since we run heavily-loaded
113 # boxes and sometimes sleep 1s in tests
114 kato=5
115 echo 'Rainbows! do'
116 if test $# -ge 1
117 then
118 echo " use :$1"
119 test $# -eq 2 && echo " worker_connections $2"
120 if test $# -eq 3
121 then
122 echo " keepalive_timeout $3"
123 else
124 echo " keepalive_timeout $kato"
126 else
127 echo " use :$model"
128 echo " keepalive_timeout $kato"
130 echo end
131 } >> $unicorn_config
134 zbatery_wait_start () {
135 # "cat $fifo" will block until the before_fork hook is called in
136 # the Unicorn config file
137 test xSTART = x"$(cat $fifo)"
138 zbatery_pid=$(cat $pid)
141 rsha1 () {
142 _cmd="$(which sha1sum 2>/dev/null || :)"
143 test -n "$_cmd" || _cmd="$(which openssl 2>/dev/null || :) sha1"
144 test "$_cmd" != " sha1" || _cmd="$(which gsha1sum 2>/dev/null || :)"
146 # last resort, see comments in sha1sum.rb for reasoning
147 test -n "$_cmd" || _cmd=sha1sum.rb
148 expr "$($_cmd < random_blob)" : '\([a-f0-9]\{40\}\)'
151 case $model in
152 Rev) require_check rev Rev::VERSION ;;
153 Revactor) require_check revactor Revactor::VERSION ;;
154 EventMachine) require_check eventmachine EventMachine::VERSION ;;
155 esac