| 1 | #!/usr/bin/perl | ||
| 2 | # vim: ts=2 sw=2 expandtab | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | my $REFCNT; | ||
| 8 | |||
| 9 | sub POE::Kernel::USE_SIGCHLD () { 1 } | ||
| 10 | sub POE::Kernel::ASSERT_DEFAULT () { 1 } | ||
| 11 | |||
| 12 | BEGIN { | ||
| 13 | package | ||
| 14 | POE::Kernel; | ||
| 15 | use constant TRACE_DEFAULT => exists($INC{'Devel/Cover.pm'}); | ||
| 16 | } | ||
| 17 | |||
| 18 | use Test::More; | ||
| 19 | use POSIX qw( SIGINT SIGUSR1 ); | ||
| 20 | use POE; | ||
| 21 | use POE::Wheel::Run; | ||
| 22 | |||
| 23 | if ($^O eq "MSWin32" and not $ENV{POE_DANTIC}) { | ||
| 24 | plan skip_all => "SIGUSR1 not supported on $^O"; | ||
| 25 | exit 0; | ||
| 26 | } | ||
| 27 | |||
| 28 | if ($INC{'Tk.pm'} and not $ENV{POE_DANTIC}) { | ||
| 29 | plan skip_all => "Test causes XIO and other errors under Tk."; | ||
| 30 | exit 0; | ||
| 31 | } | ||
| 32 | |||
| 33 | $SIG{__WARN__} = sub { | ||
| 34 | print STDERR "$$: $_[0]"; | ||
| 35 | }; | ||
| 36 | |||
| 37 | plan tests => 4; | ||
| 38 | |||
| 39 | our $PARENT = 1; | ||
| 40 | |||
| 41 | POE::Session->create( | ||
| 42 | inline_states => { | ||
| 43 | _start => \&_start, | ||
| 44 | _stop => \&_stop, | ||
| 45 | |||
| 46 | work => \&work, | ||
| 47 | child => \&child, | ||
| 48 | parent => \&parent, | ||
| 49 | T1 => \&T1, | ||
| 50 | T2 => \&T2, | ||
| 51 | |||
| 52 | sig_CHLD => \&sig_CHLD, | ||
| 53 | sig_USR1 => \&sig_USR1, | ||
| 54 | done => \&done | ||
| 55 | } | ||
| 56 | ); | ||
| 57 | |||
| 58 | note "Parent"; | ||
| 59 | $poe_kernel->run; | ||
| 60 | pass( "Sane exit" ) if $PARENT; | ||
| 61 | note "Exit"; | ||
| 62 | |||
| 63 | ### End of main code. Beginning of subroutines. | ||
| 64 | |||
| 65 | sub _start { | ||
| 66 | my( $kernel, $heap ) = @_[KERNEL, HEAP]; | ||
| 67 | |||
| 68 | note "_start"; | ||
| 69 | |||
| 70 | $kernel->yield( 'work' ); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub work { | ||
| 74 | my( $kernel, $heap, $session ) = @_[KERNEL, HEAP, SESSION]; | ||
| 75 | |||
| 76 | foreach my $name ( qw( T1 T2 ) ) { | ||
| 77 | my $pid = fork(); | ||
| 78 | die "Unable to fork: $!" unless defined $pid; | ||
| 79 | |||
| 80 | if( $pid ) { # parent | ||
| 81 | $heap->{$name}{PID} = $pid; | ||
| 82 | $heap->{pid_to_name}{ $pid } = $name; | ||
| 83 | $kernel->sig_child($pid, 'sig_CHLD'); | ||
| 84 | } | ||
| 85 | else { | ||
| 86 | $kernel->can('has_forked') and $kernel->has_forked(); | ||
| 87 | $kernel->yield( 'child' ); | ||
| 88 | return; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | foreach my $name ( qw( T1 T2 ) ) { | ||
| 93 | $kernel->refcount_increment( $session->ID, $name ); | ||
| 94 | } | ||
| 95 | |||
| 96 | $kernel->delay_add( 'parent', 3 ); | ||
| 97 | diag( "Parent $$ waiting 3sec for slow systems to settle." ); | ||
| 98 | |||
| 99 | return; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub parent | ||
| 103 | { | ||
| 104 | my( $kernel, $heap, $session ) = @_[KERNEL, HEAP, SESSION]; | ||
| 105 | note "parent"; | ||
| 106 | diag( "sending sigusr1" ); | ||
| 107 | kill SIGUSR1, $heap->{T1}{PID}; | ||
| 108 | diag( "sent sigusr1" ); | ||
| 109 | $heap->{T1}{closing} = 1; | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | sub child | ||
| 114 | { | ||
| 115 | my( $kernel, $heap, $session ) = @_[KERNEL, HEAP, SESSION]; | ||
| 116 | $PARENT = 0; | ||
| 117 | note "child"; | ||
| 118 | $kernel->sig( 'CHLD' ); | ||
| 119 | $kernel->sig( USR1 => 'sig_USR1' ); | ||
| 120 | $kernel->refcount_increment( $session->ID, 'USR1' ); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub sig_USR1 | ||
| 124 | { | ||
| 125 | my( $kernel, $heap, $session ) = @_[KERNEL, HEAP, SESSION]; | ||
| 126 | note "USR1"; | ||
| 127 | $REFCNT = 1; | ||
| 128 | $kernel->sig( 'USR1' ); | ||
| 129 | $kernel->refcount_decrement( $session->ID, 'USR1' ); | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | sub _stop { | ||
| 134 | my( $kernel, $heap ) = @_[KERNEL, HEAP]; | ||
| 135 | note "_stop"; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub done { | ||
| 139 | my( $kernel, $heap, $session ) = @_[KERNEL, HEAP, SESSION]; | ||
| 140 | note "done"; | ||
| 141 | |||
| 142 | my @list = keys %{ $heap->{pid_to_name} }; | ||
| 143 | is( 0+@list, 1, "One child left ($list[0])" ); | ||
| 144 | kill SIGUSR1, @list; | ||
| 145 | |||
| 146 | $REFCNT = 1; | ||
| 147 | $kernel->refcount_decrement( $session->ID, 'T2' ); | ||
| 148 | alarm(30); $SIG{ALRM} = sub { die "test case didn't end sanely" }; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub sig_CHLD { | ||
| 152 | my( $kernel, $heap, $signal, $pid, $status ) = @_[ | ||
| 153 | KERNEL, HEAP, ARG0..$#_ | ||
| 154 | ]; | ||
| 155 | |||
| 156 | unless( $heap->{pid_to_name}{$pid} ) { | ||
| 157 | return; | ||
| 158 | } | ||
| 159 | |||
| 160 | my $name = $heap->{pid_to_name}{$pid}; | ||
| 161 | my $D = delete $heap->{$name}; | ||
| 162 | |||
| 163 | if ($name ne 'T1') { | ||
| 164 | is( $D->{closing}, undef, "Expected child exited" ); | ||
| 165 | return; | ||
| 166 | } | ||
| 167 | |||
| 168 | is( $D->{closing}, 1, "Expected child exited" ); | ||
| 169 | $kernel->refcount_decrement( $_[SESSION]->ID, $name ); | ||
| 170 | delete $heap->{$name}; | ||
| 171 | delete $heap->{pid_to_name}{$pid}; | ||
| 172 | |||
| 173 | $kernel->yield( 'done' ); | ||
| 174 | } | ||
| 175 | |||
| 176 | 1; |