xref: /freebsd/bin/pwait/tests/pwait_test.sh (revision 5922e9d7e72bfa8a85b0f37bcfd1a8b5d866ec3b)
1
2atf_test_case basic
3basic_head()
4{
5	atf_set "descr" "Basic tests on pwait(1) utility"
6}
7
8basic_body()
9{
10	sleep 1 &
11	p1=$!
12
13	sleep 5 &
14	p5=$!
15
16	sleep 10 &
17	p10=$!
18
19	atf_check \
20		-o empty \
21		-e empty \
22		-s exit:0 \
23		timeout --preserve-status 15 pwait $p1 $p5 $p10
24
25	atf_check \
26		-o empty \
27		-e inline:"kill: $p1: No such process\n" \
28		-s exit:1 \
29		kill -0 $p1
30
31	atf_check \
32		-o empty \
33		-e inline:"kill: $p5: No such process\n" \
34		-s exit:1 \
35		kill -0 $p5
36
37	atf_check \
38		-o empty \
39		-e inline:"kill: $p10: No such process\n" \
40		-s exit:1 \
41		kill -0 $p10
42
43}
44
45basic_cleanup()
46{
47	kill $p1 $p5 $p10 >/dev/null 2>&1
48	wait $p1 $p5 $p10 >/dev/null 2>&1
49}
50
51atf_test_case time_unit
52time_unit_head()
53{
54	atf_set "descr" "Test parsing the timeout unit and value"
55}
56
57time_unit_body()
58{
59	atf_check \
60		-o empty \
61		-e inline:"pwait: timeout unit\n" \
62		-s exit:65 \
63		timeout --preserve-status 2 pwait -t 1d $$
64
65	atf_check \
66		-o empty \
67		-e inline:"pwait: timeout unit\n" \
68		-s exit:65 \
69		timeout --preserve-status 2 pwait -t 1d $$
70
71	atf_check \
72		-o empty \
73		-e inline:"pwait: timeout value\n" \
74		-s exit:65 \
75		timeout --preserve-status 2 pwait -t -1 $$
76
77	atf_check \
78		-o empty \
79		-e inline:"pwait: timeout value\n" \
80		-s exit:65 \
81		timeout --preserve-status 2 pwait -t 100000001 $$
82
83	# These long duration cases are expected to timeout from the
84	# timeout utility rather than pwait -t.
85	atf_check \
86		-o empty \
87		-e empty \
88		-s signal:15 \
89		timeout --preserve-status 2 pwait -t 100000000 $$
90
91	atf_check \
92		-o empty \
93		-e empty \
94		-s signal:15 \
95		timeout --preserve-status 2 pwait -t 1h $$
96
97	atf_check \
98		-o empty \
99		-e empty \
100		-s signal:15 \
101		timeout --preserve-status 2 pwait -t 1.5h $$
102
103	atf_check \
104		-o empty \
105		-e empty \
106		-s signal:15 \
107		timeout --preserve-status 2 pwait -t 1m $$
108
109	atf_check \
110		-o empty \
111		-e empty \
112		-s signal:15 \
113		timeout --preserve-status 2 pwait -t 1.5m $$
114
115	atf_check \
116		-o empty \
117		-e empty \
118		-s signal:15 \
119		timeout --preserve-status 2 pwait -t 0 $$
120
121	# The rest are fast enough that pwait -t is expected to trigger
122	# the timeout.
123	atf_check \
124		-o empty \
125		-e empty \
126		-s exit:124 \
127		timeout --preserve-status 2 pwait -t 1s $$
128
129	atf_check \
130		-o empty \
131		-e empty \
132		-s exit:124 \
133		timeout --preserve-status 2 pwait -t 1.5s $$
134
135	atf_check \
136		-o empty \
137		-e empty \
138		-s exit:124 \
139		timeout --preserve-status 2 pwait -t 1 $$
140
141	atf_check \
142		-o empty \
143		-e empty \
144		-s exit:124 \
145		timeout --preserve-status 2 pwait -t 1.5 $$
146
147	atf_check \
148		-o empty \
149		-e empty \
150		-s exit:124 \
151		timeout --preserve-status 2 pwait -t 0.5 $$
152}
153
154atf_test_case timeout_trigger_timeout
155timeout_trigger_timeout_head()
156{
157	atf_set "descr" "Test that exceeding the timeout is detected"
158}
159
160timeout_trigger_timeout_body()
161{
162	sleep 10 &
163	p10=$!
164
165	atf_check \
166		-o empty \
167		-e empty \
168		-s exit:124 \
169		timeout --preserve-status 6.5 pwait -t 5 $p10
170}
171
172timeout_trigger_timeout_cleanup()
173{
174	kill $p10 >/dev/null 2>&1
175	wait $p10 >/dev/null 2>&1
176}
177
178atf_test_case timeout_no_timeout
179timeout_no_timeout_head()
180{
181	atf_set "descr" "Test that not exceeding the timeout continues to wait"
182}
183
184timeout_no_timeout_body()
185{
186	sleep 10 &
187	p10=$!
188
189	atf_check \
190		-o empty \
191		-e empty \
192		-s exit:0 \
193		timeout --preserve-status 11.5 pwait -t 12 $p10
194}
195
196timeout_no_timeout_cleanup()
197{
198	kill $p10 >/dev/null 2>&1
199	wait $p10 >/dev/null 2>&1
200}
201
202atf_test_case timeout_many
203timeout_many_head()
204{
205	atf_set "descr" "Test timeout on many processes"
206}
207
208timeout_many_body()
209{
210	sleep 1 &
211	p1=$!
212
213	sleep 5 &
214	p5=$!
215
216	sleep 10 &
217	p10=$!
218
219	atf_check \
220		-o empty \
221		-e empty \
222		-s exit:124 \
223		timeout --preserve-status 7.5 pwait -t 6 $p1 $p5 $p10
224}
225
226timeout_many_cleanup()
227{
228	kill $p1 $p5 $p10 >/dev/null 2>&1
229	wait $p1 $p5 $p10 >/dev/null 2>&1
230}
231
232atf_test_case or_flag
233or_flag_head()
234{
235	atf_set "descr" "Test OR flag"
236}
237
238or_flag_body()
239{
240	sleep 2 &
241	p2=$!
242
243	sleep 4 &
244	p4=$!
245
246	sleep 6 &
247	p6=$!
248
249	atf_check \
250		-o inline:"$p2: exited with status 0.\n" \
251		-e empty \
252		-s exit:0 \
253		timeout --preserve-status 15 pwait -o -v $p2 $p4 $p6
254
255	atf_check \
256		-o empty \
257		-e inline:"pwait: $p2: No such process\n" \
258		-s exit:0 \
259		timeout --preserve-status 15 pwait -o $p2 $p4 $p6
260
261	atf_check \
262		-o empty \
263		-e empty \
264		-s exit:0 \
265		timeout --preserve-status 15 pwait -o $p4 $p6
266
267	atf_check \
268		-o empty \
269		-e inline:"pwait: $p4: No such process\n" \
270		-s exit:0 \
271		timeout --preserve-status 15 pwait -o $p4 $p6
272
273	atf_check \
274		-o inline:"$p6: exited with status 0.\n" \
275		-e empty \
276		-s exit:0 \
277		timeout --preserve-status 15 pwait -o -v $p6
278
279	atf_check \
280		-o empty \
281		-e inline:"pwait: $p6: No such process\n" \
282		-s exit:0 \
283		timeout --preserve-status 15 pwait -o $p6
284
285	atf_check \
286		-o empty \
287		-e inline:"kill: $p2: No such process\n" \
288		-s exit:1 \
289		kill -0 $p2
290
291	atf_check \
292		-o empty \
293		-e inline:"kill: $p4: No such process\n" \
294		-s exit:1 \
295		kill -0 $p4
296
297	atf_check \
298		-o empty \
299		-e inline:"kill: $p6: No such process\n" \
300		-s exit:1 \
301		kill -0 $p6
302
303}
304
305or_flag_cleanup()
306{
307	kill $p2 $p4 $p6 >/dev/null 2>&1
308	wait $p2 $p4 $p6 >/dev/null 2>&1
309}
310
311atf_test_case print
312print_head()
313{
314	atf_set "descr" "Test the -p flag"
315}
316
317print_body()
318{
319	sleep 1 &
320	p1=$!
321
322	sleep 5 &
323	p5=$!
324
325	sleep 10 &
326	p10=$!
327
328	atf_check \
329		-o inline:"$p5\n$p10\n" \
330		-s exit:124 \
331		pwait -t 2 -p $p10 $p5 $p1 $p5 $p10
332
333	atf_check \
334		-e inline:"kill: $p1: No such process\n" \
335		-s exit:1 \
336		kill -0 $p1
337
338	atf_check kill -0 $p5
339	atf_check kill -0 $p10
340}
341
342print_cleanup()
343{
344	kill $p1 $p5 $p10 >/dev/null 2>&1
345	wait $p1 $p5 $p10 >/dev/null 2>&1
346}
347
348atf_init_test_cases()
349{
350	atf_add_test_case basic
351	atf_add_test_case time_unit
352	atf_add_test_case timeout_trigger_timeout
353	atf_add_test_case timeout_no_timeout
354	atf_add_test_case timeout_many
355	atf_add_test_case or_flag
356	atf_add_test_case print
357}
358