Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Test::TCP for perl
Perl

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib/Test
t
xt
.gitignore
.shipit
Changes
MANIFEST
MANIFEST.SKIP
Makefile.PL
README
README.pod

README.pod

NAME

Test::TCP - testing TCP program

SYNOPSIS

    use Test::TCP;
    test_tcp(
        client => sub {
            my ($port, $server_pid) = @_;
            # send request to the server
        },
        server => sub {
            my $port = shift;
            # run server
        },
    );

using other server program

    use Test::TCP;
    test_tcp(
        client => sub {
            my $port = shift;
            # send request to the server
        },
        server => sub {
            exec '/foo/bar/bin/server', 'options';
        },
    );

Or, OO-ish interface

    use Test::TCP;

    my $server = Test::TCP->new(
        code => sub {
            my $port = shift;
            ...
        },
    );
    my $client = MyClient->new(host => '127.0.0.1', port => $server->port);
    undef $server; # kill child process on DESTROY

DESCRIPTION

Test::TCP is test utilities for TCP/IP program.

METHODS

empty_port
    my $port = empty_port();

Get the available port number, you can use.

test_tcp
    test_tcp(
        client => sub {
            my $port = shift;
            # send request to the server
        },
        server => sub {
            my $port = shift;
            # run server
        },
        # optional
        port => 8080
    );
wait_port
    wait_port(8080);

Waits for a particular port is available for connect.

OO-ish interface

my $server = Test::TCP->new(%args);

Create new instance of Test::TCP.

Arguments are following:

$args{auto_start}: Boolean

Call $server->start() after create instance.

Default: true

$args{code}: CodeRef

The callback function. Argument for callback function is: $code->($pid).

This parameter is required.

$server->start()

Start the server process. Normally, you don't need to call this method.

my $pid = $server->pid();

Get the pid of child process.

my $port = $server->port();

Get the port number of child process.

FAQ

How to invoke two servers?

You can call test_tcp() twice!

    test_tcp(
        client => sub {
            my $port1 = shift;
            test_tcp(
                client => sub {
                    my $port2 = shift;
                    # some client code here
                },
                server => sub {
                    my $port2 = shift;
                    # some server2 code here
                },
            );
        },
        server => sub {
            my $port1 = shift;
            # some server1 code here
        },
    );

Or use OO-ish interface instead.

    my $server1 = Test::TCP->new(code => sub {
        my $port1 = shift;
        ...
    });
    my $server2 = Test::TCP->new(code => sub {
        my $port2 = shift;
        ...
    });

    # your client code here.
    ...

AUTHOR

Tokuhiro Matsuno <tokuhirom@gmail.com>

THANKS TO

kazuhooku

dragon3

charsbar

Tatsuhiko Miyagawa

lestrrat

SEE ALSO

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Something went wrong with that request. Please try again.