1Test::TCP(3)          User Contributed Perl Documentation         Test::TCP(3)
2
3
4

NAME

6       Test::TCP - testing TCP program
7

SYNOPSIS

9           use Test::TCP;
10
11           my $server = Test::TCP->new(
12               code => sub {
13                   my $port = shift;
14                   ...
15               },
16           );
17           my $client = MyClient->new(host => '127.0.0.1', port => $server->port);
18           undef $server; # kill child process on DESTROY
19
20       Using memcached:
21
22           use Test::TCP;
23
24           my $memcached = Test::TCP->new(
25               code => sub {
26                   my $port = shift;
27
28                   exec $bin, '-p' => $port;
29                   die "cannot execute $bin: $!";
30               },
31           );
32           my $memd = Cache::Memcached->new({servers => ['127.0.0.1:' . $memcached->port]});
33           ...
34
35       And functional interface is available:
36
37           use Test::TCP;
38           test_tcp(
39               client => sub {
40                   my ($port, $server_pid) = @_;
41                   # send request to the server
42               },
43               server => sub {
44                   my $port = shift;
45                   # run server
46               },
47           );
48

DESCRIPTION

50       Test::TCP is test utilities for TCP/IP programs.
51

METHODS

53       empty_port
54               my $port = empty_port();
55
56           Get the available port number, you can use.
57
58       test_tcp
59           Functional interface.
60
61               test_tcp(
62                   client => sub {
63                       my $port = shift;
64                       # send request to the server
65                   },
66                   server => sub {
67                       my $port = shift;
68                       # run server
69                   },
70                   # optional
71                   port => 8080
72               );
73
74       wait_port
75               wait_port(8080);
76
77           Waits for a particular port is available for connect.
78

OO-ish interface

80       my $server = Test::TCP->new(%args);
81           Create new instance of Test::TCP.
82
83           Arguments are following:
84
85           $args{auto_start}: Boolean
86               Call "$server->start()" after create instance.
87
88               Default: true
89
90           $args{code}: CodeRef
91               The callback function. Argument for callback function is:
92               "$code->($pid)".
93
94               This parameter is required.
95
96       $server->start()
97           Start the server process. Normally, you don't need to call this
98           method.
99
100       $server->stop()
101           Stop the server process.
102
103       my $pid = $server->pid();
104           Get the pid of child process.
105
106       my $port = $server->port();
107           Get the port number of child process.
108

FAQ

110       How to invoke two servers?
111           You can call test_tcp() twice!
112
113               test_tcp(
114                   client => sub {
115                       my $port1 = shift;
116                       test_tcp(
117                           client => sub {
118                               my $port2 = shift;
119                               # some client code here
120                           },
121                           server => sub {
122                               my $port2 = shift;
123                               # some server2 code here
124                           },
125                       );
126                   },
127                   server => sub {
128                       my $port1 = shift;
129                       # some server1 code here
130                   },
131               );
132
133           Or use OO-ish interface instead.
134
135               my $server1 = Test::TCP->new(code => sub {
136                   my $port1 = shift;
137                   ...
138               });
139               my $server2 = Test::TCP->new(code => sub {
140                   my $port2 = shift;
141                   ...
142               });
143
144               # your client code here.
145               ...
146
147       How do you test server program written in other languages like
148       memcached?
149           You can use "exec()" in child process.
150
151               use strict;
152               use warnings;
153               use utf8;
154               use Test::More;
155               use Test::TCP 1.08;
156               use File::Which;
157
158               my $bin = scalar which 'memcached';
159               plan skip_all => 'memcached binary is not found' unless defined $bin;
160
161               my $memcached = Test::TCP->new(
162                   code => sub {
163                       my $port = shift;
164
165                       exec $bin, '-p' => $port;
166                       die "cannot execute $bin: $!";
167                   },
168               );
169
170               use Cache::Memcached;
171               my $memd = Cache::Memcached->new({servers => ['127.0.0.1:' . $memcached->port]});
172               $memd->set(foo => 'bar');
173               is $memd->get('foo'), 'bar';
174
175               done_testing;
176

AUTHOR

178       Tokuhiro Matsuno <tokuhirom@gmail.com>
179

THANKS TO

181       kazuhooku
182
183       dragon3
184
185       charsbar
186
187       Tatsuhiko Miyagawa
188
189       lestrrat
190

SEE ALSO

LICENSE

193       This library is free software; you can redistribute it and/or modify it
194       under the same terms as Perl itself.
195
196
197
198perl v5.12.3                      2011-06-01                      Test::TCP(3)
Impressum