X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=ext%2FSocket%2Fsocketpair.t;h=fea1cb53abe49167e9b23bf18bbe9499711f4ead;hb=c0bb2de79803fbbfe9a5966d43509add0c8bb4de;hp=653e1b78c96f75080a687e01eecc960e13e90aad;hpb=2948e0bde4eb0485569b1f3510975019a89a444f;p=p5sagit%2Fp5-mst-13.2.git diff --git a/ext/Socket/socketpair.t b/ext/Socket/socketpair.t index 653e1b7..fea1cb5 100644 --- a/ext/Socket/socketpair.t +++ b/ext/Socket/socketpair.t @@ -1,30 +1,70 @@ #!./perl -w +my $child; +my $can_fork; +my $has_perlio; + BEGIN { chdir 't' if -d 't'; @INC = '../lib'; require Config; import Config; - if ($Config{'extensions'} !~ /\bSocket\b/ && + $can_fork = $Config{'d_fork'} + || ($^O eq 'MSWin32' && $Config{useithreads} + && $Config{ccflags} =~ /-DPERL_IMPLICIT_SYS\b/); + + + if ($^O eq "hpux" or $Config{'extensions'} !~ /\bSocket\b/ && !(($^O eq 'VMS') && $Config{d_socket})) { print "1..0\n"; exit 0; + } + + # Too many things in this test will hang forever if something is wrong, + # so we need a self destruct timer. And IO can hang despite an alarm. + + # This is convoluted, but we must fork before Test::More, else child's + # Test::More thinks that it ran no tests, and prints a message to that + # effect + if( $can_fork) { + my $parent = $$; + $child = fork; + die "Fork failed" unless defined $child; + if (!$child) { + $SIG{INT} = sub {exit 0}; # You have 60 seconds. Your time starts now. + my $must_finish_by = time + 60; + my $remaining; + while (($remaining = $must_finish_by - time) > 0) { + sleep $remaining; + } + warn "Something unexpectedly hung during testing"; + kill "INT", $parent or die "Kill failed: $!"; + exit 1; + } + } + unless ($has_perlio = find PerlIO::Layer 'perlio') { + print < "alarm() not implemented on this platform"; +} elsif( !$can_fork ) { + plan skip_all => "fork() not implemented on this platform"; } else { # This should fail but not die if there is real socketpair eval {socketpair LEFT, RIGHT, -1, -1, -1}; - if ($@ =~ /^Unsupported socket function "socketpair" called/) { + if ($@ =~ /^Unsupported socket function "socketpair" called/ || + $! =~ /^The operation requested is not supported./) { # Stratus VOS plan skip_all => 'No socketpair (real or emulated)'; } else { eval {AF_UNIX}; @@ -36,15 +76,18 @@ if( !$Config{d_alarm} ) { } } -# Too many things in this test will hang forever if something is wrong, so -# we need a self destruct timer. -$SIG{ALRM} = sub {die "Something unexpectedly hung during testing"}; -alarm(60); +# But we'll install an alarm handler in case any of the races below fail. +$SIG{ALRM} = sub {die "Unexpected alarm during testing"}; ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC), "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)") or print "# \$\! = $!\n"; +if ($has_perlio) { + binmode(LEFT, ":bytes"); + binmode(RIGHT, ":bytes"); +} + my @left = ("hello ", "world\n"); my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here. @@ -69,9 +112,12 @@ is (read (RIGHT, $buffer, length $expect), length $expect, "read on right"); is ($buffer, $expect, "content what we expected?"); ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing"); -# This will hang forever if eof is buggy. +# This will hang forever if eof is buggy, and alarm doesn't interrupt system +# Calls. Hence the child process minder. { local $SIG{ALRM} = sub { warn "EOF on right took over 3 seconds" }; + local $TODO = "Known problems with unix sockets on $^O" + if $^O eq 'hpux' || $^O eq 'super-ux' || $^O eq 'unicosmk'; alarm 3; $! = 0; ok (eof RIGHT, "right is at EOF"); @@ -79,18 +125,25 @@ ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing"); alarm 60; } +my $err = $!; $SIG{PIPE} = 'IGNORE'; { local $SIG{ALRM} = sub { warn "syswrite to left didn't fail within 3 seconds" }; alarm 3; - is (syswrite (LEFT, "void"), undef, "syswrite to shutdown left should fail"); + # Split the system call from the is() - is() does IO so + # (say) a flush may do a seek which on a pipe may disturb errno + my $ans = syswrite (LEFT, "void"); + $err = $!; + is ($ans, undef, "syswrite to shutdown left should fail"); alarm 60; } -SKIP: { - # This may need skipping on some OSes - ok (($! == EPIPE or $! == ESHUTDOWN), '$! should be EPIPE or ESHUTDOWN') - or printf "\$\!=%d(%s)\n", $!, $!; +{ + # This may need skipping on some OSes - restoring value saved above + # should help + $! = $err; + ok (($!{EPIPE} or $!{ESHUTDOWN}), '$! should be EPIPE or ESHUTDOWN') + or printf "\$\!=%d(%s)\n", $err, $err; } my @gripping = (chr 255, chr 127); @@ -108,14 +161,24 @@ is ($buffer, $expect, "content what we expected?"); ok (close LEFT, "close left"); ok (close RIGHT, "close right"); + # And now datagrams # I suspect we also need a self destruct time-bomb for these, as I don't see any # guarantee that the stack won't drop a UDP packet, even if it is for localhost. +SKIP: { + skip "No usable SOCK_DGRAM for socketpair", 24 if ($^O =~ /^(MSWin32|os2)\z/); + local $TODO = "socketpair not supported on $^O" if $^O eq 'nto'; + ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC), "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)") or print "# \$\! = $!\n"; +if ($has_perlio) { + binmode(LEFT, ":bytes"); + binmode(RIGHT, ":bytes"); +} + foreach (@left) { # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left"); is (syswrite (LEFT, $_), length $_, "syswrite to left"); @@ -141,10 +204,13 @@ foreach $expect (@left) { } ok (shutdown(LEFT, 1), "shutdown left for writing"); + # eof uses buffering. eof is indicated by a sysread of zero. # but for a datagram socket there's no way it can know nothing will ever be # sent -{ +SKIP: { + skip "$^O does length 0 udp reads", 2 if ($^O eq 'os390'); + my $alarmed = 0; local $SIG{ALRM} = sub { $alarmed = 1; }; print "# Approximate forever as 3 seconds. Wait 'forever'...\n"; @@ -154,6 +220,7 @@ ok (shutdown(LEFT, 1), "shutdown left for writing"); "read on right should be interrupted"); is ($alarmed, 1, "alarm should have fired"); } + alarm 30; #ok (eof RIGHT, "right is at EOF"); @@ -171,3 +238,8 @@ foreach $expect (@gripping) { ok (close LEFT, "close left"); ok (close RIGHT, "close right"); + +} # end of DGRAM SKIP + +kill "INT", $child or warn "Failed to kill child process $child: $!"; +exit 0;