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