File: accept.t

package info (click to toggle)
libio-all-perl 0.87-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 712 kB
  • sloc: perl: 2,017; makefile: 5
file content (85 lines) | stat: -rw-r--r-- 1,761 bytes parent folder | download | duplicates (4)
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
78
79
80
81
82
83
84
85
use strict; use warnings;
use lib -e 't' ? 't' : 'test';
use Test::More tests => 20;
use IO_All_Test;
use IO::All;
use IO::Socket::INET;

# This test tests for the ability of a non-forking socket to handle more
# than one connection.

my $pid = fork();
if (! $pid)
{
    # Let the child process listen on a port
    my $port = 5555;
    my $accepted = 0;
    my $start = time;
    while (1)
    {
        # Log the port to a file.
        open my $out, ">", o_dir() . "/server-port.t";
        print {$out} $port;
        close($out);

        my $server = io("localhost:$port");

        eval {
            for my $count (1 .. 10)
            {
                my $connection = $server->accept();
                $accepted = 1;
                $connection->print(sprintf("Ingy-%.2d", $count));
                $connection->close();
            }
        };
        if ($accepted)
        {
            # We have a listening socket on a port, so we can continue
            last;
        }
        last if time > $start + 10
    }
    continue
    {
        # Try a different port.
        $port++;
    }
    exit(0);
}
# Let the parent process handle the testing.

# Wait a little for the client to find a port.
sleep(1);

open my $in, "<", o_dir() . "/server-port.t";
my $port = <$in>;
close($in);

# TEST*2*10
for my $c (1 .. 10)
{
    my $sock = IO::Socket::INET->new(
        PeerAddr => "localhost",
        PeerPort => $port,
        Proto => "tcp"
    );

    ok(defined($sock), "Checking for validity of sock No. $c");

    if (!defined($sock))
    {
        last;
    }

    my $data;
    $sock->recv($data, 7);

    $sock->close();

    is ($data, sprintf("Ingy-%.2d", $c), "Checking for connection No. $c.");
}

waitpid($pid, 0);

del_output_dir();