• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3[ -f testing.sh ] && . testing.sh
4
5#testing "name" "command" "result" "infile" "stdin"
6
7# timeout's exit value is complicated!
8testcmd "times out" '.1 sleep 100 ; echo $?'  '124\n' '' ''
9testcmd "failure" '-s MONKEY .1 sleep 100 2>/dev/null ; echo $?' '125\n' '' ''
10testcmd "early failure" '2>/dev/null ; echo $?' '125\n' '' ''
11testcmd "can't execute" '.1 / 2>/dev/null ; echo $?' '126\n' '' ''
12testcmd "can't find" '.1 /does/not/exist 2>/dev/null ; echo $?' '127\n' '' ''
13testcmd "custom signal" '-s 3 .1 sleep 100; echo $?' '124\n' '' ''
14testcmd "killed" '-s 9 .1 sleep 100; echo $?' '137\n' '' ''
15testcmd "TERM" '-s TERM .1 sleep 100; echo $?' '124\n' '' ''
16testcmd "exit 0" '.1 true ; echo $?' '0\n' '' ''
17testcmd "exit 1" '.1 false ; echo $?' '1\n' '' ''
18
19testcmd "--preserve-status" '--preserve-status .1 sleep 100 ; echo $?' '143\n' '' ''
20testcmd "--preserve-status killed" '--preserve-status -s 9 .1 sleep 100 ; echo $?' '137\n' '' ''
21
22# There's another special case where if the subprocess catches our timeout
23# signal and exits, we need to report that as a timeout (unless overridden).
24cat > loop.sh <<EOF
25#!/bin/sh
26trap "exit 3" TERM
27while true; do
28  :
29done
30EOF
31chmod a+x loop.sh
32testcmd "trap-and-exit" '1 ./loop.sh ; echo $?' '124\n' '' ''
33testcmd "trap-and-exit --preserve-status" \
34  '--preserve-status 1 ./loop.sh ; echo $?' '3\n' '' ''
35rm loop.sh
36
37toyonly testcmd "-i" \
38  "-i 1 sh -c 'for i in .25 .50 2; do sleep \$i; echo hello; done'" \
39  "hello\nhello\n" "" ""
40testing '-v' "{ timeout -v .1 sleep 3;} 2>&1 | egrep -o 'TERM|sleep'" \
41  'TERM\nsleep\n' '' ''
42
43