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

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 122 CC 4 SCAR 20.2
CC
4
Cov
0%
BEGIN
20.0
POE::Kernel::ASSERT_DEFAULT
2.0
BEGIN
2.0
CRAP 7.5
1 #!/usr/bin/perl -w
2 # vim: ts=2 sw=2 expandtab
3  
4 # Tests various signals using POE's stock signal handlers. These are
5 # plain Perl signals, so mileage may vary.
6  
7 use strict;
8 use lib qw(./mylib ../mylib);
9  
10 use Test::More;
11  
12 sub POE::Kernel::ASSERT_DEFAULT () { 1 }
13  
14 BEGIN {
15 package
16 POE::Kernel;
17 use constant TRACE_DEFAULT => exists($INC{'Devel/Cover.pm'});
18 }
19  
20 # This is the number of processes to fork. Increase this number if
21 # your system can handle the resource use. Also try increasing it if
22 # you suspect a problem with POE's SIGCHLD handling. Be warned
23 # though: setting this too high can cause timing problems and test
24 # failures on some systems.
25  
26 use constant FORK_COUNT => 8;
27  
28 BEGIN {
29 # We can't "plan skip_all" because that calls exit(). And Tk will
30 # croak if you call BEGIN { exit() }. And that croak will cause
31 # this test to FAIL instead of skip.
32  
33 my $error;
34 if ($^O eq "MSWin32" and not $ENV{POE_DANTIC}) {
35 $error = "$^O does not support signals";
36 }
37 elsif ($^O eq "MacOS" and not $ENV{POE_DANTIC}) {
38 $error = "$^O does not support fork";
39 }
40  
41 if ($error) {
42 print "1..0 # Skip $error\n";
43 CORE::exit();
44 }
45  
46 plan tests => FORK_COUNT+ 7;
47 }
48  
49 use IO::Pipely qw(pipely);
50 my ($pipe_read, $pipe_write) = pipely();
51  
52 BEGIN { use_ok("POE") }
53  
54 # Set up a second session that watches for child signals. This is to
55 # test whether a session with only sig_child() stays alive because of
56 # the signals.
57  
58 POE::Session->create(
59 inline_states => {
60 _start => sub { $_[KERNEL]->alias_set("catcher") },
61 catch => sub {
62 my ($kernel, $heap, $pid) = @_[KERNEL, HEAP, ARG0];
63 $kernel->sig(CHLD => "got_sigchld");
64 $kernel->sig_child($pid, "got_chld");
65 $heap->{children}{$pid} = 1;
66 $heap->{watched}++;
67 },
68 remove_alias => sub { $_[KERNEL]->alias_remove("catcher") },
69 got_chld => sub {
70 my ($heap, $pid) = @_[HEAP, ARG1];
71 ok(delete($heap->{children}{$pid}), "caught SIGCHLD for watched pid $pid");
72 $heap->{caught}++;
73 },
74 got_sigchld => sub {
75 $_[HEAP]->{caught_sigchld}++;
76 },
77 _stop => sub {
78 my $heap = $_[HEAP];
79  
80 ok(
81 $heap->{watched} == $heap->{caught},
82 "expected $heap->{watched} reaped children, got $heap->{caught}"
83 );
84  
85 ok(
86 $heap->{watched} == $heap->{caught_sigchld},
87 "expected $heap->{watched} sig(CHLD), got $heap->{caught_sigchld}"
88 );
89  
90 ok(!keys(%{$heap->{children}}), "all reaped children were watched");
91 },
92 },
93 );
94  
95 # Set up a signal catching session. This test uses plain fork(2) and
96 # POE's $SIG{CHLD} handler.
97  
98 POE::Session->create(
99 inline_states => {
100 _start => sub {
101 my ($kernel, $heap) = @_[KERNEL, HEAP];
102  
103 # Clear the status counters, and catch SIGCHLD.
104  
105 $heap->{forked} = $heap->{reaped} = 0;
106  
107 # Fork some child processes, all to exit at the same time.
108  
109 my $fork_start_time = time();
110  
111 for (my $child = 0; $child < FORK_COUNT; $child++) {
112 my $child_pid = fork;
113  
114 if (defined $child_pid) {
115 if ($child_pid) {
116 # Parent side keeps track of child IDs.
117 $heap->{forked}++;
118 $heap->{children}{$child_pid} = 1;
119 $kernel->sig_child($child_pid, "catch_sigchld");
120 $kernel->post(catcher => catch => $child_pid);
121 }
122 else {
123 # A brief sleep so the parent has more opportunity to
124 # finish forking.
125 sleep 1;
126  
127 # Defensively make sure SIGINT will be fatal.
128 $SIG{INT} = 'DEFAULT';
129  
130 # Tell the parent we're ready.
131 print $pipe_write "$$\n";
132  
133 # Wait for SIGINT.
134 sleep 3600;
135 exit;
136 }
137 }
138 else {
139 die "fork error: $!";
140 }
141 }
142  
143 ok(
144 $heap->{forked} == FORK_COUNT,
145 "forked $heap->{forked} processes (out of " . FORK_COUNT . ")"
146 );
147  
148 # NOTE: This is bad form. We're going to block here until all
149 # children check in, or die trying.
150  
151 my $ready_count = 0;
152 while (<$pipe_read>) {
153 last if ++$ready_count >= FORK_COUNT;
154 }
155  
156 $kernel->yield( 'forking_time_is_up' );
157 },
158  
159 _stop => sub {
160 my $heap = $_[HEAP];
161  
162 # Everything is done. See whether it succeeded.
163 ok(
164 $heap->{reaped} == $heap->{forked},
165 "reaped $heap->{reaped} processes (out of $heap->{forked})"
166 );
167 },
168  
169 catch_sigchld => sub {
170 my ($kernel, $heap) = @_[KERNEL, HEAP];
171  
172 # Count the child reap. If that's all of them, wait just a
173 # little longer to make sure there aren't extra ones.
174 if (++$heap->{reaped} >= FORK_COUNT) {
175 $kernel->delay( reaping_time_is_up => 0.500 );
176 }
177 },
178  
179 forking_time_is_up => sub {
180 my ($kernel, $heap) = @_[KERNEL, HEAP];
181  
182 # Forking time is over. We kill all the child processes as
183 # immediately as possible.
184  
185 my $kill_count = kill INT => keys(%{$heap->{children}});
186 ok(
187 $kill_count == $heap->{forked},
188 "killed $kill_count processes (out of $heap->{forked})"
189 );
190  
191 # Start the reap timer. This will tell us how long to wait
192 # between CHLD signals.
193  
194 $heap->{reap_start} = time();
195  
196 # Cap the maximum time for failures.
197  
198 $kernel->delay( reaping_time_is_up => 10 );
199 },
200  
201 # Do nothing here. The timer exists just to keep the session
202 # alive. Once it's dispatched, the session can exit.
203 reaping_time_is_up => sub { undef },
204 },
205 );
206  
207 # Run the tests.
208  
209 POE::Kernel->run();
210  
211 1;