2b935765118d979b0dd85225a7b97ebf6de415b3
[unicorn.git] / t / test-lib.sh
blob2b935765118d979b0dd85225a7b97ebf6de415b3
1 #!/bin/sh
2 # Copyright (c) 2009 Rainbows! hackers
3 # Copyright (c) 2010 Unicorn hackers
4 . ./my-tap-lib.sh
6 set +u
8 # sometimes we rely on http_proxy to avoid wasting bandwidth with Isolate
9 # and multiple Ruby versions
10 NO_PROXY=${UNICORN_TEST_ADDR-127.0.0.1}
11 export NO_PROXY
13 set -e
14 RUBY="${RUBY-ruby}"
15 RUBY_VERSION=${RUBY_VERSION-$($RUBY -e 'puts RUBY_VERSION')}
16 RUBY_ENGINE=${RUBY_ENGINE-$($RUBY -e 'puts((RUBY_ENGINE rescue "ruby"))')}
17 t_pfx=$PWD/trash/$T-$RUBY_ENGINE-$RUBY_VERSION
18 set -u
20 PATH=$PWD/bin:$PATH
21 export PATH
23 test -x $PWD/bin/unused_listen || die "must be run in 't' directory"
25 wait_for_pid () {
26 path="$1"
27 nr=30
28 while ! test -s "$path" && test $nr -gt 0
30 nr=$(($nr - 1))
31 sleep 1
32 done
35 # "unix_time" is not in POSIX, but in GNU, and FreeBSD 9.0 (possibly earlier)
36 unix_time () {
37 $RUBY -e 'puts Time.now.to_i'
40 # "wc -c" outputs leading whitespace on *BSDs, filter it out for portability
41 count_bytes () {
42 wc -c | tr -d '[:space:]'
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
52 case $name in
53 *fifo)
54 _tmp=$t_pfx.$id
55 eval "$id=$_tmp"
56 rm -f $_tmp
57 mkfifo $_tmp
58 T_RM_LIST="$T_RM_LIST $_tmp"
60 *socket)
61 _tmp="$(mktemp -t $id.$$.XXXXXXXX)"
62 if test $(printf "$_tmp" |count_bytes) -gt 108
63 then
64 echo >&2 "$_tmp too long, tests may fail"
65 echo >&2 "Try to set TMPDIR to a shorter path"
67 eval "$id=$_tmp"
68 rm -f $_tmp
69 T_RM_LIST="$T_RM_LIST $_tmp"
72 _tmp=$t_pfx.$id
73 eval "$id=$_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 -v $T $_r_err | grep -i Error
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 # unicorn_setup
102 unicorn_setup () {
103 eval $(unused_listen)
104 port=$(expr $listen : '[^:]*:\([0-9]\+\)')
105 host=$(expr $listen : '\([^:]*\):[0-9]\+')
107 rtmpfiles unicorn_config pid r_err r_out fifo tmp ok
108 cat > $unicorn_config <<EOF
109 listen "$listen"
110 pid "$pid"
111 stderr_path "$r_err"
112 stdout_path "$r_out"
116 unicorn_wait_start () {
117 # no need to play tricks with FIFOs since we got "ready_pipe" now
118 unicorn_pid=$(cat $pid)
121 rsha1 () {
122 sha1sum.rb