t_wait.sh revision 1.6 1 # $NetBSD: t_wait.sh,v 1.6 2016/03/08 14:24:06 christos Exp $
2 #
3 # Copyright (c) 2008, 2009, 2010 The NetBSD Foundation, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 # POSSIBILITY OF SUCH DAMAGE.
26 #
27 # the implementation of "sh" to test
28 : ${TEST_SH:="/bin/sh"}
29
30 atf_test_case individual
31 individual_head() {
32 atf_set "descr" "Tests that waiting for individual jobs works"
33 }
34 individual_body() {
35 # atf-sh confuses wait for some reason; work it around by creating
36 # a helper script that executes /bin/sh directly.
37 cat >individualhelper.sh <<\EOF
38 sleep 3 &
39 sleep 1 &
40
41 wait %1
42 if [ $? -ne 0 ]; then
43 echo "Waiting of first job failed"
44 exit 1
45 fi
46
47 wait %2
48 if [ $? -ne 0 ]; then
49 echo "Waiting of second job failed"
50 exit 1
51 fi
52
53 exit 0
54 EOF
55 output=$("${TEST_SH}" individualhelper.sh)
56 [ $? -eq 0 ] || atf_fail "${output}"
57 rm -f individualhelper.sh
58 }
59
60 atf_test_case kill
61 kill_head() {
62 atf_set "descr" "Tests that killing the shell while in wait calls trap"
63 }
64 kill_body() {
65 # atf-sh confuses wait for some reason; work it around by creating
66 # a helper script that executes /bin/sh directly.
67 local s=$PWD/killhelper.sh
68 local z=/tmp/killhelper.$$
69 local pid=
70
71 sed 's!${SH}!'"${TEST_SH}!" >$s <<\EOF
72 #! ${SH}
73 trap "echo SIGHUP" 1
74 (sleep 5; exit 3) &
75 sl=$!
76 wait
77 S=$?
78 echo $S
79 while [ $S -ne 0 ] && [ $S != 127 ]; do wait $sl; S=$?; echo $S; done
80 EOF
81 chmod +x $s
82
83 $s > $z &
84 pid=$!
85 sleep 1
86
87 kill -HUP $pid
88 wait
89
90 output="$(cat $z | tr '\n' ' ')"
91 rm -f $s $z
92 if [ "$output" != "SIGHUP 129 3 127 " ]; then
93 atf_fail "${output} != 'SIGHUP 129 3 127 '"
94 fi
95 }
96
97 atf_init_test_cases() {
98 atf_add_test_case individual
99 atf_add_test_case kill
100 }
101