blib/lib/POE/Test/Loops/z_rt39872_sigchld.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 132 CC 4 SCAR 11.6
CC
4
Cov
0%
stdout
6.0
error
6.0
sig_CHLD
6.0
CRAP 3.2
1 #!/usr/bin/perl
2 # vim: ts=2 sw=2 expandtab
3  
4 use strict;
5 use warnings;
6  
7 sub POE::Kernel::USE_SIGCHLD () { 1 }
8 sub POE::Kernel::ASSERT_DEFAULT () { 1 }
9  
10 BEGIN {
11 package
12 POE::Kernel;
13 use constant TRACE_DEFAULT => exists($INC{'Devel/Cover.pm'});
14 }
15  
16 use POE;
17 use Test::More;
18 use POE::Wheel::Run;
19 use POSIX qw( SIGINT );
20  
21 if ($^O eq "MSWin32" and not $ENV{POE_DANTIC}) {
22 plan skip_all => "Perl crashes on $^O";
23 exit 0;
24 }
25  
26 if ($INC{'Tk.pm'}) {
27 plan skip_all => "Test causes XIO and other errors under Tk.";
28 exit 0;
29 }
30  
31 plan tests => 6;
32  
33 POE::Session->create(
34 inline_states => {
35 _start => \&_start,
36 _stop => \&_stop,
37 stdout => \&stdout,
38 stderr => \&stderr,
39 sig_CHLD => \&sig_CHLD,
40 error => \&error,
41 done => \&done
42 }
43 );
44  
45 $poe_kernel->run;
46 pass( "Sane exit" );
47  
48 ### End of main code. Beginning of subroutines.
49  
50 sub _start {
51 my( $kernel, $heap ) = @_[KERNEL, HEAP];
52  
53 # This subprocess announces its name and exits when told to.
54  
55 my $prog = <<' PERL';
56 $|++;
57 my $N = shift;
58 print "I am $N\n";
59 while(<STDIN>) {
60 chomp;
61 exit 0 if /^bye/;
62 print "Unknown command '$_'\n";
63 }
64 PERL
65  
66 note "$$ _start";
67  
68 # Linger a bit.
69 $kernel->alias_set( 'worker' );
70  
71 # The W1 test
72  
73 # Start two subprocesses.
74 # They will trigger stdout() when they announce themselves.
75  
76 $heap->{W1} = POE::Wheel::Run->new(
77 Program => [ $^X, '-e', $prog, "W1" ],
78 StdoutEvent => 'stdout',
79 StderrEvent => 'stderr',
80 ErrorEvent => 'error'
81 );
82  
83 $heap->{wheel_id_to_name}{ $heap->{W1}->ID } = 'W1';
84 $heap->{wheel_pid_to_name}{ $heap->{W1}->PID } = 'W1';
85 $kernel->sig_child($heap->{W1}->PID(), 'sig_CHLD');
86  
87 $heap->{W2} = POE::Wheel::Run->new(
88 Program => [ $^X, '-e', $prog, "W2" ],
89 StdoutEvent => 'stdout',
90 StderrEvent => 'stderr',
91 ErrorEvent => 'error'
92 );
93 $heap->{wheel_id_to_name}{ $heap->{W2}->ID } = 'W2';
94 $heap->{wheel_pid_to_name}{ $heap->{W2}->PID } = 'W2';
95 $kernel->sig_child($heap->{W2}->PID(), 'sig_CHLD');
96 }
97  
98 sub _stop {
99 my( $kernel, $heap ) = @_[KERNEL, HEAP];
100 note "$$ _stop";
101 }
102  
103 # The first wheel is done.
104 # Kill the other wheels. We want to be sure only one wheel is done.
105  
106 sub done {
107 my( $kernel, $heap ) = @_[KERNEL, HEAP];
108 note "$$ done";
109  
110 delete $heap->{W1};
111 delete $heap->{W2};
112  
113 my @list = keys %{ $heap->{wheel_pid_to_name} };
114 is( 0+@list, 1, "One wheel left" );
115 kill SIGINT, @list;
116  
117 alarm(5); $SIG{ALRM} = sub { die "test case didn't end sanely" };
118 }
119  
120 # A child process has announced itself.
121 # Test whether we got the right output.
122 # If it's the "W1" test, have it shut down cleanly.
123  
124 sub stdout {
125 my( $kernel, $heap, $input, $id ) = @_[KERNEL, HEAP, ARG0, ARG1];
126  
127 my $N = $heap->{wheel_id_to_name}{$id};
128 note "$$ ($N) ($id) STDOUT: '$input'";
129  
130 # Success if this is an announcement.
131 ok( ($input =~ /I am $N/), "Intro output" );
132  
133 return if $N ne 'W1';
134  
135 my $wheel = $heap->{ $N };
136  
137 # One of the subprocesses will be closed normally.
138 # The other will be killed later.
139  
140 $heap->{closing}{ $N } = 1;
141 $wheel->put( 'bye' );
142 }
143  
144 # Dump the child's STDERR for diagnostics.
145  
146 sub stderr {
147 my( $kernel, $heap, $input, $id ) = @_[KERNEL, HEAP, ARG0, ARG1];
148 my $N = $heap->{wheel_id_to_name}{$id};
149 diag("$$ ($N) ($id) STDERR: '$input'");
150 }
151  
152 # Abnormal errors. Not part of the test, but the test should fail
153 # anyway.
154  
155 sub error {
156 my( $kernel, $heap, $op, $errnum, $errstr, $id, $fh ) = @_[
157 KERNEL, HEAP, ARG0..$#_
158 ];
159  
160 unless ( $op eq 'read' and $errnum==0 ) {
161 my $N = $heap->{wheel_id_to_name}{$id};
162 die("$$ Error $N ($id): $op $errnum ($errstr)");
163 }
164 }
165  
166 # A child process has exited. How's that working out for us?
167  
168 sub sig_CHLD {
169 my( $kernel, $heap, $signal, $pid, $status ) = @_[
170 KERNEL, HEAP, ARG0..$#_
171 ];
172  
173 my $N = delete $heap->{wheel_pid_to_name}{$pid};
174 note "$$ CHLD $N ($pid)";
175  
176 unless ($N eq 'W1') {
177 is( $heap->{closing}{$N}, undef, "$N killed" );
178 return;
179 }
180  
181 is( $heap->{closing}{$N}, 1, "$N closing" );
182  
183 my $wheel = delete $heap->{ $N };
184  
185 delete $heap->{closing}{$N};
186 delete $heap->{wheel_id_to_name}{ $wheel->ID };
187  
188 # A brief delay to make sure all child processes are reaped.
189 $kernel->delay( done => 0.25 );
190 }
191  
192 1;