xref: /freebsd/bin/sh/tests/builtins/read12.0 (revision 8ccc0d235c226d84112561d453c49904398d085c)
1# Verify that `read -t 3 v` succeeds immediately if input is available
2# and times out after 3 s if not
3
4set -e
5
6T=$(mktemp -d ${TMPDIR:-/tmp}/sh-test.XXXXXX)
7trap 'rm -rf "$T"' 0
8cd $T
9mkfifo fifo1
10# Open fifo1 for writing
11{ echo new_value; sleep 10; } >fifo1 &
12# Wait for the child to open fifo1 for writing
13exec 3<fifo1
14
15v=original_value
16r=0
17ts=$(date +%s%3N)
18read -t 3 v <&3 || r=$?
19te=$(date +%s%3N)
20[ "$r" -eq 0 ]
21[ $((te-ts)) -lt 250 ]
22[ "$v" = "new_value" ]
23
24v=original_value
25r=0
26ts=$(date +%s%3N)
27read -t 3 v <&3 || r=$?
28te=$(date +%s%3N)
29kill -TERM "$!" || :
30[ "$r" -gt 128 ] && [ "$(kill -l "$r")" = ALRM ]
31[ $((te-ts)) -gt 3000 ] && [ $((te-ts)) -lt 3250 ]
32[ -z "$v" ]
33