ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/t/03_child.t
(Generate patch)

Comparing AnyEvent/t/03_child.t (file contents):
Revision 1.2 by root, Fri Nov 2 19:20:36 2007 UTC vs.
Revision 1.24 by root, Fri Mar 30 04:25:11 2012 UTC

1$|=1; 1use POSIX ();
2BEGIN { print "1..5\n" } 2
3no warnings;
4
5BEGIN {
6 # check for broken perls
7 if ($^O =~ /mswin32/i) {
8 my $ok;
9 local $SIG{CHLD} = sub { $ok = 1 };
10 kill 'CHLD', 0;
11
12 unless ($ok) {
13 print <<EOF;
141..0 # SKIP Your perl interpreter is badly BROKEN. Child watchers will not work, ever. Try upgrading to a newer perl or a working perl (cygwin's perl is known to work). If that is not an option, you should be able to use the remaining functionality of AnyEvent, but child watchers WILL NOT WORK.
15EOF
16 exit 0;
17 }
18 }
19}
3 20
4use AnyEvent; 21use AnyEvent;
22BEGIN { require AnyEvent::Impl::Perl unless $ENV{PERL_ANYEVENT_MODEL} }
5 23
6print "ok 1\n"; 24$| = 1; print "1..50\n";
7 25
8my $pid = fork; 26$AnyEvent::MAX_SIGNAL_LATENCY = 0.2;
9 27
10defined $pid or die "unable to fork"; 28for my $it ("", 1, 2, 3, 4) {
29 print "ok ${it}1\n";
11 30
12# work around Tk bug until it has been fixed. 31 AnyEvent::detect; # force-load event model
13my $timer = AnyEvent->timer (after => 2, cb => sub { });
14 32
15my $cv = AnyEvent->condvar; 33 my $pid = fork;
16 34
35 defined $pid or die "unable to fork";
36
37 # work around Tk bug until it has been fixed.
38 #my $timer = AnyEvent->timer (after => 2, cb => sub { });
39
40 my $cv = AnyEvent->condvar;
41
17unless ($pid) { 42 unless ($pid) {
43 print "ok ${it}2 # child $$\n";
44
45 # POE hits a race condition when the child dies too quickly
46 # because it checks for child exit before installing the signal handler.
47 # seen in version 1.352 - earlier versions had the same bug, but
48 # polled for child exits regularly, so only caused a delay.
49 sleep 1 if $AnyEvent::MODEL eq "AnyEvent::Impl::POE";
50
51 POSIX::_exit 3;
52 }
53 my $w = AnyEvent->child (pid => $pid, cb => sub {
54 print $pid == $_[0] ? "" : "not ", "ok ${it}3\ # $pid == $_[0]\n";
55 print 3 == ($_[1] >> 8) ? "" : "not ", "ok ${it}4 # 3 == $_[1] >> 8 ($_[1])\n";
56 $cv->broadcast;
57 });
58
59 $cv->recv;
60
61 my $pid2 = fork || do {
62 sleep 1 if $AnyEvent::MODEL eq "AnyEvent::Impl::POE";
63 POSIX::_exit 7;
64 };
65
66 my $cv2 = AnyEvent->condvar;
67
68 # Glib is the only model that doesn't support pid == 0
69 my $pid0 = $AnyEvent::MODEL eq "AnyEvent::Impl::Glib" ? $pid2 : 0;
70
71 my $w2 = AnyEvent->child (pid => $pid0, cb => sub {
72 print $pid2 == $_[0] ? "" : "not ", "ok ${it}5 # $pid2 == $_[0]\n";
73 print 7 == ($_[1] >> 8) ? "" : "not ", "ok ${it}6 # 7 == $_[1] >> 8 ($_[1])\n";
74 $cv2->broadcast;
75 });
76
77 my $error = AnyEvent->timer (after => 5, cb => sub {
78 print <<EOF;
79Bail out! No child exit detected. This is either a bug in AnyEvent or a bug in your Perl (mostly some windows distributions suffer from that): child watchers might not work properly on this platform. You can force installation of this module if you do not rely on child watchers, or you could upgrade to a working version of Perl for your platform.\n";
80EOF
81 exit 0;
82 });
83
84 $cv2->recv;
85
18 print "ok 2\n"; 86 print "ok ${it}7\n";
19 exit 3; 87 print "ok ${it}8\n";
88 print "ok ${it}9\n";
89 print "ok ", $it*10+10, "\n";
20} 90}
21
22my $w = AnyEvent->child (pid => $pid, cb => sub {
23 print 3 == ($? >> 8) ? "" : "not ", "ok 3\n";
24 $cv->broadcast;
25});
26
27$cv->wait;
28
29fork || exit 7;
30
31my $cv2 = AnyEvent->condvar;
32
33my $w2 = AnyEvent->child (pid => 0, cb => sub {
34 print 7 == ($? >> 8) ? "" : "not ", "ok 4\n";
35 $cv2->broadcast;
36});
37
38$cv2->wait;
39
40print "ok 5\n";
41 91
42 92
43 93
44 94

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines