xref: /freebsd/bin/pwait/tests/pwait_test.sh (revision 389e4940069316fe667ffa263fa7d6390d0a960f)
1# $FreeBSD$
2
3atf_test_case basic
4basic_head()
5{
6	atf_set "descr" "Basic tests on pwait(1) utility"
7}
8
9basic_body()
10{
11	sleep 1 &
12	p1=$!
13
14	sleep 5 &
15	p5=$!
16
17	sleep 10 &
18	p10=$!
19
20	atf_check \
21		-o empty \
22		-e empty \
23		-s exit:0 \
24		timeout --preserve-status 15 pwait $p1 $p5 $p10
25
26	atf_check \
27		-o empty \
28		-e inline:"kill: $p1: No such process\n" \
29		-s exit:1 \
30		kill -0 $p1
31
32	atf_check \
33		-o empty \
34		-e inline:"kill: $p5: No such process\n" \
35		-s exit:1 \
36		kill -0 $p5
37
38	atf_check \
39		-o empty \
40		-e inline:"kill: $p10: No such process\n" \
41		-s exit:1 \
42		kill -0 $p10
43
44}
45
46basic_cleanup()
47{
48	kill $p1 $p5 $p10 >/dev/null 2>&1
49	wait $p1 $p5 $p10 >/dev/null 2>&1
50}
51
52atf_test_case time_unit
53time_unit_head()
54{
55	atf_set "descr" "Test parsing the timeout unit and value"
56}
57
58time_unit_body()
59{
60	init=1
61
62	atf_check \
63		-o empty \
64		-e inline:"pwait: timeout unit\n" \
65		-s exit:65 \
66		timeout --preserve-status 2 pwait -t 1d $init
67
68	atf_check \
69		-o empty \
70		-e inline:"pwait: timeout unit\n" \
71		-s exit:65 \
72		timeout --preserve-status 2 pwait -t 1d $init
73
74	atf_check \
75		-o empty \
76		-e inline:"pwait: timeout value\n" \
77		-s exit:65 \
78		timeout --preserve-status 2 pwait -t -1 $init
79
80	atf_check \
81		-o empty \
82		-e inline:"pwait: timeout value\n" \
83		-s exit:65 \
84		timeout --preserve-status 2 pwait -t 100000001 $init
85
86	# These long duration cases are expected to timeout from the
87	# timeout utility rather than pwait -t.
88	atf_check \
89		-o empty \
90		-e empty \
91		-s exit:143 \
92		timeout --preserve-status 2 pwait -t 100000000 $init
93
94	atf_check \
95		-o empty \
96		-e empty \
97		-s exit:143 \
98		timeout --preserve-status 2 pwait -t 1h $init
99
100	atf_check \
101		-o empty \
102		-e empty \
103		-s exit:143 \
104		timeout --preserve-status 2 pwait -t 1.5h $init
105
106	atf_check \
107		-o empty \
108		-e empty \
109		-s exit:143 \
110		timeout --preserve-status 2 pwait -t 1m $init
111
112	atf_check \
113		-o empty \
114		-e empty \
115		-s exit:143 \
116		timeout --preserve-status 2 pwait -t 1.5m $init
117
118	atf_check \
119		-o empty \
120		-e empty \
121		-s exit:143 \
122		timeout --preserve-status 2 pwait -t 0 $init
123
124	# The rest are fast enough that pwait -t is expected to trigger
125	# the timeout.
126	atf_check \
127		-o empty \
128		-e empty \
129		-s exit:124 \
130		timeout --preserve-status 2 pwait -t 1s $init
131
132	atf_check \
133		-o empty \
134		-e empty \
135		-s exit:124 \
136		timeout --preserve-status 2 pwait -t 1.5s $init
137
138	atf_check \
139		-o empty \
140		-e empty \
141		-s exit:124 \
142		timeout --preserve-status 2 pwait -t 1 $init
143
144	atf_check \
145		-o empty \
146		-e empty \
147		-s exit:124 \
148		timeout --preserve-status 2 pwait -t 1.5 $init
149
150	atf_check \
151		-o empty \
152		-e empty \
153		-s exit:124 \
154		timeout --preserve-status 2 pwait -t 0.5 $init
155}
156
157atf_test_case timeout_trigger_timeout
158timeout_trigger_timeout_head()
159{
160	atf_set "descr" "Test that exceeding the timeout is detected"
161}
162
163timeout_trigger_timeout_body()
164{
165	sleep 10 &
166	p10=$!
167
168	atf_check \
169		-o empty \
170		-e empty \
171		-s exit:124 \
172		timeout --preserve-status 6.5 pwait -t 5 $p10
173}
174
175timeout_trigger_timeout_cleanup()
176{
177	kill $p10 >/dev/null 2>&1
178	wait $p10 >/dev/null 2>&1
179}
180
181atf_test_case timeout_no_timeout
182timeout_no_timeout_head()
183{
184	atf_set "descr" "Test that not exceeding the timeout continues to wait"
185}
186
187timeout_no_timeout_body()
188{
189	sleep 10 &
190	p10=$!
191
192	atf_check \
193		-o empty \
194		-e empty \
195		-s exit:0 \
196		timeout --preserve-status 11.5 pwait -t 12 $p10
197}
198
199timeout_no_timeout_cleanup()
200{
201	kill $p10 >/dev/null 2>&1
202	wait $p10 >/dev/null 2>&1
203}
204
205atf_test_case timeout_many
206timeout_many_head()
207{
208	atf_set "descr" "Test timeout on many processes"
209}
210
211timeout_many_body()
212{
213	sleep 1 &
214	p1=$!
215
216	sleep 5 &
217	p5=$!
218
219	sleep 10 &
220	p10=$!
221
222	atf_check \
223		-o empty \
224		-e empty \
225		-s exit:124 \
226		timeout --preserve-status 7.5 pwait -t 6 $p1 $p5 $p10
227}
228
229timeout_many_cleanup()
230{
231	kill $p1 $p5 $p10 >/dev/null 2>&1
232	wait $p1 $p5 $p10 >/dev/null 2>&1
233}
234
235atf_init_test_cases()
236{
237	atf_add_test_case basic
238	atf_add_test_case time_unit
239	atf_add_test_case timeout_trigger_timeout
240	atf_add_test_case timeout_no_timeout
241	atf_add_test_case timeout_many
242}
243