POSIX BC2000 port from perl-mvs:
[p5sagit/p5-mst-13.2.git] / t / lib / io_sock.t
index 53690b1..8fc52e4 100755 (executable)
@@ -1,14 +1,23 @@
 #!./perl
 
 BEGIN {
-    chdir 't' if -d 't';
-    @INC = '../lib' if -d '../lib';
-    require Config; import Config;
-    if ( ($Config{'extensions'} !~ /\bSocket\b/ ||
-          $Config{'extensions'} !~ /\bIO\b/)    &&
-          !(($^O eq 'VMS') && $Config{d_socket})) {
-       print "1..0\n";
-       exit 0;
+    unless(grep /blib/, @INC) {
+       chdir 't' if -d 't';
+       @INC = '../lib' if -d '../lib';
+    }
+}
+
+use Config;
+
+BEGIN {
+    if (-d "lib" && -f "TEST") {
+        if (!$Config{'d_fork'} ||
+           (($Config{'extensions'} !~ /\bSocket\b/ ||
+             $Config{'extensions'} !~ /\bIO\b/) &&
+            !(($^O eq 'VMS') && $Config{d_socket}))) {
+           print "1..0\n";
+           exit 0;
+        }
     }
 }
 
@@ -17,22 +26,22 @@ print "1..5\n";
 
 use IO::Socket;
 
-$port = 4002 + int(rand(time) & 0xff);
-$SIG{ALRM} = sub {};
-
-$pid =  fork();
+$listen = IO::Socket::INET->new(Listen => 2,
+                               Proto => 'tcp',
+                              ) or die "$!";
 
-if($pid) {
+print "ok 1\n";
 
-    $listen = IO::Socket::INET->new(Listen => 2,
-                                   Proto => 'tcp',
-                                   LocalPort => $port
-                                  ) or die "$!";
+# Check if can fork with dynamic extensions (bug in CRT):
+if ($^O eq 'os2' and
+    system "$^X -I../lib -MOpcode -e 'defined fork or die'  > /dev/null 2>&1") {
+    print "ok $_ # skipped: broken fork\n" for 2..5;
+    exit 0;
+}
 
-    print "ok 1\n";
+$port = $listen->sockport;
 
-    # Wake out child
-    kill(ALRM => $pid);
+if($pid = fork()) {
 
     $sock = $listen->accept();
     print "ok 2\n";
@@ -47,22 +56,29 @@ if($pid) {
     waitpid($pid,0);
 
     print "ok 5\n";
-} elsif(defined $pid) {
 
-    # Wait for a small pause, so that we can ensure the listen socket is setup
-    # the parent will awake us with a SIGALRM
+} elsif(defined $pid) {
 
-    sleep(10);
+    # This can fail if localhost is undefined or the
+    # special 'loopback' address 127.0.0.1 is not configured
+    # on your system. (/etc/rc.config.d/netconfig on HP-UX.)
+    # As a shortcut (not recommended) you could change 'localhost'
+    # here to be the name of this machine eg 'myhost.mycompany.com'.
 
     $sock = IO::Socket::INET->new(PeerPort => $port,
                                  Proto => 'tcp',
                                  PeerAddr => 'localhost'
-                                ) or die "$!";
+                                )
+           or die "$! (maybe your system does not have the 'localhost' address defined)";
 
     $sock->autoflush(1);
+
     print $sock "ok 3\n";
+
     print $sock->getline();
+
     $sock->close;
+
     exit;
 } else {
  die;