xref: /freebsd/bin/pwait/tests/pwait_test.sh (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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	init=1
60
61	atf_check \
62		-o empty \
63		-e inline:"pwait: timeout unit\n" \
64		-s exit:65 \
65		timeout --preserve-status 2 pwait -t 1d $init
66
67	atf_check \
68		-o empty \
69		-e inline:"pwait: timeout unit\n" \
70		-s exit:65 \
71		timeout --preserve-status 2 pwait -t 1d $init
72
73	atf_check \
74		-o empty \
75		-e inline:"pwait: timeout value\n" \
76		-s exit:65 \
77		timeout --preserve-status 2 pwait -t -1 $init
78
79	atf_check \
80		-o empty \
81		-e inline:"pwait: timeout value\n" \
82		-s exit:65 \
83		timeout --preserve-status 2 pwait -t 100000001 $init
84
85	# These long duration cases are expected to timeout from the
86	# timeout utility rather than pwait -t.
87	atf_check \
88		-o empty \
89		-e empty \
90		-s exit:143 \
91		timeout --preserve-status 2 pwait -t 100000000 $init
92
93	atf_check \
94		-o empty \
95		-e empty \
96		-s exit:143 \
97		timeout --preserve-status 2 pwait -t 1h $init
98
99	atf_check \
100		-o empty \
101		-e empty \
102		-s exit:143 \
103		timeout --preserve-status 2 pwait -t 1.5h $init
104
105	atf_check \
106		-o empty \
107		-e empty \
108		-s exit:143 \
109		timeout --preserve-status 2 pwait -t 1m $init
110
111	atf_check \
112		-o empty \
113		-e empty \
114		-s exit:143 \
115		timeout --preserve-status 2 pwait -t 1.5m $init
116
117	atf_check \
118		-o empty \
119		-e empty \
120		-s exit:143 \
121		timeout --preserve-status 2 pwait -t 0 $init
122
123	# The rest are fast enough that pwait -t is expected to trigger
124	# the timeout.
125	atf_check \
126		-o empty \
127		-e empty \
128		-s exit:124 \
129		timeout --preserve-status 2 pwait -t 1s $init
130
131	atf_check \
132		-o empty \
133		-e empty \
134		-s exit:124 \
135		timeout --preserve-status 2 pwait -t 1.5s $init
136
137	atf_check \
138		-o empty \
139		-e empty \
140		-s exit:124 \
141		timeout --preserve-status 2 pwait -t 1 $init
142
143	atf_check \
144		-o empty \
145		-e empty \
146		-s exit:124 \
147		timeout --preserve-status 2 pwait -t 1.5 $init
148
149	atf_check \
150		-o empty \
151		-e empty \
152		-s exit:124 \
153		timeout --preserve-status 2 pwait -t 0.5 $init
154}
155
156atf_test_case timeout_trigger_timeout
157timeout_trigger_timeout_head()
158{
159	atf_set "descr" "Test that exceeding the timeout is detected"
160}
161
162timeout_trigger_timeout_body()
163{
164	sleep 10 &
165	p10=$!
166
167	atf_check \
168		-o empty \
169		-e empty \
170		-s exit:124 \
171		timeout --preserve-status 6.5 pwait -t 5 $p10
172}
173
174timeout_trigger_timeout_cleanup()
175{
176	kill $p10 >/dev/null 2>&1
177	wait $p10 >/dev/null 2>&1
178}
179
180atf_test_case timeout_no_timeout
181timeout_no_timeout_head()
182{
183	atf_set "descr" "Test that not exceeding the timeout continues to wait"
184}
185
186timeout_no_timeout_body()
187{
188	sleep 10 &
189	p10=$!
190
191	atf_check \
192		-o empty \
193		-e empty \
194		-s exit:0 \
195		timeout --preserve-status 11.5 pwait -t 12 $p10
196}
197
198timeout_no_timeout_cleanup()
199{
200	kill $p10 >/dev/null 2>&1
201	wait $p10 >/dev/null 2>&1
202}
203
204atf_test_case timeout_many
205timeout_many_head()
206{
207	atf_set "descr" "Test timeout on many processes"
208}
209
210timeout_many_body()
211{
212	sleep 1 &
213	p1=$!
214
215	sleep 5 &
216	p5=$!
217
218	sleep 10 &
219	p10=$!
220
221	atf_check \
222		-o empty \
223		-e empty \
224		-s exit:124 \
225		timeout --preserve-status 7.5 pwait -t 6 $p1 $p5 $p10
226}
227
228timeout_many_cleanup()
229{
230	kill $p1 $p5 $p10 >/dev/null 2>&1
231	wait $p1 $p5 $p10 >/dev/null 2>&1
232}
233
234atf_test_case or_flag
235or_flag_head()
236{
237	atf_set "descr" "Test OR flag"
238}
239
240or_flag_body()
241{
242	sleep 2 &
243	p2=$!
244
245	sleep 4 &
246	p4=$!
247
248	sleep 6 &
249	p6=$!
250
251	atf_check \
252		-o inline:"$p2: exited with status 0.\n" \
253		-e empty \
254		-s exit:0 \
255		timeout --preserve-status 15 pwait -o -v $p2 $p4 $p6
256
257	atf_check \
258		-o empty \
259		-e inline:"pwait: $p2: No such process\n" \
260		-s exit:0 \
261		timeout --preserve-status 15 pwait -o $p2 $p4 $p6
262
263	atf_check \
264		-o empty \
265		-e empty \
266		-s exit:0 \
267		timeout --preserve-status 15 pwait -o $p4 $p6
268
269	atf_check \
270		-o empty \
271		-e inline:"pwait: $p4: No such process\n" \
272		-s exit:0 \
273		timeout --preserve-status 15 pwait -o $p4 $p6
274
275	atf_check \
276		-o inline:"$p6: exited with status 0.\n" \
277		-e empty \
278		-s exit:0 \
279		timeout --preserve-status 15 pwait -o -v $p6
280
281	atf_check \
282		-o empty \
283		-e inline:"pwait: $p6: No such process\n" \
284		-s exit:0 \
285		timeout --preserve-status 15 pwait -o $p6
286
287	atf_check \
288		-o empty \
289		-e inline:"kill: $p2: No such process\n" \
290		-s exit:1 \
291		kill -0 $p2
292
293	atf_check \
294		-o empty \
295		-e inline:"kill: $p4: No such process\n" \
296		-s exit:1 \
297		kill -0 $p4
298
299	atf_check \
300		-o empty \
301		-e inline:"kill: $p6: No such process\n" \
302		-s exit:1 \
303		kill -0 $p6
304
305}
306
307or_flag_cleanup()
308{
309	kill $p2 $p4 $p6 >/dev/null 2>&1
310	wait $p2 $p4 $p6 >/dev/null 2>&1
311}
312
313atf_init_test_cases()
314{
315	atf_add_test_case basic
316	atf_add_test_case time_unit
317	atf_add_test_case timeout_trigger_timeout
318	atf_add_test_case timeout_no_timeout
319	atf_add_test_case timeout_many
320	atf_add_test_case or_flag
321}
322