| 1 |
NAME |
| 2 |
|
| 3 |
Test::POE::Server::TCP - A POE Component providing TCP server services |
| 4 |
for test cases |
| 5 |
|
| 6 |
VERSION |
| 7 |
|
| 8 |
version 1.20 |
| 9 |
|
| 10 |
SYNOPSIS |
| 11 |
|
| 12 |
A very simple echo server with logging of requests by each client: |
| 13 |
|
| 14 |
use strict; |
| 15 |
use POE; |
| 16 |
use Test::POE::Server::TCP; |
| 17 |
|
| 18 |
POE::Session->create( |
| 19 |
package_states => [ |
| 20 |
'main' => [qw( |
| 21 |
_start |
| 22 |
testd_connected |
| 23 |
testd_disconnected |
| 24 |
testd_client_input |
| 25 |
)], |
| 26 |
], |
| 27 |
); |
| 28 |
|
| 29 |
$poe_kernel->run(); |
| 30 |
exit 0; |
| 31 |
|
| 32 |
sub _start { |
| 33 |
# Spawn the Test::POE::Server::TCP server. |
| 34 |
$_[HEAP]->{testd} = Test::POE::Server::TCP->spawn( |
| 35 |
address => '127.0.0.1', |
| 36 |
port => 0, |
| 37 |
); |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
sub testd_connected { |
| 42 |
my ($heap,$id) = @_[HEAP,ARG0]; |
| 43 |
|
| 44 |
# A client connected the unique ID is in ARG0 |
| 45 |
# Create a blank arrayref for this client on *our* heap |
| 46 |
|
| 47 |
$heap->{clients}->{ $id } = [ ]; |
| 48 |
|
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
sub testd_client_input { |
| 53 |
my ($kernel,$heap,$sender,$id,$input) = @_[KERNEL,HEAP,SENDER,ARG0,ARG1]; |
| 54 |
|
| 55 |
# The client sent us a line of input |
| 56 |
# lets store it |
| 57 |
|
| 58 |
push @{ $heap->{clients}->{ $id } }, $input; |
| 59 |
|
| 60 |
# Okay, we are an echo server so lets send it back to the client |
| 61 |
# We know the SENDER so can always obtain the server object. |
| 62 |
|
| 63 |
my $testd = $sender->get_heap(); |
| 64 |
$testd->send_to_client( $id, $input ); |
| 65 |
|
| 66 |
# Or even |
| 67 |
|
| 68 |
# $sender->get_heap()->send_to_client( $id, $input ); |
| 69 |
|
| 70 |
# Alternatively we could just post back to the SENDER |
| 71 |
|
| 72 |
# $kernel->post( $sender, 'send_to_client', $id, $input ); |
| 73 |
|
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
sub testd_disconnected { |
| 78 |
my ($heap,$id) = @_[HEAP,ARG0]; |
| 79 |
|
| 80 |
# Client disconnected for whatever reason |
| 81 |
# We need to free up our storage |
| 82 |
|
| 83 |
delete $heap->{clients}->{ $id }; |
| 84 |
|
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
Using the module in a testcase: |
| 89 |
|
| 90 |
use strict; |
| 91 |
use Test::More; |
| 92 |
use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); |
| 93 |
use Test::POE::Server::TCP; |
| 94 |
|
| 95 |
plan tests => 5; |
| 96 |
|
| 97 |
my @data = ( |
| 98 |
'This is a test', |
| 99 |
'This is another test', |
| 100 |
'This is the last test', |
| 101 |
); |
| 102 |
|
| 103 |
POE::Session->create( |
| 104 |
package_states => [ |
| 105 |
'main' => [qw( |
| 106 |
_start |
| 107 |
_sock_up |
| 108 |
_sock_fail |
| 109 |
_sock_in |
| 110 |
_sock_err |
| 111 |
testd_connected |
| 112 |
testd_disconnected |
| 113 |
testd_client_input |
| 114 |
)], |
| 115 |
], |
| 116 |
heap => { data => \@data, }, |
| 117 |
); |
| 118 |
|
| 119 |
$poe_kernel->run(); |
| 120 |
exit 0; |
| 121 |
|
| 122 |
sub _start { |
| 123 |
$_[HEAP]->{testd} = Test::POE::Server::TCP->spawn( |
| 124 |
address => '127.0.0.1', |
| 125 |
port => 0, |
| 126 |
); |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
sub testd_registered { |
| 131 |
my ($heap,$object) = @_[HEAP,ARG0]; |
| 132 |
$heap->{port} = $object->port(); |
| 133 |
$heap->{factory} = POE::Wheel::SocketFactory->new( |
| 134 |
RemoteAddress => '127.0.0.1', |
| 135 |
RemotePort => $heap->{port}, |
| 136 |
SuccessEvent => '_sock_up', |
| 137 |
FailureEvent => '_sock_fail', |
| 138 |
); |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
sub _sock_up { |
| 143 |
my ($heap,$socket) = @_[HEAP,ARG0]; |
| 144 |
delete $heap->{factory}; |
| 145 |
$heap->{socket} = POE::Wheel::ReadWrite->new( |
| 146 |
Handle => $socket, |
| 147 |
InputEvent => '_sock_in', |
| 148 |
ErrorEvent => '_sock_err', |
| 149 |
); |
| 150 |
$heap->{socket}->put( $heap->{data}->[0] ); |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
sub _sock_fail { |
| 155 |
my $heap = $_[HEAP]; |
| 156 |
delete $heap->{factory}; |
| 157 |
$heap->{testd}->shutdown(); |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
sub _sock_in { |
| 162 |
my ($heap,$input) = @_[HEAP,ARG0]; |
| 163 |
my $data = shift @{ $heap->{data} }; |
| 164 |
ok( $input eq $data, 'Data matched' ); |
| 165 |
unless ( scalar @{ $heap->{data} } ) { |
| 166 |
delete $heap->{socket}; |
| 167 |
return; |
| 168 |
} |
| 169 |
$heap->{socket}->put( $heap->{data}->[0] ); |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
sub _sock_err { |
| 174 |
delete $_[HEAP]->{socket}; |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
sub testd_connected { |
| 179 |
my ($heap,$state,$id) = @_[HEAP,STATE,ARG0]; |
| 180 |
pass($state); |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
sub testd_disconnected { |
| 185 |
pass($_[STATE]); |
| 186 |
$poe_kernel->post( $_[SENDER], 'shutdown' ); |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
sub testd_client_input { |
| 191 |
my ($sender,$id,$input) = @_[SENDER,ARG0,ARG1]; |
| 192 |
my $testd = $_[SENDER]->get_heap(); |
| 193 |
$testd->send_to_client( $id, $input ); |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
DESCRIPTION |
| 198 |
|
| 199 |
Test::POE::Server::TCP is a POE component that provides a TCP server |
| 200 |
framework for inclusion in client component test cases, instead of |
| 201 |
having to roll your own. |
| 202 |
|
| 203 |
Once registered with the component, a session will receive events |
| 204 |
related to client connects, disconnects, input and flushed output. Each |
| 205 |
of these events will refer to a unique client ID which may be used in |
| 206 |
communication with the component when sending data to the client or |
| 207 |
disconnecting a client connection. |
| 208 |
|
| 209 |
If AF_INET6 sockets are supported the component with create an AF_INET |
| 210 |
and an AF_INET6 socket. |
| 211 |
|
| 212 |
CONSTRUCTOR |
| 213 |
|
| 214 |
spawn |
| 215 |
|
| 216 |
Takes a number of optional arguments: |
| 217 |
|
| 218 |
'alias', set an alias on the component; |
| 219 |
'address', bind the listening socket to a particular address; |
| 220 |
'port', listen on a particular port, default is 0, assign a random port; |
| 221 |
'options', a hashref of POE::Session options; |
| 222 |
'filter', specify a POE::Filter to use for client connections, default is POE::Filter::Line; |
| 223 |
'inputfilter', specify a POE::Filter for client input; |
| 224 |
'outputfilter', specify a POE::Filter for output to clients; |
| 225 |
'prefix', specify a different prefix than 'testd' for events; |
| 226 |
|
| 227 |
The semantics for filter, inputfilter and outputfilter are the same |
| 228 |
as for POE::Component::Server::TCP in that one may provide either a |
| 229 |
SCALAR, ARRAYREF or an OBJECT. |
| 230 |
|
| 231 |
If the component is spawned within another session it will |
| 232 |
automatically register the parent session to receive all events. |
| 233 |
|
| 234 |
METHODS |
| 235 |
|
| 236 |
session_id |
| 237 |
|
| 238 |
Returns the POE::Session ID of the component. |
| 239 |
|
| 240 |
shutdown |
| 241 |
|
| 242 |
Terminates the component. Shuts down the listener and disconnects |
| 243 |
connected clients. |
| 244 |
|
| 245 |
send_to_client |
| 246 |
|
| 247 |
Send some output to a connected client. First parameter must be a |
| 248 |
valid client id. Second parameter is a string of text to send. The |
| 249 |
second parameter may also be an arrayref of items to send to the |
| 250 |
client. If the filter you have used requires an arrayref as input, |
| 251 |
nest that arrayref within another arrayref. |
| 252 |
|
| 253 |
send_to_all_clients |
| 254 |
|
| 255 |
Send some output to all connected clients. The parameter is a string |
| 256 |
of text to send. The parameter may also be an arrayref of items to |
| 257 |
send to the clients. If the filter you have used requires an arrayref |
| 258 |
as input, nest that arrayref within another arrayref. |
| 259 |
|
| 260 |
client_info |
| 261 |
|
| 262 |
Retrieve socket information of a given client. Requires a valid |
| 263 |
client ID as a parameter. If called in a list context it returns a |
| 264 |
list consisting of, in order, the client address, the client TCP |
| 265 |
port, our address and our TCP port. In a scalar context it returns a |
| 266 |
HASHREF with the following keys: |
| 267 |
|
| 268 |
'peeraddr', the client address; |
| 269 |
'peerport', the client TCP port; |
| 270 |
'sockaddr', our address; |
| 271 |
'sockport', our TCP port; |
| 272 |
|
| 273 |
client_wheel |
| 274 |
|
| 275 |
Retrieve the POE::Wheel::ReadWrite object of a given client. Requires |
| 276 |
a valid client ID as a parameter. This enables one to manipulate the |
| 277 |
given POE::Wheel::ReadWrite object, say to switch POE::Filter. |
| 278 |
|
| 279 |
disconnect |
| 280 |
|
| 281 |
Places a client connection in pending disconnect state. Requires a |
| 282 |
valid client ID as a parameter. Set this, then send an applicable |
| 283 |
message to the client using send_to_client() and the client |
| 284 |
connection will be terminated. |
| 285 |
|
| 286 |
terminate |
| 287 |
|
| 288 |
Immediately disconnects a client conenction. Requires a valid client |
| 289 |
ID as a parameter. |
| 290 |
|
| 291 |
pause_listening |
| 292 |
|
| 293 |
Stops the underlying listening socket from accepting new connections. |
| 294 |
This lets you test whether you handle the connection timing out |
| 295 |
gracefully. |
| 296 |
|
| 297 |
resume_listening |
| 298 |
|
| 299 |
The companion of pause_listening |
| 300 |
|
| 301 |
getsockname |
| 302 |
|
| 303 |
Access to the POE::Wheel::SocketFactory method of the underlying |
| 304 |
listening AF_INET socket. |
| 305 |
|
| 306 |
port |
| 307 |
|
| 308 |
Returns the port that the component is listening on. |
| 309 |
|
| 310 |
getsockname6 |
| 311 |
|
| 312 |
Access to the POE::Wheel::SocketFactory method of the underlying |
| 313 |
listening AF_INET6 socket. |
| 314 |
|
| 315 |
port6 |
| 316 |
|
| 317 |
Returns the port that the component is listening on for AF_INET6. |
| 318 |
|
| 319 |
start_listener |
| 320 |
|
| 321 |
If the listener fails on listen you can attempt to restart it with |
| 322 |
this. |
| 323 |
|
| 324 |
INPUT EVENTS |
| 325 |
|
| 326 |
These are events that the component will accept: |
| 327 |
|
| 328 |
register |
| 329 |
|
| 330 |
Takes N arguments: a list of event names that your session wants to |
| 331 |
listen for, minus the 'testd_' prefix. |
| 332 |
|
| 333 |
Registering for 'all' will cause it to send all TESTD-related events |
| 334 |
to you; this is the easiest way to handle it. |
| 335 |
|
| 336 |
unregister |
| 337 |
|
| 338 |
Takes N arguments: a list of event names which you don't want to |
| 339 |
receive. If you've previously done a 'register' for a particular |
| 340 |
event which you no longer care about, this event will tell the POP3D |
| 341 |
to stop sending them to you. (If you haven't, it just ignores you. No |
| 342 |
big deal). |
| 343 |
|
| 344 |
shutdown |
| 345 |
|
| 346 |
Terminates the component. Shuts down the listener and disconnects |
| 347 |
connected clients. |
| 348 |
|
| 349 |
send_to_client |
| 350 |
|
| 351 |
Send some output to a connected client. First parameter must be a |
| 352 |
valid client id. Second parameter is a string of text to send. The |
| 353 |
second parameter may also be an arrayref of items to send to the |
| 354 |
client. If the filter you have used requires an arrayref as input, |
| 355 |
nest that arrayref within another arrayref. |
| 356 |
|
| 357 |
send_to_all_clients |
| 358 |
|
| 359 |
Send some output to all connected clients. The parameter is a string |
| 360 |
of text to send. The parameter may also be an arrayref of items to |
| 361 |
send to the clients. If the filter you have used requires an arrayref |
| 362 |
as input, nest that arrayref within another arrayref. |
| 363 |
|
| 364 |
disconnect |
| 365 |
|
| 366 |
Places a client connection in pending disconnect state. Requires a |
| 367 |
valid client ID as a parameter. Set this, then send an applicable |
| 368 |
message to the client using send_to_client() and the client |
| 369 |
connection will be terminated. |
| 370 |
|
| 371 |
terminate |
| 372 |
|
| 373 |
Immediately disconnects a client conenction. Requires a valid client |
| 374 |
ID as a parameter. |
| 375 |
|
| 376 |
start_listener |
| 377 |
|
| 378 |
If the listener fails on listen you can attempt to restart it with |
| 379 |
this. |
| 380 |
|
| 381 |
OUTPUT EVENTS |
| 382 |
|
| 383 |
The component sends the following events to registered sessions. If you |
| 384 |
have changed the prefix option in spawn then substitute testd with the |
| 385 |
event prefix that you specified. |
| 386 |
|
| 387 |
testd_registered |
| 388 |
|
| 389 |
This event is sent to a registering session. ARG0 is the |
| 390 |
Test::POE::Server::TCP object. |
| 391 |
|
| 392 |
testd_listener_failed |
| 393 |
|
| 394 |
Generated if the component cannot either start a listener or there is |
| 395 |
a problem accepting client connections. ARG0 contains the name of the |
| 396 |
operation that failed. ARG1 and ARG2 hold numeric and string values |
| 397 |
for $!, respectively. |
| 398 |
|
| 399 |
If the operation was listen, the component will remove the listener. |
| 400 |
You may attempt to start it again using start_listener. |
| 401 |
|
| 402 |
testd_connected |
| 403 |
|
| 404 |
Generated whenever a client connects to the component. ARG0 is the |
| 405 |
client ID, ARG1 is the client's IP address, ARG2 is the client's TCP |
| 406 |
port. ARG3 is our IP address and ARG4 is our socket port. |
| 407 |
|
| 408 |
testd_disconnected |
| 409 |
|
| 410 |
Generated whenever a client disconnects. ARG0 was the client ID, ARG1 |
| 411 |
was the client's IP address, ARG2 was the client's TCP port. ARG3 was |
| 412 |
our IP address and ARG4 was our socket port. |
| 413 |
|
| 414 |
testd_client_input |
| 415 |
|
| 416 |
Generated whenever a client sends us some traffic. ARG0 is the client |
| 417 |
ID, ARG1 is the data sent ( tokenised by whatever POE::Filter you |
| 418 |
specified ). |
| 419 |
|
| 420 |
testd_client_flushed |
| 421 |
|
| 422 |
Generated whenever anything we send to the client is actually flushed |
| 423 |
down the 'line'. ARG0 is the client ID. |
| 424 |
|
| 425 |
CREDITS |
| 426 |
|
| 427 |
This module uses code borrowed from POE::Component::Server::TCP by |
| 428 |
Rocco Caputo, Ann Barcomb and Jos Boumans. |
| 429 |
|
| 430 |
SEE ALSO |
| 431 |
|
| 432 |
POE |
| 433 |
|
| 434 |
POE::Component::Server::TCP |
| 435 |
|
| 436 |
AUTHOR |
| 437 |
|
| 438 |
Chris Williams <[email protected]> |
| 439 |
|
| 440 |
COPYRIGHT AND LICENSE |
| 441 |
|
| 442 |
This software is copyright (c) 2016 by Chris Williams, Rocco Caputo, |
| 443 |
Ann Barcomb and Jos Boumans. |
| 444 |
|
| 445 |
This is free software; you can redistribute it and/or modify it under |
| 446 |
the same terms as the Perl 5 programming language system itself. |
| 447 |
|