/
Depends on
- libio-socket-ip-perl (libio-socket-ip-perl)
- libtest-sharedfork-perl (libtest-sharedfork-perl)
- perl (perl)
- debhelper-compat
Used by
- alice 0.19-3
- ddclient 3.11.2-2.1
- feersum 1.505-1
- libanyevent-connection-perl 0.06-6
- libanyevent-connector-perl 0.04-1
- libanyevent-memcached-perl 0.08-2
- libanyevent-redis-perl 0.24-4
- libapp-termcast-perl 0.13-4
- libcache-memcached-fast-safe-perl 0.06-2
- libcpan-mini-inject-perl 1.012-1
- libdancer-logger-psgi-perl 1.0.1-4
- libdancer-perl 1.3522-2
- libdanga-socket-perl 1.62-2
- libdbd-multi-perl 1.02-3
- libfurl-perl 3.15-1
- libgearman-client-perl 2.004.015-3
- libhttp-async-perl 0.33-3
- libio-socket-timeout-perl 0.32-3
- libmastodon-client-perl 0.017-2
- libperlio-via-timeout-perl 0.32-3
Show 14 more
- libplack-app-proxy-perl 0.29-2
- libplack-perl 1.0054-1
- libplack-test-agent-perl 1.6-1
- libplack-test-externalserver-perl 0.02-3
- libpod-webserver-perl 3.11-2
- libproc-guard-perl 0.07-3
- libprotocol-http2-perl 1.13-1
- libredis-fast-perl 0.37+dfsg-1
- libredis-perl 2:2.000-1
- librole-rest-client-perl 0.23-1
- libtest-redisserver-perl 0.24-1
- libtest-unixsock-perl 0.4-2
- libtwiggy-tls-perl 0.0020-2
- libtest-redisserver-perl 0.25-1
README (full)
NAME
Test::TCP - testing TCP program
SYNOPSIS
use Test::TCP;
my $server = Test::TCP->new(
listen => 1,
code => sub {
my $socket = shift;
...
},
);
my $client = MyClient->new(host => '127.0.0.1', port => $server->port);
undef $server; # kill child process on DESTROY
If using a server that can only accept a port number, e.g. memcached:
use Test::TCP;
my $memcached = Test::TCP->new(
code => sub {
my $port = shift;
exec $bin, '-p' => $port;
die "cannot execute $bin: $!";
},
);
my $memd = Cache::Memcached->new({servers => ['127.0.0.1:' . $memcached->port]});
...
N.B.: This is vulnerable to race conditions, if another process binds to the same port after Net::EmptyPort found it available.
And functional interface is available:
use Test::TCP;
test_tcp(
listen => 1,
client => sub {
my ($port, $server_pid) = @_;
# send request to the server
},
server => sub {
my $socket = shift;
# run server, calling $socket->accept
},
);
test_tcp(
client => sub {
my ($port, $server_pid) = @_;
# send request to the server
},
server => sub {
my $port = shift;
# run server, binding to $port
},
);
DESCRIPTION
Test::TCP is a test utility to test TCP/IP-based server programs.
METHODS
-
test_tcp
Functional interface.
test_tcp( listen => 1, client => sub { my $port = shift; # send request to the server }, server => sub { my $socket = shift; # run server }, # optional host => '127.0.0.1', # specify '::1' to test using IPv6 port => 8080, max_wait => 3, # seconds );If
listenis false,serveris instead passed a port number that was free before it was called. -
wait_port
wait_port(8080);Waits for a particular port is available for connect.
Object Oriented 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->($socket)or$code->($port), depending on the value oflisten.This parameter is required.
-
$args{max_wait} : Number
Will wait for at most
$max_waitseconds before checking port.See also Net::EmptyPort.
Default: 10
-
$args{listen} : Boolean
If true, open a listening socket and pass this to the callback. Otherwise find a free port and pass the number of it to the callback.
-
-
$server->start()
Start the server process. Normally, you don't need to call this method.
-
$server->stop()
Stop the server process.
-
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 the OO 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. ... -
How do you test server program written in other languages like memcached?
You can use
exec()in child process.use strict; use warnings; use utf8; use Test::More; use Test::TCP 1.08; use File::Which; my $bin = scalar which 'memcached'; plan skip_all => 'memcached binary is not found' unless defined $bin; my $memcached = Test::TCP->new( code => sub { my $port = shift; exec $bin, '-p' => $port; die "cannot execute $bin: $!"; }, ); use Cache::Memcached; my $memd = Cache::Memcached->new({servers => ['127.0.0.1:' . $memcached->port]}); $memd->set(foo => 'bar'); is $memd->get('foo'), 'bar'; done_testing; -
How do I use address other than "127.0.0.1" for testing?
You can use the
hostparameter to specify the bind address.# let the server bind to "0.0.0.0" for testing test_tcp( client => sub { ... }, server => sub { ... }, host => '0.0.0.0', ); -
How should I write IPv6 tests?
You should use the "can_bind" in Net::EmptyPort function to check if the program can bind to the loopback address of IPv6, as well as the
hostparameter of the "test_tcp" function to specify the same address as the bind address.use Net::EmptyPort qw(can_bind); plan skip_all => "IPv6 not available" unless can_bind('::1'); test_tcp( client => sub { ... }, server => sub { ... }, host => '::1', );
AUTHOR
Tokuhiro Matsuno [email protected]
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.
| dir | debian/ |
| dir | lib/ |
| dir | t/ |
| dir | xt/ |
| file | Changes |
| file | LICENSE |
| file | MANIFEST |
| file | MANIFEST.SKIP |
| file | META.json |
| file | META.yml |
| file | Makefile.PL |
| file | README.md |