Integrate mainline
[p5sagit/p5-mst-13.2.git] / t / io / openpid.t
CommitLineData
d91d68c1 1#!./perl
2
3#####################################################################
4#
5# Test for process id return value from open
6# Ronald Schmidt (The Software Path) RonaldWS@software-path.com
7#
8#####################################################################
9
10BEGIN {
11 chdir 't' if -d 't';
20822f61 12 @INC = '../lib';
f0060996 13 require './test.pl';
d91d68c1 14}
15
dc459aad 16if ($^O eq 'dos' || $^O eq 'MacOS') {
f0060996 17 skip_all("no multitasking");
18}
19
20plan tests => 10;
21
22
4d61ec05 23use Config;
d27127a1 24$| = 1;
d91d68c1 25$SIG{PIPE} = 'IGNORE';
26
f0060996 27my $perl = which_perl();
28$perl .= qq[ "-I../lib"];
d91d68c1 29
30#
31# commands run 4 perl programs. Two of these programs write a
32# short message to STDOUT and exit. Two of these programs
33# read from STDIN. One reader never exits and must be killed.
34# the other reader reads one line, waits a few seconds and then
35# exits to test the waitpid function.
36#
d27127a1 37$cmd1 = qq/$perl -e "\$|=1; print qq[first process\\n]; sleep 30;"/;
38$cmd2 = qq/$perl -e "\$|=1; print qq[second process\\n]; sleep 30;"/;
d91d68c1 39$cmd3 = qq/$perl -e "print <>;"/; # hangs waiting for end of STDIN
40$cmd4 = qq/$perl -e "print scalar <>;"/;
41
42#warn "#$cmd1\n#$cmd2\n#$cmd3\n#$cmd4\n";
43
44# start the processes
f0060996 45ok( $pid1 = open(FH1, "$cmd1 |"), 'first process started');
46ok( $pid2 = open(FH2, "$cmd2 |"), ' second' );
47ok( $pid3 = open(FH3, "| $cmd3"), ' third' );
48ok( $pid4 = open(FH4, "| $cmd4"), ' fourth' );
d91d68c1 49
50print "# pids were $pid1, $pid2, $pid3, $pid4\n";
51
4d61ec05 52my $killsig = 'HUP';
53$killsig = 1 unless $Config{sig_name} =~ /\bHUP\b/;
54
d91d68c1 55# get message from first process and kill it
56chomp($from_pid1 = scalar(<FH1>));
f0060996 57is( $from_pid1, 'first process', 'message from first process' );
58
4d61ec05 59$kill_cnt = kill $killsig, $pid1;
f0060996 60is( $kill_cnt, 1, 'first process killed' ) ||
61 print "# errno == $!\n";
d91d68c1 62
63# get message from second process and kill second process and reader process
64chomp($from_pid2 = scalar(<FH2>));
f0060996 65is( $from_pid2, 'second process', 'message from second process' );
66
4d61ec05 67$kill_cnt = kill $killsig, $pid2, $pid3;
f0060996 68is( $kill_cnt, 2, 'killing procs 2 & 3' ) ||
69 print "# errno == $!\n";
70
d91d68c1 71
72# send one expected line of text to child process and then wait for it
d27127a1 73select(FH4); $| = 1; select(STDOUT);
74
f0060996 75printf FH4 "ok %d - text sent to fourth process\n", curr_test();
76next_test();
d91d68c1 77print "# waiting for process $pid4 to exit\n";
78$reap_pid = waitpid $pid4, 0;
f0060996 79is( $reap_pid, $pid4, 'fourth process reaped' );
80