Integrate with mainperl.
[p5sagit/p5-mst-13.2.git] / t / io / openpid.t
1
2 #!./perl
3
4 #####################################################################
5 #
6 # Test for process id return value from open
7 # Ronald Schmidt (The Software Path) RonaldWS@software-path.com
8 #
9 #####################################################################
10
11 BEGIN {
12     chdir 't' if -d 't';
13     unshift @INC, '../lib';
14 }
15
16
17 use FileHandle;
18 autoflush STDOUT 1;
19 $SIG{PIPE} = 'IGNORE';
20
21 print "1..10\n";
22
23 $perl = "$^X -I../lib";
24
25 #
26 # commands run 4 perl programs.  Two of these programs write a
27 # short message to STDOUT and exit.  Two of these programs
28 # read from STDIN.  One reader never exits and must be killed.
29 # the other reader reads one line, waits a few seconds and then
30 # exits to test the waitpid function.
31 #
32 $cmd1 = qq/$perl -e "use FileHandle; autoflush STDOUT 1; / .
33         qq/print qq[first process\\n]; sleep 30;"/;
34 $cmd2 = qq/$perl -e "use FileHandle; autoflush STDOUT 1; / .
35         qq/print qq[second process\\n]; sleep 30;"/;
36 $cmd3 = qq/$perl -e "print <>;"/; # hangs waiting for end of STDIN
37 $cmd4 = qq/$perl -e "print scalar <>;"/;
38
39 #warn "#$cmd1\n#$cmd2\n#$cmd3\n#$cmd4\n";
40
41 # start the processes
42 $pid1 = open(FH1, "$cmd1 |") or print "not ";
43 print "ok 1\n";
44 $pid2 = open(FH2, "$cmd2 |") or print "not ";
45 print "ok 2\n";
46 $pid3 = open(FH3, "| $cmd3") or print "not ";
47 print "ok 3\n";
48 $pid4 = open(FH4, "| $cmd4") or print "not ";
49 print "ok 4\n";
50
51 print "# pids were $pid1, $pid2, $pid3, $pid4\n";
52
53 # get message from first process and kill it
54 chomp($from_pid1 = scalar(<FH1>));
55 print "# child1 returned [$from_pid1]\nnot "
56     unless $from_pid1 eq 'first process';
57 print "ok 5\n";
58 $kill_cnt = kill STOP, $pid1;
59 print "not " unless $kill_cnt == 1;
60 print "ok 6\n";
61
62 # get message from second process and kill second process and reader process
63 chomp($from_pid2 = scalar(<FH2>));
64 print "# child2 returned [$from_pid2]\nnot "
65     unless $from_pid2 eq 'second process';
66 print "ok 7\n";
67 $kill_cnt = kill STOP, $pid2, $pid3;
68 print "not " unless $kill_cnt == 2;
69 print "ok 8\n";
70
71 # send one expected line of text to child process and then wait for it
72 autoflush FH4 1;
73 print FH4 "ok 9\n";
74 print "# waiting for process $pid4 to exit\n";
75 $reap_pid = waitpid $pid4, 0;
76 print "# reaped pid $reap_pid != $pid4\nnot "
77     unless $reap_pid == $pid4;
78 print "ok 10\n";