« blib/lib/POE/Test/Loops/comp_tcp_concurrent.pm
↑ Summary
blib/lib/POE/Test/Loops/wheel_sf_ipv6.pm »
| 1 | #!/usr/bin/perl -w | ||
| 2 | # vim: ts=2 sw=2 expandtab | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use lib qw(./mylib ../mylib); | ||
| 6 | use Socket; | ||
| 7 | |||
| 8 | use Test::More; | ||
| 9 | |||
| 10 | # Skip these tests if fork() is unavailable. | ||
| 11 | # We can't test_setup(0, "reason") because that calls exit(). And Tk | ||
| 12 | # will croak if you call BEGIN { exit() }. And that croak will cause | ||
| 13 | # this test to FAIL instead of skip. | ||
| 14 | our $RUNNING_WIN32; | ||
| 15 | BEGIN { | ||
| 16 | my $error; | ||
| 17 | if ($^O eq "MacOS") { | ||
| 18 | $error = "$^O does not support fork"; | ||
| 19 | } | ||
| 20 | |||
| 21 | if ($^O eq "MSWin32") { | ||
| 22 | eval 'use Win32::Console'; | ||
| 23 | if ($@) { | ||
| 24 | $error = "Win32::Console is required on $^O - try ActivePerl"; | ||
| 25 | } | ||
| 26 | elsif (exists $INC{"Tk.pm"} and not $ENV{POE_DANTIC}) { | ||
| 27 | $error = "$^O with Tk seems to hang on this test"; | ||
| 28 | } | ||
| 29 | elsif (exists $INC{"Event.pm"} and not $ENV{POE_DANTIC}) { | ||
| 30 | $error = "$^O\'s fork() emulation breaks Event"; | ||
| 31 | } | ||
| 32 | elsif (not $ENV{POE_DANTIC}) { | ||
| 33 | $error = "Signal handling on $^O is too fragile - Perl crashes"; | ||
| 34 | } | ||
| 35 | |||
| 36 | $RUNNING_WIN32 = 1; | ||
| 37 | } | ||
| 38 | |||
| 39 | if ($error) { | ||
| 40 | plan skip_all => $error; | ||
| 41 | CORE::exit(); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub STD_TEST_COUNT () { 8 } | ||
| 45 | |||
| 46 | plan tests => | ||
| 47 | 4 + | ||
| 48 | 15 + | ||
| 49 | 8 + | ||
| 50 | 8 + | ||
| 51 | 4 + #Silent/Open FD tests | ||
| 52 | 8 * STD_TEST_COUNT; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub POE::Kernel::ASSERT_DEFAULT () { 1 } | ||
| 56 | |||
| 57 | BEGIN { | ||
| 58 | package | ||
| 59 | POE::Kernel; | ||
| 60 | use constant TRACE_DEFAULT => exists($INC{'Devel/Cover.pm'}); | ||
| 61 | } | ||
| 62 | |||
| 63 | use POE qw(Wheel::Run Filter::Line); | ||
| 64 | |||
| 65 | # the child program comes in two varieties: {{{ | ||
| 66 | # - a string suitable for running with system() | ||
| 67 | # - a coderef | ||
| 68 | |||
| 69 | my ($chld_program_string, $chld_program_coderef); | ||
| 70 | { | ||
| 71 | my $text = <<'END'; | ||
| 72 | my $out = shift; | ||
| 73 | my $err = shift; | ||
| 74 | local $/ = q(!); | ||
| 75 | local $\ = q(!); | ||
| 76 | my $notify_eof_flag = 0; | ||
| 77 | select STDERR; $| = 1; select STDOUT; $| = 1; | ||
| 78 | my $eof_counter = 0; | ||
| 79 | OUTER: while (1) { | ||
| 80 | $eof_counter++; | ||
| 81 | CORE::exit if $eof_counter > 10; | ||
| 82 | while (<STDIN>) { | ||
| 83 | chomp; | ||
| 84 | $eof_counter = 0; | ||
| 85 | last OUTER if /^bye/; | ||
| 86 | $notify_eof_flag = 1 if s/^notify eof/out/; | ||
| 87 | print(STDOUT qq($out: $$)) if /^pid/; | ||
| 88 | print(STDOUT qq($out: $_)) if s/^out //; | ||
| 89 | print(STDERR qq($err: $_)) if s/^err //; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | if ($notify_eof_flag) { | ||
| 93 | print(STDOUT qq($out: got eof)); | ||
| 94 | sleep 10; | ||
| 95 | } | ||
| 96 | END | ||
| 97 | $text =~ s/\s+/ /g; | ||
| 98 | |||
| 99 | my $os_quote = ($^O eq 'MSWin32') ? q(") : q('); | ||
| 100 | $chld_program_string = [ $^X, "-we", "$text CORE::exit 0" ]; | ||
| 101 | $chld_program_coderef = eval "sub { \$! = 1; " . $text . " }"; | ||
| 102 | die $@ if $@; | ||
| 103 | } | ||
| 104 | |||
| 105 | my $shutdown_program = sub { | ||
| 106 | my $out = shift; | ||
| 107 | my $err = shift; | ||
| 108 | select STDERR; $| = 1; select STDOUT; $| = 1; | ||
| 109 | local $/ = q(!); | ||
| 110 | local $\ = q(!); | ||
| 111 | my $flag = 0; | ||
| 112 | $SIG{ALRM} = sub { die "alarm\n" }; | ||
| 113 | eval { | ||
| 114 | alarm(30); | ||
| 115 | while (<STDIN>) { | ||
| 116 | chomp; | ||
| 117 | if (/flag (\d+)/) { $flag = $1 } | ||
| 118 | elsif (/out (\S+)/) { print STDOUT "$out: $1" } | ||
| 119 | } | ||
| 120 | }; | ||
| 121 | alarm(0); | ||
| 122 | if ($@ eq "alarm\n") { | ||
| 123 | print STDOUT "$out: got alarm"; | ||
| 124 | } | ||
| 125 | else { | ||
| 126 | print STDOUT "$out: got eof $flag"; | ||
| 127 | } | ||
| 128 | sleep 1; | ||
| 129 | }; | ||
| 130 | # }}} | ||
| 131 | |||
| 132 | { # manage a global timeout {{{ | ||
| 133 | sub TIMEOUT_HALFTIME () { 15 } | ||
| 134 | my $timeout_initialized = 0; | ||
| 135 | my $timeout_poked = 0; | ||
| 136 | my $timeout_refs = 0; | ||
| 137 | |||
| 138 | create_timeout_session(); | ||
| 139 | |||
| 140 | sub create_timeout_session { | ||
| 141 | my $sess = POE::Session->create( | ||
| 142 | inline_states => { | ||
| 143 | _start => sub { | ||
| 144 | $_[KERNEL]->alias_set("timeout") and return; | ||
| 145 | $_[KERNEL]->delay(check_timeout => TIMEOUT_HALFTIME); | ||
| 146 | }, | ||
| 147 | check_timeout => sub { | ||
| 148 | unless ($timeout_poked) { | ||
| 149 | warn scalar(localtime), " - inactivity timeout reached!"; | ||
| 150 | CORE::exit 1; | ||
| 151 | } else { | ||
| 152 | $timeout_poked = 0; | ||
| 153 | $_[KERNEL]->delay(check_timeout => TIMEOUT_HALFTIME); | ||
| 154 | } | ||
| 155 | }, | ||
| 156 | try_shutdown => sub { | ||
| 157 | return unless $timeout_refs == 0; | ||
| 158 | $_[KERNEL]->delay(check_timeout => undef); | ||
| 159 | $_[KERNEL]->alias_remove("timeout"); | ||
| 160 | }, | ||
| 161 | _stop => sub { }, # Pacify assertions. | ||
| 162 | }, | ||
| 163 | ); | ||
| 164 | return $sess->ID; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub timeout_poke { | ||
| 168 | $timeout_poked++; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub timeout_incref { | ||
| 172 | timeout_poke(); | ||
| 173 | $timeout_refs++; | ||
| 174 | } | ||
| 175 | |||
| 176 | sub timeout_decref { | ||
| 177 | timeout_poke(); | ||
| 178 | $timeout_refs--; | ||
| 179 | if ($timeout_refs == 0) { | ||
| 180 | $poe_kernel->post("timeout", "try_shutdown"); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | } # }}} | ||
| 184 | |||
| 185 | { # {{{ a proxy around POE::Filter::Line that doesn't support get_one | ||
| 186 | package | ||
| 187 | My::LineFilter; | ||
| 188 | sub new { | ||
| 189 | my $class = shift; | ||
| 190 | return bless [ POE::Filter::Line->new(@_) ], $class; | ||
| 191 | } | ||
| 192 | sub get { my $s = shift; return $s->[0]->get(@_) } | ||
| 193 | sub put { my $s = shift; return $s->[0]->put(@_) } | ||
| 194 | } # }}} | ||
| 195 | |||
| 196 | # next follow some event handles that are used in constructing | ||
| 197 | # each session in &create_test_session | ||
| 198 | sub do_nonexistent { | ||
| 199 | warn scalar(localtime), " - $_[STATE] called on session ".$_[SESSION]->ID." ($_[HEAP]->{label})"; | ||
| 200 | CORE::exit 1; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub do_error { | ||
| 204 | note(scalar(localtime) . " - $_[HEAP]->{label}: $_[ARG0] error $_[ARG1]: $_[ARG2]"); | ||
| 205 | } | ||
| 206 | |||
| 207 | # {{{ definition of the main test session | ||
| 208 | |||
| 209 | sub main_perform_state { | ||
| 210 | my $heap = $_[HEAP]; | ||
| 211 | |||
| 212 | return unless @{$heap->{expected}}; | ||
| 213 | return unless defined $heap->{expected}->[0][0]; | ||
| 214 | |||
| 215 | my $action = $heap->{expected}->[0][0]; | ||
| 216 | |||
| 217 | unless (ref $action) { | ||
| 218 | note(scalar(localtime) . " - $heap->{label}: performing put state: $action"); | ||
| 219 | eval { $heap->{wheel}->put( $action ) }; | ||
| 220 | } elsif ($action->[0] =~ m/^(?:pause|resume)_std(?:out|err)$/) { | ||
| 221 | my $method = $action->[0]; | ||
| 222 | note(scalar(localtime) . " - $heap->{label}: performing method state: $method"); | ||
| 223 | $heap->{wheel}->$method(); | ||
| 224 | } elsif ($action->[0] eq "kill") { | ||
| 225 | note(scalar(localtime) . " - $heap->{label}: performing kill"); | ||
| 226 | $heap->{wheel}->kill(); | ||
| 227 | } elsif ($action->[0] eq "shutdown_stdin") { | ||
| 228 | note(scalar(localtime) . " - $heap->{label}: shutdown_stdin"); | ||
| 229 | $heap->{wheel}->shutdown_stdin(); | ||
| 230 | } else { | ||
| 231 | warn scalar(localtime), " - weird action @$action, this is a bug in the test script"; | ||
| 232 | CORE::exit 1; | ||
| 233 | } | ||
| 234 | |||
| 235 | # sometimes we don't have anything to wait for, so | ||
| 236 | # just perform the next action | ||
| 237 | if (not defined $heap->{expected}->[0][1]) { | ||
| 238 | shift @{$heap->{expected}}; | ||
| 239 | goto &main_perform_state; | ||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | my $main_counter = 0; | ||
| 244 | sub main_start { | ||
| 245 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 246 | my ($label, $program, $conduit, $expected) = @_[ARG0..$#_]; | ||
| 247 | |||
| 248 | $heap->{label} = $label; | ||
| 249 | |||
| 250 | # Sometimes use a filter without get_one support | ||
| 251 | my $filter_class = "POE::Filter::Line"; | ||
| 252 | if ($main_counter++ % 2) { | ||
| 253 | $filter_class = "My::LineFilter"; | ||
| 254 | } | ||
| 255 | |||
| 256 | # Run the child process | ||
| 257 | my $no_stderr = (defined $conduit and $conduit eq "pty"); | ||
| 258 | $heap->{wheel} = POE::Wheel::Run->new( | ||
| 259 | Program => $program, | ||
| 260 | ProgramArgs => [ "out$$", "err$$" ], # we assume $program expects this | ||
| 261 | (defined $conduit ? (Conduit => $conduit) : ()), | ||
| 262 | StdioFilter => $filter_class->new( Literal => "!" ), | ||
| 263 | (!$no_stderr ? (StderrFilter => $filter_class->new( Literal => "!" )) | ||
| 264 | : ()), | ||
| 265 | StdoutEvent => 'stdout_nonexistent', | ||
| 266 | (!$no_stderr ? (StderrEvent => 'stderr_nonexistent') : ()), | ||
| 267 | StdinEvent => 'stdin_nonexistent', | ||
| 268 | ErrorEvent => 'error_nonexistent', | ||
| 269 | CloseEvent => 'close_nonexistent', | ||
| 270 | ); | ||
| 271 | |||
| 272 | # Test event changing. | ||
| 273 | $heap->{wheel}->event( | ||
| 274 | StdoutEvent => 'stdout', | ||
| 275 | (!$no_stderr ? (StderrEvent => 'stderr') : ()), | ||
| 276 | StdinEvent => 'stdin', | ||
| 277 | ); | ||
| 278 | $heap->{wheel}->event( | ||
| 279 | ErrorEvent => 'error', | ||
| 280 | CloseEvent => 'close', | ||
| 281 | ); | ||
| 282 | |||
| 283 | $_[KERNEL]->sig_child($heap->{wheel}->PID, "sigchld"); | ||
| 284 | |||
| 285 | # start the test statemachine | ||
| 286 | $heap->{expected} = [@$expected]; | ||
| 287 | &main_perform_state; # Deliberately passing @_ through. | ||
| 288 | |||
| 289 | $heap->{flushes_expected} = scalar( | ||
| 290 | grep { (!ref $_->[0]) and defined($_->[1]) } @$expected | ||
| 291 | ); | ||
| 292 | $heap->{flushes} = 0; | ||
| 293 | |||
| 294 | # timeout delay | ||
| 295 | timeout_incref(); | ||
| 296 | |||
| 297 | note(scalar(localtime) . " - $heap->{label}: _start"); | ||
| 298 | } | ||
| 299 | |||
| 300 | my $x__ = 0; | ||
| 301 | sub main_stop { | ||
| 302 | my $heap = $_[HEAP]; | ||
| 303 | |||
| 304 | # Due to loop timing differences, "out delayed1" and "out | ||
| 305 | # immediate2" may sometimes not be flushed together. Allow one | ||
| 306 | # extra flush to account for "out delayed1" going separately. | ||
| 307 | |||
| 308 | $heap->{flushes_expected}++ if ( | ||
| 309 | $heap->{label} eq 'string/pause_resume' and | ||
| 310 | $heap->{flushes} - $heap->{flushes_expected} == 1 | ||
| 311 | ); | ||
| 312 | |||
| 313 | is( $heap->{flushes}, $heap->{flushes_expected}, | ||
| 314 | scalar(localtime) . " - $heap->{label} flush count ($$)" ) | ||
| 315 | unless $heap->{ignore_flushes}; | ||
| 316 | note(scalar(localtime) . " - $heap->{label}: _stop ($$)"); | ||
| 317 | } | ||
| 318 | |||
| 319 | sub main_stdin { | ||
| 320 | my $heap = $_[HEAP]; | ||
| 321 | $heap->{flushes}++; | ||
| 322 | timeout_poke(); | ||
| 323 | note(scalar(localtime) . " - $heap->{label}: stdin flush"); | ||
| 324 | } | ||
| 325 | |||
| 326 | sub main_output { | ||
| 327 | my ($heap, $state) = @_[HEAP, STATE]; | ||
| 328 | my $input = $_[ARG0]; | ||
| 329 | |||
| 330 | my $prefix = $heap->{expected}->[0][1][2] . $$; | ||
| 331 | |||
| 332 | $heap->{expected}->[0][1][1] = $heap->{wheel}->PID | ||
| 333 | unless defined $heap->{expected}->[0][1][1]; | ||
| 334 | |||
| 335 | is($state, $heap->{expected}->[0][1][0], | ||
| 336 | scalar(localtime) . " - $heap->{label} response type"); | ||
| 337 | is($input, "$prefix: ".$heap->{expected}->[0][1][1], | ||
| 338 | scalar(localtime) . " - $heap->{label} $state response"); | ||
| 339 | |||
| 340 | note(scalar(localtime) . " - $heap->{label}: $state $input"); | ||
| 341 | |||
| 342 | timeout_poke(); | ||
| 343 | |||
| 344 | shift @{$heap->{expected}}; | ||
| 345 | &main_perform_state; | ||
| 346 | } | ||
| 347 | |||
| 348 | sub main_close { | ||
| 349 | my ($heap, $kernel) = @_[HEAP, KERNEL]; | ||
| 350 | is('close', $heap->{expected}->[0][1][0], | ||
| 351 | scalar(localtime) . " - $heap->{label} close"); | ||
| 352 | is($_[HEAP]->{wheel}->get_driver_out_octets, 0, | ||
| 353 | scalar(localtime) . " - $heap->{label} driver_out_octets at close") | ||
| 354 | unless $heap->{ignore_flushes}; | ||
| 355 | is($_[HEAP]->{wheel}->get_driver_out_messages, 0, | ||
| 356 | scalar(localtime) . " - $heap->{label} driver_out_messages at close") | ||
| 357 | unless $heap->{ignore_flushes}; | ||
| 358 | delete $_[HEAP]->{wheel}; | ||
| 359 | timeout_decref(); | ||
| 360 | $kernel->sig("CHLD" => undef); | ||
| 361 | note(scalar(localtime) . " - $heap->{label}: close"); | ||
| 362 | } | ||
| 363 | |||
| 364 | sub main_sigchld { | ||
| 365 | my $heap = $_[HEAP]; | ||
| 366 | my ($signame, $child_pid) = @_[ARG0, ARG1]; | ||
| 367 | |||
| 368 | my $our_child = $heap->{wheel} ? $heap->{wheel}->PID : -1; | ||
| 369 | note( | ||
| 370 | scalar(localtime) . " - $heap->{label}: sigchld $signame for $child_pid ($our_child)" | ||
| 371 | ); | ||
| 372 | |||
| 373 | return unless $heap->{wheel} and $our_child == $child_pid; | ||
| 374 | |||
| 375 | # turn it into a close | ||
| 376 | &main_close; | ||
| 377 | } | ||
| 378 | |||
| 379 | sub create_test_session { | ||
| 380 | my ($label, $program, $conduit, $expected, $ignore_flushes) = @_; | ||
| 381 | |||
| 382 | my $sess = POE::Session->create( | ||
| 383 | args => [$label, $program, $conduit, $expected], | ||
| 384 | heap => { ignore_flushes => $ignore_flushes }, | ||
| 385 | inline_states => { | ||
| 386 | _start => \&main_start, | ||
| 387 | _stop => \&main_stop, | ||
| 388 | error => \&do_error, | ||
| 389 | close => \&main_close, | ||
| 390 | stdin => \&main_stdin, | ||
| 391 | stdout => \&main_output, | ||
| 392 | stderr => \&main_output, | ||
| 393 | sigchld => \&main_sigchld, | ||
| 394 | stdout_nonexistent => \&do_nonexistent, | ||
| 395 | stderr_nonexistent => \&do_nonexistent, | ||
| 396 | stdin_nonexistent => \&do_nonexistent, | ||
| 397 | error_nonexistent => \&do_nonexistent, | ||
| 398 | close_nonexistent => \&do_nonexistent, | ||
| 399 | }, | ||
| 400 | ); | ||
| 401 | |||
| 402 | return $sess->ID; | ||
| 403 | } | ||
| 404 | # }}} | ||
| 405 | |||
| 406 | # {{{ Constructor tests | ||
| 407 | sub create_constructor_session { | ||
| 408 | my $sess = POE::Session->create( | ||
| 409 | inline_states => { | ||
| 410 | _start => sub { | ||
| 411 | eval { | ||
| 412 | POE::Wheel::Run->new( | ||
| 413 | Program => sub { 1; }, | ||
| 414 | Conduit => 'wibble-magic-pipe', | ||
| 415 | StdoutEvent => 'stdout_event', | ||
| 416 | ErrorEvent => 'error_event', | ||
| 417 | ); | ||
| 418 | }; | ||
| 419 | ok(!(!$@), scalar(localtime) . " - new: only valid conduits"); | ||
| 420 | |||
| 421 | eval { | ||
| 422 | POE::Wheel::Run->new( | ||
| 423 | Program => sub { 1; }, | ||
| 424 | Filter => POE::Filter::Line->new( Literal => "!" ), | ||
| 425 | StdioFilter => POE::Filter::Line->new( Literal => "!" ), | ||
| 426 | StdoutEvent => 'stdout_event', | ||
| 427 | ErrorEvent => 'error_event', | ||
| 428 | ); | ||
| 429 | }; | ||
| 430 | ok(!(!$@), scalar(localtime) . " - new: cannot mix deprecated Filter with StdioFilter"); | ||
| 431 | |||
| 432 | eval { | ||
| 433 | POE::Wheel::Run->new( | ||
| 434 | ProgramArgs => [ "out$$", "err$$" ], | ||
| 435 | Conduit => "pty", | ||
| 436 | StdioFilter => POE::Filter::Line->new( Literal => "!" ), | ||
| 437 | StderrFilter => POE::Filter::Line->new( Literal => "!" ), | ||
| 438 | StdoutEvent => 'stdout_nonexistent', | ||
| 439 | StderrEvent => 'stderr_nonexistent', | ||
| 440 | StdinEvent => 'stdin_nonexistent', | ||
| 441 | ErrorEvent => 'error_nonexistent', | ||
| 442 | CloseEvent => 'close_nonexistent', | ||
| 443 | ); | ||
| 444 | }; | ||
| 445 | ok(!(!$@), scalar(localtime) . " - new: Program is needed"); | ||
| 446 | |||
| 447 | eval { | ||
| 448 | POE::Wheel::Run->new( | ||
| 449 | Program => sub { 0 }, | ||
| 450 | StdoutEvent => "stdout_nonexistent", | ||
| 451 | RedirectStdout => "/non/existent" | ||
| 452 | ); | ||
| 453 | }; | ||
| 454 | ok(!(!$@), scalar(localtime) . " - new: *Event and Redirect* are mutually exclusive"); | ||
| 455 | |||
| 456 | timeout_poke(); | ||
| 457 | }, | ||
| 458 | _stop => sub { }, # Pacify assertions. | ||
| 459 | }, | ||
| 460 | ); | ||
| 461 | |||
| 462 | return $sess->ID; | ||
| 463 | } | ||
| 464 | # }}} | ||
| 465 | |||
| 466 | # Main program: Create test sessions {{{ | ||
| 467 | my @one_stream_expected = ( | ||
| 468 | [ "out test-out", ["stdout", "test-out", "out"] ], | ||
| 469 | [ "err test-err", ["stdout", "test-err", "err"] ], # std*out* not stderr | ||
| 470 | [ "bye", ["close"] ], | ||
| 471 | ); | ||
| 472 | my @two_stream_expected = ( | ||
| 473 | [ "out test-out", ["stdout", "test-out", "out"] ], | ||
| 474 | [ "err test-err", ["stderr", "test-err", "err"] ], | ||
| 475 | [ "bye", ["close"] ], | ||
| 476 | ); | ||
| 477 | my @pause_resume_expected = ( | ||
| 478 | [ "out init", ["stdout", "init", "out"] ], | ||
| 479 | [ ["pause_stdout"], undef ], | ||
| 480 | [ "out delayed1", undef ], | ||
| 481 | [ "err immediate1", ["stderr", "immediate1", "err"] ], | ||
| 482 | [ ["pause_stderr"], undef ], | ||
| 483 | [ "err delayed2", undef ], | ||
| 484 | [ ["resume_stdout"], ["stdout", "delayed1", "out"] ], | ||
| 485 | [ "out immediate2", ["stdout", "immediate2", "out"] ], | ||
| 486 | [ ["resume_stderr"], ["stderr", "delayed2", "err"] ], | ||
| 487 | [ "out immediate3", ["stdout", "immediate3", "out"] ], | ||
| 488 | [ "err immediate4", ["stderr", "immediate4", "err"] ], | ||
| 489 | [ "bye", ["close"] ], | ||
| 490 | ); | ||
| 491 | my @killing_expected = ( | ||
| 492 | [ "out init", ["stdout", "init", "out"] ], | ||
| 493 | [ "pid", ["stdout", undef, "out"] ], | ||
| 494 | [ ["kill"], ["close"] ], | ||
| 495 | ); | ||
| 496 | my @shutdown_expected = ( | ||
| 497 | [ "flag 1", undef], | ||
| 498 | [ "out init", ["stdout", "init", "out"] ], | ||
| 499 | [ ["shutdown_stdin"], undef], | ||
| 500 | [ "flag 2", ["stdout", "got eof 1", "out"] ], | ||
| 501 | [ ["kill"], ["close"] ], | ||
| 502 | ); | ||
| 503 | |||
| 504 | my @chld_programs = ( | ||
| 505 | ["string", $chld_program_string], | ||
| 506 | ["coderef", $chld_program_coderef], | ||
| 507 | ); | ||
| 508 | |||
| 509 | # create constructor test session | ||
| 510 | create_constructor_session(); | ||
| 511 | |||
| 512 | # test pausing/resuming for both stdout and stderr | ||
| 513 | create_test_session( | ||
| 514 | "string/pause_resume", | ||
| 515 | $chld_program_string, | ||
| 516 | undef, | ||
| 517 | \@pause_resume_expected, | ||
| 518 | ); | ||
| 519 | SKIP: { | ||
| 520 | skip "PIDs and shutdown don't work on windows", 13 | ||
| 521 | if $RUNNING_WIN32; | ||
| 522 | # testing killing, and PID | ||
| 523 | create_test_session( | ||
| 524 | "string/killing", | ||
| 525 | $chld_program_string, | ||
| 526 | undef, | ||
| 527 | \@killing_expected, | ||
| 528 | ); # needs to be skipped on windows | ||
| 529 | # test shutdown_stdin | ||
| 530 | create_test_session( | ||
| 531 | "coderef/shutdown", | ||
| 532 | $shutdown_program, | ||
| 533 | "pipe", | ||
| 534 | \@shutdown_expected, | ||
| 535 | 1, # ignore flush counts etc | ||
| 536 | ); | ||
| 537 | } | ||
| 538 | |||
| 539 | sub silent_start { | ||
| 540 | pipe my ($stdout_read,$stdout_write); | ||
| 541 | pipe my ($stdin_read, $stdin_write); | ||
| 542 | |||
| 543 | my $wheel = POE::Wheel::Run->new( | ||
| 544 | Program => sub { | ||
| 545 | select STDERR; $|=1; | ||
| 546 | select STDOUT; $|=1; | ||
| 547 | eval "print STDOUT 'CHILD:'"; | ||
| 548 | eval 'my $input = <STDIN>; chomp($input); print STDERR $input;'; | ||
| 549 | eval 'print STDERR "DONE:";'; | ||
| 550 | close STDOUT; | ||
| 551 | close STDERR; | ||
| 552 | exit(0); | ||
| 553 | }, | ||
| 554 | |||
| 555 | RedirectOutput => $stdout_write, | ||
| 556 | RedirectStdin => $stdin_read | ||
| 557 | |||
| 558 | ); | ||
| 559 | |||
| 560 | select $stdin_write; $|=1; | ||
| 561 | |||
| 562 | $_[HEAP]->{silent_fdes} = [$stdout_read, $stdin_write]; | ||
| 563 | $_[HEAP]->{silent_wheel} = $wheel; | ||
| 564 | |||
| 565 | print $stdin_write "PARENT:\n"; | ||
| 566 | $poe_kernel->select_read($stdout_read, 'silent_got_stdout'); | ||
| 567 | $poe_kernel->sig_child($wheel->PID, 'silent_sigchld'); | ||
| 568 | |||
| 569 | my $no_stdio = POE::Wheel::Run->new( | ||
| 570 | Program => \¬e | ||
| 571 | ); | ||
| 572 | $poe_kernel->sig_child($no_stdio->PID, 'silent_sigchld'); | ||
| 573 | |||
| 574 | ok(!($no_stdio->[ $no_stdio->HANDLE_STDOUT ] || | ||
| 575 | $no_stdio->[ $no_stdio->HANDLE_STDERR ]), | ||
| 576 | scalar(localtime) . "stdio/standard output handles closed without events"); | ||
| 577 | |||
| 578 | ok($no_stdio->[ $no_stdio->HANDLE_STDIN ], | ||
| 579 | scalar(localtime) . "stdio discard/STDIN still alive"); | ||
| 580 | |||
| 581 | $no_stdio = POE::Wheel::Run->new( | ||
| 582 | Program => \¬e, | ||
| 583 | NoStdin => 1 | ||
| 584 | ); | ||
| 585 | |||
| 586 | $poe_kernel->sig_child($no_stdio->PID, 'silent_sigchld'); | ||
| 587 | ok(!($no_stdio->[ $no_stdio->HANDLE_STDIN ]), | ||
| 588 | scalar(localtime) . "stdio/discarded STDIN with NoStdin"); | ||
| 589 | } | ||
| 590 | |||
| 591 | sub _silent_check_common { | ||
| 592 | my ($fh,$re,$desc,$a,$term) = @_; | ||
| 593 | sysread($fh, my $buf = "", 8192); | ||
| 594 | if( $a ) { | ||
| 595 | $$a = '' unless defined $$a; | ||
| 596 | $$a .= $buf; | ||
| 597 | return unless $$a =~ $term; | ||
| 598 | } | ||
| 599 | |||
| 600 | like($buf, $re, $desc); | ||
| 601 | $poe_kernel->select_read($fh); | ||
| 602 | close($fh); | ||
| 603 | } | ||
| 604 | |||
| 605 | my $ACCUME; | ||
| 606 | sub silent_got_stdout { | ||
| 607 | _silent_check_common( | ||
| 608 | $_[ARG0], | ||
| 609 | qr/CHILD:PARENT:DONE:/, | ||
| 610 | "stdio/redirection", | ||
| 611 | \$ACCUME, | ||
| 612 | qr/DONE/ | ||
| 613 | ); | ||
| 614 | } | ||
| 615 | |||
| 616 | sub silent_fd_status { | ||
| 617 | _silent_check_common( | ||
| 618 | $_[ARG0], | ||
| 619 | qr/STDERR:-1,STDIN:-1,STDOUT:-1/, | ||
| 620 | "stdio closed in child"); | ||
| 621 | } | ||
| 622 | |||
| 623 | sub silent_sigchld { | ||
| 624 | #dummy.. | ||
| 625 | } | ||
| 626 | |||
| 627 | |||
| 628 | |||
| 629 | sub silent_test { | ||
| 630 | #Use pipes here for redirection. | ||
| 631 | my $sess = POE::Session->create( | ||
| 632 | inline_states => { | ||
| 633 | _start => \&silent_start, | ||
| 634 | _stop => sub { }, | ||
| 635 | #_stop => sub { note scalar(localtime) . " - Stopped!" }, | ||
| 636 | silent_got_stdout => \&silent_got_stdout, | ||
| 637 | silent_fd_status => \&silent_fd_status, | ||
| 638 | silent_sigchld => \&silent_sigchld | ||
| 639 | } | ||
| 640 | ); | ||
| 641 | } | ||
| 642 | |||
| 643 | for my $chld_program (@chld_programs) { | ||
| 644 | my ($chld_name, $chld_code) = @$chld_program; | ||
| 645 | |||
| 646 | create_test_session( | ||
| 647 | "$chld_name/default", | ||
| 648 | $chld_code, # program | ||
| 649 | undef, # conduit | ||
| 650 | \@two_stream_expected # expected | ||
| 651 | ); | ||
| 652 | |||
| 653 | SKIP: { | ||
| 654 | skip "$chld_name/pipe: doesn't work on windows", STD_TEST_COUNT | ||
| 655 | if $RUNNING_WIN32; | ||
| 656 | create_test_session( | ||
| 657 | "$chld_name/pipe", | ||
| 658 | $chld_code, # program | ||
| 659 | "pipe", # conduit | ||
| 660 | \@two_stream_expected # expected | ||
| 661 | ); | ||
| 662 | } | ||
| 663 | |||
| 664 | SKIP: { | ||
| 665 | skip "$chld_name/pty: IO::Pty is needed for this test.", 2*STD_TEST_COUNT | ||
| 666 | unless POE::Wheel::Run::PTY_AVAILABLE; | ||
| 667 | |||
| 668 | skip "$chld_name/pty: The underlying event loop has trouble with ptys on $^O", 2*STD_TEST_COUNT | ||
| 669 | if $^O eq "darwin" and ( | ||
| 670 | exists $INC{"POE/Loop/IO_Poll.pm"} or | ||
| 671 | exists $INC{"POE/Loop/Event.pm"} or | ||
| 672 | $ENV{POE_LOOP_USES_POLL} | ||
| 673 | ); | ||
| 674 | |||
| 675 | create_test_session( | ||
| 676 | "$chld_name/pty", | ||
| 677 | $chld_code, # program | ||
| 678 | "pty", # conduit | ||
| 679 | \@one_stream_expected # expected | ||
| 680 | ); | ||
| 681 | create_test_session( | ||
| 682 | "$chld_name/pty-pipe", | ||
| 683 | $chld_code, # program | ||
| 684 | "pty-pipe", # conduit | ||
| 685 | \@two_stream_expected # expected | ||
| 686 | ); | ||
| 687 | } | ||
| 688 | } | ||
| 689 | # }}} | ||
| 690 | |||
| 691 | silent_test(); | ||
| 692 | |||
| 693 | $poe_kernel->run; | ||
| 694 | |||
| 695 | 1; |