OILS / spec / background.test.sh View on Github | oilshell.org

184 lines, 81 significant
1## oils_failures_allowed: 2
2## compare_shells: dash bash mksh
3
4# Job control constructs:
5# & terminator (instead of ;)
6# $! -- last PID
7# wait builtin (wait -n waits for next)
8#
9# Only interactive:
10# fg
11# bg
12# %1 -- current job
13
14#### wait with nothing to wait for
15wait
16## status: 0
17
18#### wait -n with nothing to wait for
19# The 127 is STILL overloaded. Copying bash for now.
20wait -n
21## status: 127
22## N-I dash status: 2
23## N-I mksh status: 1
24
25#### wait with jobspec syntax %nonexistent
26wait %nonexistent
27## status: 127
28## OK dash status: 2
29
30#### wait with invalid PID
31wait 12345678
32## status: 127
33
34#### wait with invalid arg
35wait zzz
36## status: 2
37## OK bash status: 1
38# mksh confuses a syntax error with 'command not found'!
39## BUG mksh status: 127
40
41#### Builtin in background
42echo async &
43wait
44## stdout: async
45
46#### External command in background
47sleep 0.01 &
48wait
49## stdout-json: ""
50
51#### Pipeline in Background
52echo hi | { exit 99; } &
53wait $!
54echo status=$?
55## stdout: status=99
56
57#### Wait for job doesn't support PIPESTATUS
58
59# foreground works
60{ echo hi; exit 55; } | false
61echo status=$? pipestatus=${PIPESTATUS[@]}
62
63{ echo hi; exit 55; } | false &
64echo status=$? pipestatus=${PIPESTATUS[@]}
65
66# Hm pipestatus doesn't work
67wait %+
68#wait %1
69#wait $!
70echo status=$? pipestatus=${PIPESTATUS[@]}
71
72## STDOUT:
73status=1 pipestatus=55 1
74status=0 pipestatus=0
75status=1 pipestatus=1
76## END
77## N-I dash status: 2
78## N-I dash stdout-json: ""
79
80#### Brace group in background, wait all
81{ sleep 0.09; exit 9; } &
82{ sleep 0.07; exit 7; } &
83wait # wait for all gives 0
84echo "status=$?"
85## stdout: status=0
86
87#### Wait on background process PID
88{ sleep 0.09; exit 9; } &
89pid1=$!
90{ sleep 0.07; exit 7; } &
91pid2=$!
92wait $pid2
93echo "status=$?"
94wait $pid1
95echo "status=$?"
96## stdout-json: "status=7\nstatus=9\n"
97
98#### Wait on multiple specific IDs returns last status
99{ sleep 0.08; exit 8; } &
100jid1=$!
101{ sleep 0.09; exit 9; } &
102jid2=$!
103{ sleep 0.07; exit 7; } &
104jid3=$!
105wait $jid1 $jid2 $jid3 # NOTE: not using %1 %2 %3 syntax on purpose
106echo "status=$?" # third job I think
107## stdout: status=7
108
109#### wait -n
110case $SH in (dash|mksh) return ;; esac
111
112{ sleep 0.09; exit 9; } &
113{ sleep 0.03; exit 3; } &
114wait -n
115echo "status=$?"
116wait -n
117echo "status=$?"
118## STDOUT:
119status=3
120status=9
121## END
122## N-I dash/mksh stdout-json: ""
123
124#### Async for loop
125for i in 1 2 3; do
126 echo $i
127 sleep 0.0$i
128done &
129wait
130## stdout-json: "1\n2\n3\n"
131## status: 0
132
133#### Background process doesn't affect parent
134echo ${foo=1}
135echo $foo
136echo ${bar=2} &
137wait
138echo $bar # bar is NOT SET in the parent process
139## stdout-json: "1\n1\n2\n\n"
140
141#### Background process and then a singleton pipeline
142
143# This was inspired by #416, although that symptom there was timing, so it's
144# technically not a regression test. It's hard to test timing.
145
146{ sleep 0.1; exit 42; } &
147echo begin
148! true
149echo end
150wait $!
151echo status=$?
152## STDOUT:
153begin
154end
155status=42
156## END
157
158#### jobs prints one line per job
159sleep 0.1 &
160sleep 0.1 | cat &
161
162# dash doesn't print if it's not a terminal?
163jobs | wc -l
164
165## STDOUT:
1662
167## END
168## BUG dash STDOUT:
1690
170## END
171
172#### jobs -p prints one line per job
173sleep 0.1 &
174sleep 0.1 | cat &
175
176jobs -p > tmp.txt
177
178cat tmp.txt | wc -l # 2 lines, one for each job
179cat tmp.txt | wc -w # each line is a single "word"
180
181## STDOUT:
1822
1832
184## END