coprocess.sh revision 7c2fbfb345896881c631598ee3852ce9ce33fb07
########################################################################
# #
# This software is part of the ast package #
# Copyright (c) 1982-2008 AT&T Intellectual Property #
# and is licensed under the #
# Common Public License, Version 1.0 #
# by AT&T Intellectual Property #
# #
# A copy of the License is available at #
# (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) #
# #
# Information and Software Systems Research #
# AT&T Research #
# Florham Park NJ #
# #
# David Korn <dgk@research.att.com> #
# #
########################################################################
# test the behavior of co-processes
function err_exit
{
print -u2 -n "\t"
let Errors+=1
}
alias err_exit='err_exit $LINENO'
integer Errors=0
if [[ -d /cygdrive ]]
then err_exit cygwin detected - coprocess tests disabled - enable at the risk of wedging your system
fi
function ping # id
{
integer x=0
while ((x++ < 5))
do read -r
print -r "$1 $REPLY"
done
}
cat |&
print -p "hello"
read -p line
exec 5>&p 6<&p
exec 5<&- 6<&-
wait $!
exec 3>&p
exec 4>&p
integer count
do case $i in
esac
done
read -p
set -- $REPLY
if [[ $1 != $2 ]]
then err_exit "$1 does not match $2"
fi
case $1 in
three) ;;
four) ;;
pipe) ;;
esac
done
!
sleep 10 |&
exec 5<&p 6>&p
exec 5<&- 6>&-
print -p $'print hello | cat\nprint Done'
read -t 5 -p
read -t 5 -p
then err_exit "${SHELL-ksh} coprocess not working"
fi
exec 5<&p 6>&p
exec 5<&- 6>&-
wait $!
{
} |&
while read -p -t 10 line
done
fi
wait $!
err_exit "coprocess cleanup not working correctly"
err_exit "subshell coprocess lingers in parent"
wait $!
unset N r e
integer N=5
e=12345
(
integer i
for ((i = 1; i <= N; i++))
do print $i |&
read -p r
print -n $r
wait $!
done
print
[[ $r == $e ]] || err_exit "coprocess timing bug -- expected $e, got '$r'"
r=
(
integer i
for ((i = 1; i <= N; i++))
do print $i |&
sleep 0.01
r=$r$(cat <&p)
wait $!
done
print $r
[[ $r == $e ]] || err_exit "coprocess command substitution bug -- expected $e, got '$r'"
(
sleep 0.01
exec 6>&p
exec 6>&-
sleep 1
cat |&
pid=$!
(
sleep 2
kill -$sig $$
sleep 2
kill -$sig $$
kill $pid
sleep 2
kill $$
) &
read -p
++EOF++
then err_exit 'traps when reading from coprocess not working'
fi
break
fi
done
{ sleep 5; kill $$; } &
sleep_pid=$!
builtin cat
cat |&
pid=$!
exec 5<&p 6>&p
exec 6>&-
wait $pid
trap - TERM
[[ $sleep_pid ]] && kill $sleep_pid
{ sleep 5; kill $$; } &
sleep_pid=$!
cat |&
pid=$!
kill $pid
trap - TERM
[[ $sleep_pid ]] && kill $sleep_pid
{ sleep 5; kill $$; } &
sleep_pid=$!
cat |&
pid=$!
print -p foo
print -p bar
read <&p || err_exit 'first read from coprocess failed'
read <&p || err_exit 'second read from coprocess failed'
kill $pid
trap - TERM
[[ $sleep_pid ]] && kill $sleep_pid