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