| 1 | #!/usr/bin/perl | ||
| 2 | # vim: ts=2 sw=2 expandtab | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | |||
| 7 | use Test::More; | ||
| 8 | |||
| 9 | if ($^O eq "MSWin32" and not $ENV{POE_DANTIC}) { | ||
| 10 | plan skip_all => "Perl crashes on $^O"; | ||
| 11 | } | ||
| 12 | |||
| 13 | plan tests => 14; | ||
| 14 | |||
| 15 | my $port; | ||
| 16 | |||
| 17 | use POE; | ||
| 18 | use POE::Wheel::Run; | ||
| 19 | |||
| 20 | SKIP: { | ||
| 21 | skip("The underlying event loop has trouble with ptys on $^O", 6) | ||
| 22 | if $^O eq "darwin" and ( | ||
| 23 | exists $INC{"POE/Loop/IO_Poll.pm"} or | ||
| 24 | exists $INC{"POE/Loop/Event.pm"} or | ||
| 25 | $ENV{POE_LOOP_USES_POLL} | ||
| 26 | ) and not $ENV{POE_DANTIC}; | ||
| 27 | |||
| 28 | eval "use IO::Pty"; | ||
| 29 | skip("IO::Pty is not available; skipping pty tests", 6) if $@; | ||
| 30 | |||
| 31 | PoeTestWorker->spawn( 'pty' ); | ||
| 32 | PoeTestWorker->spawn( 'pty-pipe' ); | ||
| 33 | } | ||
| 34 | |||
| 35 | PoeTestWorker->spawn( 'socketpair' ); | ||
| 36 | PoeTestWorker->spawn( 'inet' ); | ||
| 37 | |||
| 38 | pass( "Start" ); | ||
| 39 | |||
| 40 | $poe_kernel->run; | ||
| 41 | |||
| 42 | pass( "Done" ); | ||
| 43 | |||
| 44 | |||
| 45 | ############################################################################# | ||
| 46 | package | ||
| 47 | PoeTestWorker; | ||
| 48 | |||
| 49 | use strict; | ||
| 50 | use warnings; | ||
| 51 | |||
| 52 | use POE; | ||
| 53 | use Test::More; | ||
| 54 | |||
| 55 | sub DEBUG () { 0 } | ||
| 56 | |||
| 57 | sub seen | ||
| 58 | { | ||
| 59 | my( $heap, $what ) = @_; | ||
| 60 | $heap->{seen}{$what}++; | ||
| 61 | DEBUG and diag "$$: seen $what\n"; | ||
| 62 | if( 3==keys %{$heap->{seen}} ) { | ||
| 63 | delete $heap->{wheel}; | ||
| 64 | $poe_kernel->alarm_remove( delete $heap->{tid} ) if $heap->{tid}; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | sub spawn | ||
| 70 | { | ||
| 71 | my( $package, $conduit ) = @_; | ||
| 72 | POE::Session->create( | ||
| 73 | args => [ $conduit ], | ||
| 74 | inline_states => { | ||
| 75 | _start => sub { | ||
| 76 | my( $heap, $kernel, $conduit ) = @_[ HEAP, KERNEL, ARG0 ]; | ||
| 77 | $heap->{conduit} = $conduit; | ||
| 78 | $heap->{seen} = {}; | ||
| 79 | $kernel->sig_child( TERM => 'TERM' ); | ||
| 80 | |||
| 81 | $heap->{wheel} = POE::Wheel::Run->new( | ||
| 82 | StderrEvent => ( | ||
| 83 | $conduit eq 'pty' ? undef() : 'stderr' | ||
| 84 | ), | ||
| 85 | StdoutEvent => 'stdout', | ||
| 86 | #ErrorEvent => 'error', | ||
| 87 | CloseEvent => 'closeE', | ||
| 88 | Conduit => $conduit, | ||
| 89 | Program => sub { | ||
| 90 | print "hello\n"; | ||
| 91 | sleep 1; | ||
| 92 | print "hello world" x 1024, "\n"; | ||
| 93 | print "done\n"; | ||
| 94 | } | ||
| 95 | ); | ||
| 96 | $kernel->sig_child( $heap->{wheel}->PID, 'CHLD' ); | ||
| 97 | $heap->{tid} = $kernel->delay_set( timeout => 600 ); | ||
| 98 | }, | ||
| 99 | _stop => sub { | ||
| 100 | my( $heap, $kernel ) = @_[ HEAP, KERNEL ]; | ||
| 101 | ## This is the money shot | ||
| 102 | foreach my $need ( qw( done close CHLD ) ) { | ||
| 103 | is( $heap->{seen}{$need}, 1, "$heap->{conduit}: $need" ); | ||
| 104 | } | ||
| 105 | }, | ||
| 106 | timeout => sub { | ||
| 107 | my( $heap, $kernel ) = @_[ HEAP, KERNEL ]; | ||
| 108 | $poe_kernel->alarm_remove( delete $heap->{tid} ) if $heap->{tid}; | ||
| 109 | delete $heap->{wheel}; | ||
| 110 | delete $heap->{tid}; | ||
| 111 | }, | ||
| 112 | TERM => sub { | ||
| 113 | my( $heap, $kernel ) = @_[ HEAP, KERNEL ]; | ||
| 114 | $poe_kernel->alarm_remove( delete $heap->{tid} ) if $heap->{tid}; | ||
| 115 | delete $heap->{wheel}; | ||
| 116 | $kernel->sig_handled; | ||
| 117 | }, | ||
| 118 | closeE => sub { | ||
| 119 | my( $heap, $kernel ) = @_[ HEAP, KERNEL ]; | ||
| 120 | seen( $heap, 'close' ); | ||
| 121 | }, | ||
| 122 | CHLD => sub { | ||
| 123 | my( $heap, $kernel ) = @_[ HEAP, KERNEL ]; | ||
| 124 | seen( $heap, 'CHLD' ); | ||
| 125 | $kernel->sig_handled; | ||
| 126 | }, | ||
| 127 | stdout => sub { | ||
| 128 | my( $heap, $kernel, $line, $wid ) = @_[ HEAP, KERNEL, ARG0..$#_ ]; | ||
| 129 | seen( $heap, 'done' ) if $line eq 'done'; | ||
| 130 | }, | ||
| 131 | stderr => sub { | ||
| 132 | my( $heap, $kernel, $line, $wid ) = @_[ HEAP, KERNEL, ARG0..$#_ ]; | ||
| 133 | warn "ERROR [$$]: $line\n"; | ||
| 134 | seen( $heap, 'error' ); | ||
| 135 | $poe_kernel->alarm_remove( delete $heap->{tid} ) if $heap->{tid}; | ||
| 136 | }, | ||
| 137 | } | ||
| 138 | ); | ||
| 139 | } | ||
| 140 | |||
| 141 | 1; |