--- AnyEvent/t/03_child.t 2007/11/25 13:53:04 1.6 +++ AnyEvent/t/03_child.t 2012/03/30 04:25:11 1.24 @@ -1,46 +1,93 @@ -$|=1; -BEGIN { print "1..7\n" } +use POSIX (); + +no warnings; + +BEGIN { + # check for broken perls + if ($^O =~ /mswin32/i) { + my $ok; + local $SIG{CHLD} = sub { $ok = 1 }; + kill 'CHLD', 0; + + unless ($ok) { + print <timer (after => 2, cb => sub { }); + AnyEvent::detect; # force-load event model -my $cv = AnyEvent->condvar; + my $pid = fork; -unless ($pid) { - print "ok 2\n"; - exit 3; -} + defined $pid or die "unable to fork"; + + # work around Tk bug until it has been fixed. + #my $timer = AnyEvent->timer (after => 2, cb => sub { }); + + my $cv = AnyEvent->condvar; -my $w = AnyEvent->child (pid => $pid, cb => sub { - print $pid == $_[0] ? "" : "not ", "ok 3\n"; - print 3 == ($_[1] >> 8) ? "" : "not ", "ok 4\n"; - $cv->broadcast; -}); + unless ($pid) { + print "ok ${it}2 # child $$\n"; -$cv->wait; + # POE hits a race condition when the child dies too quickly + # because it checks for child exit before installing the signal handler. + # seen in version 1.352 - earlier versions had the same bug, but + # polled for child exits regularly, so only caused a delay. + sleep 1 if $AnyEvent::MODEL eq "AnyEvent::Impl::POE"; -my $pid2 = fork || exit 7; + POSIX::_exit 3; + } + my $w = AnyEvent->child (pid => $pid, cb => sub { + print $pid == $_[0] ? "" : "not ", "ok ${it}3\ # $pid == $_[0]\n"; + print 3 == ($_[1] >> 8) ? "" : "not ", "ok ${it}4 # 3 == $_[1] >> 8 ($_[1])\n"; + $cv->broadcast; + }); -my $cv2 = AnyEvent->condvar; + $cv->recv; -my $w2 = AnyEvent->child (pid => 0, cb => sub { - print $pid2 == $_[0] ? "" : "not ", "ok 5\n"; - print 7 == ($_[1] >> 8) ? "" : "not ", "ok 6\n"; - $cv2->broadcast; -}); + my $pid2 = fork || do { + sleep 1 if $AnyEvent::MODEL eq "AnyEvent::Impl::POE"; + POSIX::_exit 7; + }; -$cv2->wait; + my $cv2 = AnyEvent->condvar; -print "ok 7\n"; + # Glib is the only model that doesn't support pid == 0 + my $pid0 = $AnyEvent::MODEL eq "AnyEvent::Impl::Glib" ? $pid2 : 0; + + my $w2 = AnyEvent->child (pid => $pid0, cb => sub { + print $pid2 == $_[0] ? "" : "not ", "ok ${it}5 # $pid2 == $_[0]\n"; + print 7 == ($_[1] >> 8) ? "" : "not ", "ok ${it}6 # 7 == $_[1] >> 8 ($_[1])\n"; + $cv2->broadcast; + }); + + my $error = AnyEvent->timer (after => 5, cb => sub { + print <recv; + + print "ok ${it}7\n"; + print "ok ${it}8\n"; + print "ok ${it}9\n"; + print "ok ", $it*10+10, "\n"; +}