summaryrefslogtreecommitdiff
path: root/t/lib/io_sock.t
blob: 156f6cb78f3327bae7805a6fe2f1291bce158070 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!./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;
    }
}

$| = 1;
print "1..5\n";

use IO::Socket;

srand(time);
$port = 4002 + int(rand 0xff);
print "# using port $port.\n";
$SIG{ALRM} = sub {};

$pid =  fork();

if($pid) {

    $listen = IO::Socket::INET->new(Listen => 2,
				    Proto => 'tcp',
				    LocalPort => $port
				   ) or die "$!";

    print "ok 1\n";

    # Wake out child
    kill(ALRM => $pid);

    $sock = $listen->accept();
    print "ok 2\n";

    $sock->autoflush(1);
    print $sock->getline();

    print $sock "ok 4\n";

    $sock->close;

    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

    sleep(10);

    $sock = IO::Socket::INET->new(PeerPort => $port,
				  Proto => 'tcp',
				  PeerAddr => 'localhost'
				 ) or die "$!";

    $sock->autoflush(1);
    print $sock "ok 3\n";
    print $sock->getline();
    $sock->close;
    exit;
} else {
 die;
}