| 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 | BEGIN { | ||
| 21 | # We can't "plan skip_all" because that calls exit(). And Tk will | ||
| 22 | # croak if you call BEGIN { exit() }. And that croak will cause | ||
| 23 | # this test to FAIL instead of skip. | ||
| 24 | |||
| 25 | my $error; | ||
| 26 | if ($^O eq "MSWin32" and not $ENV{POE_DANTIC}) { | ||
| 27 | $error = "$^O does not support signals"; | ||
| 28 | } | ||
| 29 | elsif ($^O eq "MacOS" and not $ENV{POE_DANTIC}) { | ||
| 30 | $error = "$^O does not support fork"; | ||
| 31 | } | ||
| 32 | |||
| 33 | if ($error) { | ||
| 34 | print "1..0 # Skip $error\n"; | ||
| 35 | CORE::exit(); | ||
| 36 | } | ||
| 37 | |||
| 38 | plan tests => 8; | ||
| 39 | } | ||
| 40 | |||
| 41 | BEGIN { use_ok("POE") } | ||
| 42 | |||
| 43 | # This is the number of processes to fork. Increase this number if | ||
| 44 | # your system can handle the resource use. Also try increasing it if | ||
| 45 | # you suspect a problem with POE's SIGCHLD handling. Be warned | ||
| 46 | # though: setting this too high can cause timing problems and test | ||
| 47 | # failures on some systems. | ||
| 48 | |||
| 49 | my $fork_count = 8; | ||
| 50 | |||
| 51 | use IO::Pipely qw(pipely); | ||
| 52 | my ($pipe_read, $pipe_write) = pipely(); | ||
| 53 | |||
| 54 | # Set up a signal catching session. This test uses plain fork(2) and | ||
| 55 | # POE's $SIG{CHLD} handler. | ||
| 56 | |||
| 57 | POE::Session->create( | ||
| 58 | inline_states => { | ||
| 59 | _start => sub { | ||
| 60 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 61 | |||
| 62 | # Clear the status counters, and catch SIGCHLD. | ||
| 63 | |||
| 64 | $heap->{forked} = $heap->{reaped} = 0; | ||
| 65 | $kernel->sig( CHLD => 'catch_sigchld' ); | ||
| 66 | |||
| 67 | # Fork some child processes, all to exit at the same time. | ||
| 68 | |||
| 69 | my $fork_start_time = time(); | ||
| 70 | |||
| 71 | for (my $child = 0; $child < $fork_count; $child++) { | ||
| 72 | my $child_pid = fork; | ||
| 73 | |||
| 74 | if (defined $child_pid) { | ||
| 75 | if ($child_pid) { | ||
| 76 | # Parent side keeps track of child IDs. | ||
| 77 | $heap->{forked}++; | ||
| 78 | $heap->{children}->{$child_pid} = 1; | ||
| 79 | } | ||
| 80 | else { | ||
| 81 | # A brief sleep so the parent has more opportunity to | ||
| 82 | # finish forking. | ||
| 83 | sleep 1; | ||
| 84 | |||
| 85 | # Defensively make sure SIGINT will be fatal. | ||
| 86 | $SIG{INT} = 'DEFAULT'; | ||
| 87 | |||
| 88 | # Tell the parent we're ready. | ||
| 89 | print $pipe_write "$$\n"; | ||
| 90 | |||
| 91 | # Wait for SIGINT. | ||
| 92 | sleep 3600; | ||
| 93 | exit; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | else { | ||
| 97 | die "fork error: $!"; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | ok( | ||
| 102 | $heap->{forked} == $fork_count, | ||
| 103 | "forked $heap->{forked} processes (out of $fork_count)" | ||
| 104 | ); | ||
| 105 | |||
| 106 | # NOTE: This is bad form. We're going to block here until all | ||
| 107 | # children check in, or die trying. | ||
| 108 | |||
| 109 | my $ready_count = 0; | ||
| 110 | while (<$pipe_read>) { | ||
| 111 | last if ++$ready_count >= $fork_count; | ||
| 112 | } | ||
| 113 | |||
| 114 | $kernel->yield( 'forking_time_is_up' ); | ||
| 115 | }, | ||
| 116 | |||
| 117 | _stop => sub { | ||
| 118 | my $heap = $_[HEAP]; | ||
| 119 | |||
| 120 | # Everything is done. See whether it succeeded. | ||
| 121 | ok( | ||
| 122 | $heap->{reaped} == $heap->{forked}, | ||
| 123 | "reaped $heap->{reaped} processes (out of $heap->{forked})" | ||
| 124 | ); | ||
| 125 | }, | ||
| 126 | |||
| 127 | catch_sigchld => sub { | ||
| 128 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 129 | |||
| 130 | # Count the child reap. If that's all of them, wait just a | ||
| 131 | # little longer to make sure there aren't extra ones. | ||
| 132 | if (++$heap->{reaped} >= $fork_count) { | ||
| 133 | $kernel->delay( reaping_time_is_up => 0.500 ); | ||
| 134 | } | ||
| 135 | }, | ||
| 136 | |||
| 137 | forking_time_is_up => sub { | ||
| 138 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 139 | |||
| 140 | # Forking time is over. We kill all the child processes as | ||
| 141 | # immediately as possible. | ||
| 142 | |||
| 143 | my $kill_count = kill INT => keys(%{$heap->{children}}); | ||
| 144 | ok( | ||
| 145 | $kill_count == $heap->{forked}, | ||
| 146 | "killed $kill_count processes (out of $heap->{forked})" | ||
| 147 | ); | ||
| 148 | |||
| 149 | # Start the reap timer. This will tell us how long to wait | ||
| 150 | # between CHLD signals. | ||
| 151 | |||
| 152 | $heap->{reap_start} = time(); | ||
| 153 | |||
| 154 | # Cap the maximum time for failures. | ||
| 155 | $kernel->delay( reaping_time_is_up => 10 ); | ||
| 156 | }, | ||
| 157 | |||
| 158 | # Do nothing here. The timer exists just to keep the session | ||
| 159 | # alive. Once it's dispatched, the session can exit. | ||
| 160 | reaping_time_is_up => sub { | ||
| 161 | $_[KERNEL]->sig( CHLD => undef ); | ||
| 162 | }, | ||
| 163 | }, | ||
| 164 | ); | ||
| 165 | |||
| 166 | # mstevens found a subtle incompatibility between nested sessions and | ||
| 167 | # SIGIDLE. This should be fun to debug, but first I'll add the test | ||
| 168 | # case here. | ||
| 169 | |||
| 170 | sub spawn_server { | ||
| 171 | POE::Session->create( | ||
| 172 | inline_states => { | ||
| 173 | _start => sub { | ||
| 174 | $_[KERNEL]->alias_set("server"); | ||
| 175 | }, | ||
| 176 | do_thing => sub { | ||
| 177 | $_[KERNEL]->post($_[SENDER], thing_done => $_[ARG0]); | ||
| 178 | }, | ||
| 179 | _child => sub { 0 }, | ||
| 180 | _stop => sub { 0 }, | ||
| 181 | }, | ||
| 182 | ); | ||
| 183 | } | ||
| 184 | |||
| 185 | POE::Session->create( | ||
| 186 | inline_states => { | ||
| 187 | _start => sub { | ||
| 188 | spawn_server(); | ||
| 189 | $_[KERNEL]->post(server => do_thing => 1); | ||
| 190 | }, | ||
| 191 | thing_done => sub { 0 }, | ||
| 192 | _child => sub { 0 }, | ||
| 193 | _stop => sub { 0 }, | ||
| 194 | }, | ||
| 195 | ); | ||
| 196 | |||
| 197 | # See how SIGPIPE gets handled. | ||
| 198 | |||
| 199 | POE::Session->create( | ||
| 200 | inline_states => { | ||
| 201 | _start => sub { | ||
| 202 | $_[KERNEL]->sig(USR1 => "got_usr1"); | ||
| 203 | $_[KERNEL]->sig(PIPE => "got_pipe"); | ||
| 204 | $_[KERNEL]->yield("send_signals"); | ||
| 205 | }, | ||
| 206 | send_signals => sub { | ||
| 207 | ok(kill("USR1", $$) == 1, "sent self SIGUSR1"); | ||
| 208 | ok(kill("PIPE", $$) == 1, "sent self SIGPIPE"); | ||
| 209 | $_[KERNEL]->delay(signal_wait_timeout => 1); | ||
| 210 | }, | ||
| 211 | got_usr1 => sub { | ||
| 212 | $_[HEAP]->{usr1}++; | ||
| 213 | _reduce_signal_wait_timeout($_[HEAP]); | ||
| 214 | }, | ||
| 215 | got_pipe => sub { | ||
| 216 | $_[HEAP]->{pipe}++; | ||
| 217 | _reduce_signal_wait_timeout($_[HEAP]); | ||
| 218 | }, | ||
| 219 | signal_wait_timeout => sub { | ||
| 220 | $_[KERNEL]->sig( USR1 => undef ); | ||
| 221 | $_[KERNEL]->sig( PIPE => undef ); | ||
| 222 | }, | ||
| 223 | _stop => sub { | ||
| 224 | ok($_[HEAP]->{usr1} == 1, "caught SIGUSR1"); | ||
| 225 | ok($_[HEAP]->{pipe} == 1, "caught SIGPIPE"); | ||
| 226 | }, | ||
| 227 | }, | ||
| 228 | ); | ||
| 229 | |||
| 230 | sub _reduce_signal_wait_timeout { | ||
| 231 | my ($heap) = @_; | ||
| 232 | if ($heap->{usr1} and $heap->{pipe}) { | ||
| 233 | POE::Kernel->delay(signal_wait_timeout => 0.100); | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | # Run the tests. | ||
| 238 | |||
| 239 | POE::Kernel->run(); | ||
| 240 | |||
| 241 | 1; |