« blib/lib/POE/Test/Loops/k_run_returns.pm
↑ Summary
blib/lib/POE/Test/Loops/z_kogman_sig_order.pm »
| 1 | # vim: ts=2 sw=2 expandtab | ||
| 2 | |||
| 3 | # Yuval Kogman's test case for edge issues with rethrowing unhandled | ||
| 4 | # die() exceptions and re-calling run() after it's returned due to | ||
| 5 | # such exceptions. | ||
| 6 | |||
| 7 | use warnings; | ||
| 8 | use strict; | ||
| 9 | |||
| 10 | use Test::More; | ||
| 11 | |||
| 12 | if ($^O eq "MSWin32" and not $ENV{POE_DANTIC}) { | ||
| 13 | plan skip_all => "Perl on $^O is too fragile for this test - it crashes"; | ||
| 14 | CORE::exit(0); | ||
| 15 | |||
| 16 | eval 'use Win32::Console'; | ||
| 17 | if ($@) { | ||
| 18 | plan skip_all => "Win32::Console is required on $^O - try ActivePerl"; | ||
| 19 | } | ||
| 20 | if (exists $INC{'Tk.pm'} and not $ENV{POE_DANTIC}) { | ||
| 21 | plan skip_all => "Perl crashes in this test with Tk on $^O"; | ||
| 22 | } | ||
| 23 | if (exists $INC{'Event.pm'} and not $ENV{POE_DANTIC}) { | ||
| 24 | plan skip_all => "Perl crashes in this test with Event on $^O"; | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | plan tests => 9; | ||
| 29 | |||
| 30 | sub POE::Kernel::ASSERT_DEFAULT () { 1 } | ||
| 31 | use POE qw/Wheel::Run/; | ||
| 32 | |||
| 33 | foreach my $die_on_bad_exit ( 0, 1 ) { | ||
| 34 | foreach my $exit ( 0, 1, 0, 0 ) { | ||
| 35 | POE::Session->create( | ||
| 36 | inline_states => { | ||
| 37 | _start => sub { | ||
| 38 | POE::Session->create( | ||
| 39 | inline_states => { | ||
| 40 | stdout => sub { }, | ||
| 41 | stdin => sub { }, | ||
| 42 | _parent => sub { }, | ||
| 43 | _start => sub { | ||
| 44 | my ( $kernel, $session, $heap ) = @_[KERNEL, SESSION, HEAP]; | ||
| 45 | |||
| 46 | $kernel->sig( CHLD => "sigchld_handler" ); | ||
| 47 | |||
| 48 | my $wheel = POE::Wheel::Run->new( | ||
| 49 | Program => $heap->{program}, | ||
| 50 | StdinEvent => "stdin", | ||
| 51 | StdoutEvent => "stdout", | ||
| 52 | ); | ||
| 53 | |||
| 54 | $heap->{pid_to_wheel}->{ $wheel->PID } = $wheel; | ||
| 55 | $heap->{id_to_wheel}->{ $wheel->ID } = $wheel; | ||
| 56 | |||
| 57 | $kernel->refcount_increment( | ||
| 58 | $session->ID, "running_processes" | ||
| 59 | ); | ||
| 60 | }, | ||
| 61 | sigchld_handler => sub { | ||
| 62 | my ( $kernel, $session, $heap, $pid, $child_error ) = @_[ | ||
| 63 | KERNEL, SESSION, HEAP, ARG1, ARG2 | ||
| 64 | ]; | ||
| 65 | return unless exists $heap->{pid_to_wheel}{$pid}; | ||
| 66 | |||
| 67 | $kernel->refcount_decrement( | ||
| 68 | $session->ID, "running_processes" | ||
| 69 | ); | ||
| 70 | |||
| 71 | my $wheel = delete $heap->{pid_to_wheel}{$pid}; | ||
| 72 | delete $heap->{id_to_wheel}{ $wheel->ID }; | ||
| 73 | $kernel->sig( CHLD => undef ); | ||
| 74 | |||
| 75 | $heap->{program_exit} = $child_error; | ||
| 76 | }, | ||
| 77 | _stop => sub { | ||
| 78 | my ( $heap ) = $_[HEAP]; | ||
| 79 | |||
| 80 | if ( scalar keys %{ $heap->{pid_to_wheel} } ) { | ||
| 81 | die "AAAAAAAHHH Running process leak!"; | ||
| 82 | } | ||
| 83 | |||
| 84 | die "bad exit\n" if $die_on_bad_exit and ( | ||
| 85 | $heap->{program_exit} >> 8 | ||
| 86 | ) != 0; | ||
| 87 | } | ||
| 88 | }, | ||
| 89 | heap => { program => [ $^X, "-wle", "exit $exit" ] }, | ||
| 90 | ); | ||
| 91 | }, | ||
| 92 | _stop => sub { }, | ||
| 93 | _child => sub { }, | ||
| 94 | _parent => sub { }, | ||
| 95 | }, | ||
| 96 | ); | ||
| 97 | |||
| 98 | eval { POE::Kernel->run }; | ||
| 99 | |||
| 100 | if ( $die_on_bad_exit and $exit ) { | ||
| 101 | ok( $@, "($die_on_bad_exit-$exit) died with bad exit code" ); | ||
| 102 | is( $@, "bad exit\n", "($die_on_bad_exit-$exit) error is correct" ); | ||
| 103 | } | ||
| 104 | else { | ||
| 105 | ok( | ||
| 106 | !$@, "($die_on_bad_exit-$exit) no error when process exited OK" | ||
| 107 | ) or diag($@); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | 1; |