xref: /freebsd/bin/pwait/tests/pwait_test.sh (revision e17f5b1d307b7b8910d67883e57a9604305906d5)
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_test_case or_flag
236or_flag_head()
237{
238	atf_set "descr" "Test OR flag"
239}
240
241or_flag_body()
242{
243	sleep 2 &
244	p2=$!
245
246	sleep 4 &
247	p4=$!
248
249	sleep 6 &
250	p6=$!
251
252	atf_check \
253		-o inline:"$p2: exited with status 0.\n" \
254		-e empty \
255		-s exit:0 \
256		timeout --preserve-status 15 pwait -o -v $p2 $p4 $p6
257
258	atf_check \
259		-o empty \
260		-e inline:"pwait: $p2: No such process\n" \
261		-s exit:0 \
262		timeout --preserve-status 15 pwait -o $p2 $p4 $p6
263
264	atf_check \
265		-o empty \
266		-e empty \
267		-s exit:0 \
268		timeout --preserve-status 15 pwait -o $p4 $p6
269
270	atf_check \
271		-o empty \
272		-e inline:"pwait: $p4: No such process\n" \
273		-s exit:0 \
274		timeout --preserve-status 15 pwait -o $p4 $p6
275
276	atf_check \
277		-o inline:"$p6: exited with status 0.\n" \
278		-e empty \
279		-s exit:0 \
280		timeout --preserve-status 15 pwait -o -v $p6
281
282	atf_check \
283		-o empty \
284		-e inline:"pwait: $p6: No such process\n" \
285		-s exit:0 \
286		timeout --preserve-status 15 pwait -o $p6
287
288	atf_check \
289		-o empty \
290		-e inline:"kill: $p2: No such process\n" \
291		-s exit:1 \
292		kill -0 $p2
293
294	atf_check \
295		-o empty \
296		-e inline:"kill: $p4: No such process\n" \
297		-s exit:1 \
298		kill -0 $p4
299
300	atf_check \
301		-o empty \
302		-e inline:"kill: $p6: No such process\n" \
303		-s exit:1 \
304		kill -0 $p6
305
306}
307
308or_flag_cleanup()
309{
310	kill $p2 $p4 $p6 >/dev/null 2>&1
311	wait $p2 $p4 $p6 >/dev/null 2>&1
312}
313
314atf_init_test_cases()
315{
316	atf_add_test_case basic
317	atf_add_test_case time_unit
318	atf_add_test_case timeout_trigger_timeout
319	atf_add_test_case timeout_no_timeout
320	atf_add_test_case timeout_many
321	atf_add_test_case or_flag
322}
323