Added test to make sure we get the 3 "completion" events from
[poe:poe-loop-event.git] / poe / t / 90_regression / leolo-poe-wheel-run.t
1 #!/usr/bin/perl
2 # $Id$
3 # vim: filetype=perl
4
5 use warnings;
6 use strict;
7
8 use Test::More tests => 14;
9
10 my $port;
11
12 use POE;
13 use POE::Wheel::Run;
14
15 Worker->spawn( 'pty' );
16 Worker->spawn( 'pty-pipe' );
17 Worker->spawn( 'socketpair' );
18 Worker->spawn( 'inet' );
19
20 pass( "Start" );
21
22 $poe_kernel->run;
23
24 pass( "Done" );
25
26
27 #############################################################################
28 package Worker;
29
30 use strict;
31 use warnings;
32
33 use POE;
34 use Test::More;
35
36 sub DEBUG () { 0 }
37
38 sub seen
39 {
40     my( $heap, $what ) = @_;
41     $heap->{seen}{$what}++;
42     DEBUG and diag "$$: seen $what\n";
43     if( 3==keys %{$heap->{seen}} ) {
44         delete $heap->{wheel};
45         $poe_kernel->alarm_remove( delete $heap->{tid} ) if $heap->{tid};
46     }
47 }
48
49
50 sub spawn 
51 {
52     my( $package, $conduit ) = @_;
53     POE::Session->create(
54         args => [ $conduit ],
55         inline_states => {
56             _start => sub {
57                 my( $heap, $kernel, $conduit ) = @_[ HEAP, KERNEL, ARG0 ];
58                 $heap->{conduit} = $conduit;
59                 $heap->{seen} = {};
60                 $kernel->sig_child( TERM => 'TERM' );
61
62                 $heap->{wheel} = POE::Wheel::Run->new(
63                         StderrEvent => ( $conduit eq 'pty' ? undef() 
64                                                            : 'stderr' ),
65                         StdoutEvent => 'stdout',
66 #                        ErrorEvent  => 'error',
67                         CloseEvent  => 'closeE',
68                         Conduit     => $conduit,
69                         Program     => sub {
70                                 print "hello\n";
71                                 sleep 1;
72                                 print "hello world" x 1024, "\n";
73                                 print "done\n";
74                             }
75                     );
76                 $kernel->sig_child( $heap->{wheel}->PID, 'CHLD' );
77                 $heap->{tid} = $kernel->delay_set( timeout => 600 );
78             },
79             _stop => sub {
80                 my( $heap, $kernel ) = @_[ HEAP, KERNEL ];
81                 ## This is the money shot
82                 foreach my $need ( qw( done close CHLD ) ) {
83                     is( $heap->{seen}{$need}, 1, "$heap->{conduit}: $need" );
84                 }
85             },
86             timeout => sub {
87                 my( $heap, $kernel ) = @_[ HEAP, KERNEL ];
88                 $poe_kernel->alarm_remove( delete $heap->{tid} ) if $heap->{tid};
89                 delete $heap->{wheel};
90                 delete $heap->{tid};
91             },
92             TERM => sub {
93                 my( $heap, $kernel ) = @_[ HEAP, KERNEL ];
94                 $poe_kernel->alarm_remove( delete $heap->{tid} ) if $heap->{tid};
95                 delete $heap->{wheel};
96                 $kernel->sig_handled;
97             },
98             closeE => sub {
99                 my( $heap, $kernel ) = @_[ HEAP, KERNEL ];
100                 seen( $heap, 'close' );
101             },
102             CHLD => sub {
103                 my( $heap, $kernel ) = @_[ HEAP, KERNEL ];
104                 seen( $heap, 'CHLD' );
105                 $kernel->sig_handled;
106             },
107             stdout => sub {
108                 my( $heap, $kernel, $line, $wid ) = @_[ HEAP, KERNEL, ARG0..$#_ ];
109                 seen( $heap, 'done' ) if $line eq 'done';
110             },
111             stderr => sub {
112                 my( $heap, $kernel, $line, $wid ) = @_[ HEAP, KERNEL, ARG0..$#_ ];
113                 warn "ERROR [$$]: $line\n";
114                 seen( $heap, 'error' );
115                 $poe_kernel->alarm_remove( delete $heap->{tid} ) if $heap->{tid};
116             },
117         }
118     );
119 }