Test-POE-Server-TCP-1.20000755001750001750 012706430410 14606 5ustar00bingosbingos000000000000README100644001750001750 3205112706430410 15570 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20NAME Test::POE::Server::TCP - A POE Component providing TCP server services for test cases VERSION version 1.20 SYNOPSIS A very simple echo server with logging of requests by each client: use strict; use POE; use Test::POE::Server::TCP; POE::Session->create( package_states => [ 'main' => [qw( _start testd_connected testd_disconnected testd_client_input )], ], ); $poe_kernel->run(); exit 0; sub _start { # Spawn the Test::POE::Server::TCP server. $_[HEAP]->{testd} = Test::POE::Server::TCP->spawn( address => '127.0.0.1', port => 0, ); return; } sub testd_connected { my ($heap,$id) = @_[HEAP,ARG0]; # A client connected the unique ID is in ARG0 # Create a blank arrayref for this client on *our* heap $heap->{clients}->{ $id } = [ ]; return; } sub testd_client_input { my ($kernel,$heap,$sender,$id,$input) = @_[KERNEL,HEAP,SENDER,ARG0,ARG1]; # The client sent us a line of input # lets store it push @{ $heap->{clients}->{ $id } }, $input; # Okay, we are an echo server so lets send it back to the client # We know the SENDER so can always obtain the server object. my $testd = $sender->get_heap(); $testd->send_to_client( $id, $input ); # Or even # $sender->get_heap()->send_to_client( $id, $input ); # Alternatively we could just post back to the SENDER # $kernel->post( $sender, 'send_to_client', $id, $input ); return; } sub testd_disconnected { my ($heap,$id) = @_[HEAP,ARG0]; # Client disconnected for whatever reason # We need to free up our storage delete $heap->{clients}->{ $id }; return; } Using the module in a testcase: use strict; use Test::More; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use Test::POE::Server::TCP; plan tests => 5; my @data = ( 'This is a test', 'This is another test', 'This is the last test', ); POE::Session->create( package_states => [ 'main' => [qw( _start _sock_up _sock_fail _sock_in _sock_err testd_connected testd_disconnected testd_client_input )], ], heap => { data => \@data, }, ); $poe_kernel->run(); exit 0; sub _start { $_[HEAP]->{testd} = Test::POE::Server::TCP->spawn( address => '127.0.0.1', port => 0, ); return; } sub testd_registered { my ($heap,$object) = @_[HEAP,ARG0]; $heap->{port} = $object->port(); $heap->{factory} = POE::Wheel::SocketFactory->new( RemoteAddress => '127.0.0.1', RemotePort => $heap->{port}, SuccessEvent => '_sock_up', FailureEvent => '_sock_fail', ); return; } sub _sock_up { my ($heap,$socket) = @_[HEAP,ARG0]; delete $heap->{factory}; $heap->{socket} = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{socket}->put( $heap->{data}->[0] ); return; } sub _sock_fail { my $heap = $_[HEAP]; delete $heap->{factory}; $heap->{testd}->shutdown(); return; } sub _sock_in { my ($heap,$input) = @_[HEAP,ARG0]; my $data = shift @{ $heap->{data} }; ok( $input eq $data, 'Data matched' ); unless ( scalar @{ $heap->{data} } ) { delete $heap->{socket}; return; } $heap->{socket}->put( $heap->{data}->[0] ); return; } sub _sock_err { delete $_[HEAP]->{socket}; return; } sub testd_connected { my ($heap,$state,$id) = @_[HEAP,STATE,ARG0]; pass($state); return; } sub testd_disconnected { pass($_[STATE]); $poe_kernel->post( $_[SENDER], 'shutdown' ); return; } sub testd_client_input { my ($sender,$id,$input) = @_[SENDER,ARG0,ARG1]; my $testd = $_[SENDER]->get_heap(); $testd->send_to_client( $id, $input ); return; } DESCRIPTION Test::POE::Server::TCP is a POE component that provides a TCP server framework for inclusion in client component test cases, instead of having to roll your own. Once registered with the component, a session will receive events related to client connects, disconnects, input and flushed output. Each of these events will refer to a unique client ID which may be used in communication with the component when sending data to the client or disconnecting a client connection. If AF_INET6 sockets are supported the component with create an AF_INET and an AF_INET6 socket. CONSTRUCTOR spawn Takes a number of optional arguments: 'alias', set an alias on the component; 'address', bind the listening socket to a particular address; 'port', listen on a particular port, default is 0, assign a random port; 'options', a hashref of POE::Session options; 'filter', specify a POE::Filter to use for client connections, default is POE::Filter::Line; 'inputfilter', specify a POE::Filter for client input; 'outputfilter', specify a POE::Filter for output to clients; 'prefix', specify a different prefix than 'testd' for events; The semantics for filter, inputfilter and outputfilter are the same as for POE::Component::Server::TCP in that one may provide either a SCALAR, ARRAYREF or an OBJECT. If the component is spawned within another session it will automatically register the parent session to receive all events. METHODS session_id Returns the POE::Session ID of the component. shutdown Terminates the component. Shuts down the listener and disconnects connected clients. send_to_client Send some output to a connected client. First parameter must be a valid client id. Second parameter is a string of text to send. The second parameter may also be an arrayref of items to send to the client. If the filter you have used requires an arrayref as input, nest that arrayref within another arrayref. send_to_all_clients Send some output to all connected clients. The parameter is a string of text to send. The parameter may also be an arrayref of items to send to the clients. If the filter you have used requires an arrayref as input, nest that arrayref within another arrayref. client_info Retrieve socket information of a given client. Requires a valid client ID as a parameter. If called in a list context it returns a list consisting of, in order, the client address, the client TCP port, our address and our TCP port. In a scalar context it returns a HASHREF with the following keys: 'peeraddr', the client address; 'peerport', the client TCP port; 'sockaddr', our address; 'sockport', our TCP port; client_wheel Retrieve the POE::Wheel::ReadWrite object of a given client. Requires a valid client ID as a parameter. This enables one to manipulate the given POE::Wheel::ReadWrite object, say to switch POE::Filter. disconnect Places a client connection in pending disconnect state. Requires a valid client ID as a parameter. Set this, then send an applicable message to the client using send_to_client() and the client connection will be terminated. terminate Immediately disconnects a client conenction. Requires a valid client ID as a parameter. pause_listening Stops the underlying listening socket from accepting new connections. This lets you test whether you handle the connection timing out gracefully. resume_listening The companion of pause_listening getsockname Access to the POE::Wheel::SocketFactory method of the underlying listening AF_INET socket. port Returns the port that the component is listening on. getsockname6 Access to the POE::Wheel::SocketFactory method of the underlying listening AF_INET6 socket. port6 Returns the port that the component is listening on for AF_INET6. start_listener If the listener fails on listen you can attempt to restart it with this. INPUT EVENTS These are events that the component will accept: register Takes N arguments: a list of event names that your session wants to listen for, minus the 'testd_' prefix. Registering for 'all' will cause it to send all TESTD-related events to you; this is the easiest way to handle it. unregister Takes N arguments: a list of event names which you don't want to receive. If you've previously done a 'register' for a particular event which you no longer care about, this event will tell the POP3D to stop sending them to you. (If you haven't, it just ignores you. No big deal). shutdown Terminates the component. Shuts down the listener and disconnects connected clients. send_to_client Send some output to a connected client. First parameter must be a valid client id. Second parameter is a string of text to send. The second parameter may also be an arrayref of items to send to the client. If the filter you have used requires an arrayref as input, nest that arrayref within another arrayref. send_to_all_clients Send some output to all connected clients. The parameter is a string of text to send. The parameter may also be an arrayref of items to send to the clients. If the filter you have used requires an arrayref as input, nest that arrayref within another arrayref. disconnect Places a client connection in pending disconnect state. Requires a valid client ID as a parameter. Set this, then send an applicable message to the client using send_to_client() and the client connection will be terminated. terminate Immediately disconnects a client conenction. Requires a valid client ID as a parameter. start_listener If the listener fails on listen you can attempt to restart it with this. OUTPUT EVENTS The component sends the following events to registered sessions. If you have changed the prefix option in spawn then substitute testd with the event prefix that you specified. testd_registered This event is sent to a registering session. ARG0 is the Test::POE::Server::TCP object. testd_listener_failed Generated if the component cannot either start a listener or there is a problem accepting client connections. ARG0 contains the name of the operation that failed. ARG1 and ARG2 hold numeric and string values for $!, respectively. If the operation was listen, the component will remove the listener. You may attempt to start it again using start_listener. testd_connected Generated whenever a client connects to the component. ARG0 is the client ID, ARG1 is the client's IP address, ARG2 is the client's TCP port. ARG3 is our IP address and ARG4 is our socket port. testd_disconnected Generated whenever a client disconnects. ARG0 was the client ID, ARG1 was the client's IP address, ARG2 was the client's TCP port. ARG3 was our IP address and ARG4 was our socket port. testd_client_input Generated whenever a client sends us some traffic. ARG0 is the client ID, ARG1 is the data sent ( tokenised by whatever POE::Filter you specified ). testd_client_flushed Generated whenever anything we send to the client is actually flushed down the 'line'. ARG0 is the client ID. CREDITS This module uses code borrowed from POE::Component::Server::TCP by Rocco Caputo, Ann Barcomb and Jos Boumans. SEE ALSO POE POE::Component::Server::TCP AUTHOR Chris Williams COPYRIGHT AND LICENSE This software is copyright (c) 2016 by Chris Williams, Rocco Caputo, Ann Barcomb and Jos Boumans. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. LICENSE100644001750001750 4406412706430410 15724 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20This software is copyright (c) 2016 by Chris Williams, Rocco Caputo, Ann Barcomb and Jos Boumans. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2016 by Chris Williams, Rocco Caputo, Ann Barcomb and Jos Boumans. This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2016 by Chris Williams, Rocco Caputo, Ann Barcomb and Jos Boumans. This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End Changes100644001750001750 274512706430410 16172 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20================================================== Changes from 2011-04-24 00:00:00 +0000 to present. ================================================== ----------------------------------------- version 1.20 at 2016-04-22 14:10:51 +0000 ----------------------------------------- Change: 9aea167dd7998ff16039b0bd6d80286d530037f8 Author: Chris 'BinGOs' Williams Date : 2016-04-22 15:10:51 +0000 Support IPv6 ----------------------------------------- version 1.18 at 2014-12-14 19:04:59 +0000 ----------------------------------------- Change: 80e33094eb45214e39d5aaf25a163751f3649730 Author: Chris 'BinGOs' Williams Date : 2014-12-14 19:04:59 +0000 Correct the code in the first SYNPOSIS ----------------------------------------- version 1.16 at 2011-06-29 09:03:36 +0000 ----------------------------------------- Change: e4ffebf034a67b0a7b6f3ead2bd9bad38c512ed0 Author: Chris 'BinGOs' Williams Date : 2011-06-29 10:03:36 +0000 Resolve [rt.cpan.org #69175] "Test-POE-Server-TCP-1.14 stuck in test on Strawberry Perl 5.12.3" Made the explicit socket shutdown for 'cygwin' applicable to MSWin32 as well. I was able to reproduce the issue with: This is perl 5, version 14, subversion 1 (v5.14.1) built for MSWin32-x64-multi-thread ================================================ Plus 8 releases after 2011-04-24 00:00:00 +0000. ================================================ dist.ini100644001750001750 62512706430410 16316 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20name = Test-POE-Server-TCP version = 1.20 author = Chris Williams license = Perl_5 copyright_holder = Chris Williams, Rocco Caputo, Ann Barcomb and Jos Boumans [@BINGOS] [Prereqs / BuildRequires] Test::More = 0.47 Text::ParseWords = 0 [Prereqs] POE = 1.004 POE::Filter = 0 POE::Filter::Line = 0 POE::Wheel::ReadWrite = 0 POE::Wheel::SocketFactory = 0 Socket = 2.0 perl = v5.6.0 META.yml100644001750001750 150212706430410 16136 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20--- abstract: 'A POE Component providing TCP server services for test cases' author: - 'Chris Williams ' build_requires: File::Spec: '0' IO::Handle: '0' IPC::Open3: '0' Test::More: '0.47' Text::ParseWords: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 0 generated_by: 'Dist::Zilla version 5.045, CPAN::Meta::Converter version 2.150005' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Test-POE-Server-TCP requires: POE: '1.004' POE::Filter: '0' POE::Filter::Line: '0' POE::Wheel::ReadWrite: '0' POE::Wheel::SocketFactory: '0' Socket: '2.0' perl: v5.6.0 resources: homepage: https://github.com/bingos/test-poe-server-tcp repository: https://github.com/bingos/test-poe-server-tcp.git version: '1.20' MANIFEST100644001750001750 70012706430410 15775 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20# This file was automatically generated by Dist::Zilla::Plugin::Manifest v5.045. Changes LICENSE MANIFEST META.json META.yml Makefile.PL README dist.ini examples/synopsis.pl lib/Test/POE/Server/TCP.pm t/00-compile.t t/01_spawn.t t/02_register.t t/03_register.t t/04_multiple.t t/05_subclass.t t/06_filter.t t/07_synopsis.t t/08_buffer.t t/09_client_info.t t/10_prefix.t t/11_pause.t t/12_all_clients.t t/author-pod-coverage.t t/author-pod-syntax.t META.json100644001750001750 331712706430410 16314 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20{ "abstract" : "A POE Component providing TCP server services for test cases", "author" : [ "Chris Williams " ], "dynamic_config" : 0, "generated_by" : "Dist::Zilla version 5.045, CPAN::Meta::Converter version 2.150005", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "Test-POE-Server-TCP", "prereqs" : { "build" : { "requires" : { "Test::More" : "0.47", "Text::ParseWords" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "develop" : { "requires" : { "Pod::Coverage::TrustPod" : "0", "Test::Pod" : "1.41", "Test::Pod::Coverage" : "1.08" } }, "runtime" : { "requires" : { "POE" : "1.004", "POE::Filter" : "0", "POE::Filter::Line" : "0", "POE::Wheel::ReadWrite" : "0", "POE::Wheel::SocketFactory" : "0", "Socket" : "2.0", "perl" : "v5.6.0" } }, "test" : { "requires" : { "File::Spec" : "0", "IO::Handle" : "0", "IPC::Open3" : "0", "Test::More" : "0.47" } } }, "release_status" : "stable", "resources" : { "homepage" : "https://github.com/bingos/test-poe-server-tcp", "repository" : { "type" : "git", "url" : "https://github.com/bingos/test-poe-server-tcp.git", "web" : "https://github.com/bingos/test-poe-server-tcp" } }, "version" : "1.20" } Makefile.PL100644001750001750 313512706430410 16643 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v5.045. use strict; use warnings; use 5.006000; use ExtUtils::MakeMaker; my %WriteMakefileArgs = ( "ABSTRACT" => "A POE Component providing TCP server services for test cases", "AUTHOR" => "Chris Williams ", "BUILD_REQUIRES" => { "Test::More" => "0.47", "Text::ParseWords" => 0 }, "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => 0 }, "DISTNAME" => "Test-POE-Server-TCP", "LICENSE" => "perl", "MIN_PERL_VERSION" => "5.006000", "NAME" => "Test::POE::Server::TCP", "PREREQ_PM" => { "POE" => "1.004", "POE::Filter" => 0, "POE::Filter::Line" => 0, "POE::Wheel::ReadWrite" => 0, "POE::Wheel::SocketFactory" => 0, "Socket" => "2.0" }, "TEST_REQUIRES" => { "File::Spec" => 0, "IO::Handle" => 0, "IPC::Open3" => 0, "Test::More" => "0.47" }, "VERSION" => "1.20", "test" => { "TESTS" => "t/*.t" } ); my %FallbackPrereqs = ( "File::Spec" => 0, "IO::Handle" => 0, "IPC::Open3" => 0, "POE" => "1.004", "POE::Filter" => 0, "POE::Filter::Line" => 0, "POE::Wheel::ReadWrite" => 0, "POE::Wheel::SocketFactory" => 0, "Socket" => "2.0", "Test::More" => "0.47", "Text::ParseWords" => 0 ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { delete $WriteMakefileArgs{TEST_REQUIRES}; delete $WriteMakefileArgs{BUILD_REQUIRES}; $WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs; } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); t000755001750001750 012706430410 14772 5ustar00bingosbingos000000000000Test-POE-Server-TCP-1.2001_spawn.t100644001750001750 442012706430410 16747 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20/tuse strict; use Socket; use Test::More; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use Test::POE::Server::TCP; my %data = ( tests => [ [ '+OK' => 'cock' ], [ '-ERR' => 'quit' ], ], ); plan tests => 10; POE::Session->create( package_states => [ 'main' => [qw( _start _sock_up _sock_fail _sock_in _sock_err testd_registered testd_connected testd_disconnected testd_client_input )], ], heap => \%data, ); $poe_kernel->run(); exit 0; sub _start { $_[HEAP]->{testd} = Test::POE::Server::TCP->spawn( address => '127.0.0.1', port => 0, options => { trace => 0 }, ); isa_ok( $_[HEAP]->{testd}, 'Test::POE::Server::TCP' ); return; } sub testd_registered { my ($heap,$object) = @_[HEAP,ARG0]; isa_ok( $object, 'Test::POE::Server::TCP' ); $heap->{port} = $object->port(); $heap->{factory} = POE::Wheel::SocketFactory->new( RemoteAddress => '127.0.0.1', RemotePort => $heap->{port}, SuccessEvent => '_sock_up', FailureEvent => '_sock_fail', ); return; } sub _sock_up { my ($heap,$socket) = @_[HEAP,ARG0]; delete $heap->{factory}; $heap->{socket} = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); return; } sub _sock_fail { my $heap = $_[HEAP]; delete $heap->{factory}; $heap->{testd}->shutdown(); return; } sub _sock_in { my ($heap,$input) = @_[HEAP,ARG0]; my @parms = split /\s+/, $input; my $test = shift @{ $heap->{tests} }; if ( $test and $test->[0] eq $parms[0] ) { pass($input); $heap->{socket}->put( $test->[1] ); return; } pass($input); return; } sub _sock_err { delete $_[HEAP]->{socket}; pass("Disconnected"); $_[HEAP]->{testd}->shutdown(); return; } sub testd_connected { my ($heap,$state,$id) = @_[HEAP,STATE,ARG0]; $heap->{testd}->send_to_client( $id, '+OK POP3 Fakepop 6.9 server ready' ); pass($state); return; } sub testd_disconnected { pass($_[STATE]); return; } sub testd_client_input { my ($sender,$id,$input) = @_[SENDER,ARG0,ARG1]; my $testd = $_[SENDER]->get_heap(); pass($_[STATE]); if ( $input eq 'quit' ) { $testd->disconnect( $id ); $testd->send_to_client( $id, '+OK POP3 server signing off' ); return; } $testd->send_to_client( $id, '-ERR' ); return; } 11_pause.t100644001750001750 435112706430410 16740 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20/tuse strict; use Socket; use Test::More; use POE qw(Wheel::SocketFactory Wheel::ReadWrite); use Test::POE::Server::TCP; plan tests => 4; POE::Session->create( package_states => [ 'main' => [qw( _start _sock_up _sock_fail testd_registered _sock_in _sock_err testd_connected timeout )], ], # heap => \%data, ); $poe_kernel->run(); exit 0; sub _start { $_[HEAP]->{testd} = Test::POE::Server::TCP->spawn( address => '127.0.0.1', port => 0, options => { trace => 0 }, ); isa_ok( $_[HEAP]->{testd}, 'Test::POE::Server::TCP' ); return; } sub testd_registered { my ($kernel,$heap,$object) = @_[KERNEL,HEAP,ARG0]; isa_ok( $object, 'Test::POE::Server::TCP' ); $heap->{port} = $object->port(); $object->pause_listening(); $heap->{want_timeout} = 1; $kernel->delay(timeout => 1); $_[HEAP]->{factory} = POE::Wheel::SocketFactory->new( RemoteAddress => '127.0.0.1', RemotePort => $object->port(), SuccessEvent => '_sock_up', FailureEvent => '_sock_fail', ); return; } sub timeout { my ($kernel,$heap) = @_[KERNEL,HEAP]; if (delete $heap->{want_timeout}) { pass('got timeout'); $heap->{testd}->resume_listening(); } else { BAIL_OUT('unexpected timeout'); delete $heap->{factory}; $heap->{testd}->shutdown; } } sub _sock_up { my ($heap,$socket) = @_[HEAP,ARG0]; delete $heap->{factory}; $heap->{socket} = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); return; } sub _sock_fail { my $heap = $_[HEAP]; delete $heap->{factory}; $heap->{testd}->shutdown(); BAIL_OUT('connection failed'); return; } sub _sock_in { my ($heap,$input) = @_[HEAP,ARG0]; BAIL_OUT('unexpected input'); delete $_[HEAP]->{socket}; $_[HEAP]->{testd}->shutdown(); return; } sub _sock_err { BAIL_OUT('unexpected error'); delete $_[HEAP]->{socket}; $_[HEAP]->{testd}->shutdown(); return; } sub testd_connected { my ($heap,$state,$id) = @_[HEAP,STATE,ARG0]; if ($heap->{want_timeout}) { BAIL_OUT('unexpected connection'); } else { pass($state); } delete $heap->{socket}; $heap->{testd}->shutdown; } 08_buffer.t100644001750001750 361212706430410 17101 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20/tuse strict; use Test::More; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use Test::POE::Server::TCP; plan tests => 7; my @data = ( 'This is a test', 'This is another test', 'This is the last test', ); POE::Session->create( package_states => [ 'main' => [qw( _start _sock_up _sock_fail _sock_in _sock_err testd_registered testd_connected testd_disconnected testd_client_flushed )], ], heap => { data => \@data, }, ); $poe_kernel->run(); exit 0; sub _start { my $heap = $_[HEAP]; $heap->{testd} = Test::POE::Server::TCP->spawn( address => '127.0.0.1', port => 0, ); $heap->{count} = @{ $heap->{data} }; return; } sub testd_registered { my ($heap,$object) = @_[HEAP,ARG0]; $heap->{port} = $object->port(); $heap->{factory} = POE::Wheel::SocketFactory->new( RemoteAddress => '127.0.0.1', RemotePort => $heap->{port}, SuccessEvent => '_sock_up', FailureEvent => '_sock_fail', ); return; } sub _sock_up { my ($heap,$socket) = @_[HEAP,ARG0]; delete $heap->{factory}; $heap->{socket} = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); return; } sub _sock_fail { my $heap = $_[HEAP]; pass($_[STATE]); delete $heap->{factory}; $heap->{testd}->shutdown(); return; } sub _sock_in { my ($heap,$input) = @_[HEAP,ARG0]; pass($input); return; } sub _sock_err { delete $_[HEAP]->{socket}; pass($_[STATE]); $_[HEAP]->{testd}->shutdown(); return; } sub testd_connected { my ($heap,$state,$id) = @_[HEAP,STATE,ARG0]; pass($state); $heap->{testd}->send_to_client( $id, [ @{ $heap->{data} } ] ); return; } sub testd_disconnected { pass($_[STATE]); $poe_kernel->post( $_[SENDER], 'shutdown' ); return; } sub testd_client_flushed { my ($heap,$state,$id) = @_[HEAP,STATE,ARG0]; pass($state); $heap->{testd}->terminate($id); return; } 10_prefix.t100644001750001750 415612706430410 17122 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20/tuse strict; use Test::More; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use Test::POE::Server::TCP; plan tests => 5; my @data = ( 'This is a test', 'This is another test', 'This is the last test', ); POE::Session->create( package_states => [ 'main' => [qw( _start _sock_up _sock_fail _sock_in _sock_err bingosd_registered bingosd_connected bingosd_disconnected bingosd_client_input )], ], heap => { data => \@data, }, ); $poe_kernel->run(); exit 0; sub _start { $_[HEAP]->{testd} = Test::POE::Server::TCP->spawn( address => '127.0.0.1', port => 0, prefix => 'bingosd', ); return; } sub bingosd_registered { my ($heap,$object) = @_[HEAP,ARG0]; $heap->{port} = $object->port(); $heap->{factory} = POE::Wheel::SocketFactory->new( RemoteAddress => '127.0.0.1', RemotePort => $heap->{port}, SuccessEvent => '_sock_up', FailureEvent => '_sock_fail', ); return; } sub _sock_up { my ($heap,$socket) = @_[HEAP,ARG0]; delete $heap->{factory}; $heap->{socket} = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{socket}->put( $heap->{data}->[0] ); return; } sub _sock_fail { my $heap = $_[HEAP]; delete $heap->{factory}; $heap->{testd}->shutdown(); return; } sub _sock_in { my ($heap,$input) = @_[HEAP,ARG0]; my $data = shift @{ $heap->{data} }; ok( $input eq $data, 'Data matched' ); unless ( scalar @{ $heap->{data} } ) { if ( $^O =~ /(cygwin|MSWin)/ ) { $heap->{socket}->shutdown_input(); $heap->{socket}->shutdown_output(); } delete $heap->{socket}; return; } $heap->{socket}->put( $heap->{data}->[0] ); return; } sub _sock_err { delete $_[HEAP]->{socket}; return; } sub bingosd_connected { my ($heap,$state,$id) = @_[HEAP,STATE,ARG0]; pass($state); return; } sub bingosd_disconnected { pass($_[STATE]); $poe_kernel->post( $_[SENDER], 'shutdown' ); return; } sub bingosd_client_input { my ($sender,$id,$input) = @_[SENDER,ARG0,ARG1]; my $testd = $_[SENDER]->get_heap(); $testd->send_to_client( $id, $input ); return; } 06_filter.t100644001750001750 702612706430410 17116 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20/tuse strict; { package TPSTParseWords; use base qw(POE::Filter); use Text::ParseWords; my $VERSION = '1.02'; sub new { my $class = shift; my %opts = @_; $opts{lc $_} = delete $opts{$_} for keys %opts; $opts{keep} = 0 unless $opts{keep}; $opts{delim} = '\s+' unless $opts{delim}; $opts{BUFFER} = []; bless \%opts, $class; } sub get { my ($self, $raw) = @_; my $events = []; push @$events, [ parse_line( $self->{delim}, $self->{keep}, $_ ) ] for @$raw; return $events; } sub get_one_start { my ($self, $raw) = @_; push @{ $self->{BUFFER} }, $_ for @$raw; } sub get_one { my $self = shift; my $events = []; my $event = shift @{ $self->{BUFFER} }; push @$events, [ parse_line( $self->{delim}, $self->{keep}, $event ) ] if defined $event; return $events; } sub put { warn "PUT is unimplemented\n"; return; } sub clone { my $self = shift; my $nself = { }; $nself->{$_} = $self->{$_} for keys %{ $self }; $nself->{BUFFER} = [ ]; return bless $nself, ref $self; } } use Socket; use Test::More; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use Test::POE::Server::TCP; plan tests => 11; my %data = ( tests => [ [ 'Howdy!' => '"This is just a test" "line" "so there"' ], [ 'bleh' => 'quit' ], ], ); POE::Session->create( package_states => [ 'main' => [qw( _start _sock_up _sock_fail _sock_in _sock_err testd_registered testd_connected testd_disconnected testd_client_input )], ], heap => \%data, ); $poe_kernel->run(); exit 0; sub _start { $_[HEAP]->{testd} = Test::POE::Server::TCP->spawn( address => '127.0.0.1', port => 0, options => { trace => 0 }, inputfilter => TPSTParseWords->new(), outputfilter => POE::Filter::Line->new(), ); isa_ok( $_[HEAP]->{testd}, 'Test::POE::Server::TCP' ); return; } sub testd_registered { my ($heap,$object) = @_[HEAP,ARG0]; isa_ok( $object, 'Test::POE::Server::TCP' ); $heap->{port} = $object->port(); $heap->{factory} = POE::Wheel::SocketFactory->new( RemoteAddress => '127.0.0.1', RemotePort => $heap->{port}, SuccessEvent => '_sock_up', FailureEvent => '_sock_fail', ); return; } sub _sock_up { my ($heap,$socket) = @_[HEAP,ARG0]; delete $heap->{factory}; $heap->{socket} = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); return; } sub _sock_fail { my $heap = $_[HEAP]; delete $heap->{factory}; $heap->{testd}->shutdown(); return; } sub _sock_in { my ($heap,$input) = @_[HEAP,ARG0]; my @parms = split /\s+/, $input; my $test = shift @{ $heap->{tests} }; if ( $test and $test->[0] eq $parms[0] ) { pass($input); $heap->{socket}->put( $test->[1] ); return; } pass($input); return; } sub _sock_err { delete $_[HEAP]->{socket}; pass("Disconnected"); $_[HEAP]->{testd}->shutdown(); return; } sub testd_connected { my ($heap,$state,$id) = @_[HEAP,STATE,ARG0]; $heap->{testd}->send_to_client( $id, 'Howdy!' ); pass($state); return; } sub testd_disconnected { pass($_[STATE]); return; } sub testd_client_input { my ($sender,$id,$input) = @_[SENDER,ARG0,ARG1]; my $testd = $_[SENDER]->get_heap(); pass($_[STATE]); if ( $input->[0] eq 'quit' ) { $testd->disconnect( $id ); $testd->send_to_client( $id, 'Buh Bye!' ); return; } ok( ( $input->[0] eq 'This is just a test' and $input->[1] eq 'line' and $input->[2] eq 'so there' ) , 'Test Get' ); $testd->send_to_client( $id, 'bleh' ); return; } 00-compile.t100644001750001750 234012706430410 17163 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20/tuse 5.006; use strict; use warnings; # this test was generated with Dist::Zilla::Plugin::Test::Compile 2.054 use Test::More; plan tests => 1 + ($ENV{AUTHOR_TESTING} ? 1 : 0); my @module_files = ( 'Test/POE/Server/TCP.pm' ); # no fake home requested my $inc_switch = -d 'blib' ? '-Mblib' : '-Ilib'; use File::Spec; use IPC::Open3; use IO::Handle; open my $stdin, '<', File::Spec->devnull or die "can't open devnull: $!"; my @warnings; for my $lib (@module_files) { # see L my $stderr = IO::Handle->new; my $pid = open3($stdin, '>&STDERR', $stderr, $^X, $inc_switch, '-e', "require q[$lib]"); binmode $stderr, ':crlf' if $^O eq 'MSWin32'; my @_warnings = <$stderr>; waitpid($pid, 0); is($?, 0, "$lib loaded ok"); shift @_warnings if @_warnings and $_warnings[0] =~ /^Using .*\bblib/ and not eval { require blib; blib->VERSION('1.01') }; if (@_warnings) { warn @_warnings; push @warnings, @_warnings; } } is(scalar(@warnings), 0, 'no warnings found') or diag 'got warnings: ', ( Test::More->can('explain') ? Test::More::explain(\@warnings) : join("\n", '', @warnings) ) if $ENV{AUTHOR_TESTING}; 04_multiple.t100644001750001750 550212706430410 17457 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20/tuse strict; use Socket; use Test::More; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use Test::POE::Server::TCP; my %data = ( tests => [ [ '+OK' => 'cock' ], [ '-ERR' => 'quit' ], ], clients => 2, ); plan tests => 22; POE::Session->create( package_states => [ 'main' => [qw( _start _sock_up _sock_fail _sock_in _sock_err testd_registered testd_connected testd_disconnected testd_client_input testd_client_flushed )], ], heap => \%data, ); $poe_kernel->run(); exit 0; sub _start { $_[HEAP]->{testd} = Test::POE::Server::TCP->spawn( address => '127.0.0.1', port => 0, options => { trace => 0 }, ); isa_ok( $_[HEAP]->{testd}, 'Test::POE::Server::TCP' ); return; } sub testd_registered { my ($heap,$object) = @_[HEAP,ARG0]; isa_ok( $object, 'Test::POE::Server::TCP' ); $heap->{port} = $object->port(); for ( 1 .. $heap->{clients} ) { my $factory = POE::Wheel::SocketFactory->new( RemoteAddress => '127.0.0.1', RemotePort => $heap->{port}, SuccessEvent => '_sock_up', FailureEvent => '_sock_fail', ); $heap->{factories}->{ $factory->ID } = $factory; } return; } sub _sock_up { my ($heap,$socket,$fact_id) = @_[HEAP,ARG0,ARG3]; delete $heap->{factories}->{ $fact_id }; my $wheel = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{wheels}->{ $wheel->ID } = $wheel; $heap->{numbers}->{ $wheel->ID } = 0; return; } sub _sock_fail { my ($heap,$fact_id) = @_[HEAP,ARG3]; delete $heap->{factory}->{ $fact_id }; $heap->{clients}--; $heap->{testd}->shutdown() if $heap->{clients} <= 0; return; } sub _sock_in { my ($heap,$input,$wheel_id) = @_[HEAP,ARG0,ARG1]; my @parms = split /\s+/, $input; my $test = $heap->{tests}->[ $heap->{numbers}->{ $wheel_id } ]; $heap->{numbers}->{ $wheel_id }++; if ( $test and $test->[0] eq $parms[0] ) { pass($input); $heap->{wheels}->{ $wheel_id }->put( $test->[1] ); return; } pass($input); return; } sub _sock_err { my ($heap,$wheel_id) = @_[HEAP,ARG3]; delete $heap->{wheels}->{ $wheel_id }; pass("Disconnected"); $heap->{clients}--; $heap->{testd}->shutdown() if $heap->{clients} <= 0; return; } sub testd_connected { my ($heap,$state,$id) = @_[HEAP,STATE,ARG0]; $heap->{testd}->send_to_client( $id, '+OK POP3 Fakepop 6.9 server ready' ); pass($state); return; } sub testd_disconnected { pass($_[STATE]); return; } sub testd_client_flushed { pass($_[STATE]); return; } sub testd_client_input { my ($sender,$id,$input) = @_[SENDER,ARG0,ARG1]; my $testd = $_[SENDER]->get_heap(); pass($_[STATE]); if ( $input eq 'quit' ) { $testd->disconnect( $id ); $testd->send_to_client( $id, '+OK POP3 server signing off' ); return; } $testd->send_to_client( $id, '-ERR' ); return; } 03_register.t100644001750001750 112512706430410 17444 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20/tuse strict; use Test::More; use POE; use Test::POE::Server::TCP; plan tests => 4; POE::Session->create( package_states => [ 'main' => [qw( _start _stop testd_registered )], ], ); $poe_kernel->run(); exit 0; sub _start { $_[HEAP]->{testd} = Test::POE::Server::TCP->spawn(); pass($_[STATE]); return; } sub _stop { $_[HEAP]->{testd}->shutdown; pass($_[STATE]); return; } sub testd_registered { my ($sender,$object) = @_[SENDER,ARG0]; pass($_[STATE]); isa_ok( $object, 'Test::POE::Server::TCP' ); $poe_kernel->post( $sender, 'unregister', 'all' ); return; } 02_register.t100644001750001750 120112706430410 17436 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20/tuse strict; use Test::More; use POE; use Test::POE::Server::TCP; plan tests => 4; my $testd = Test::POE::Server::TCP->spawn(); POE::Session->create( package_states => [ 'main' => [qw( _start _stop testd_registered )], ], ); $poe_kernel->run(); exit 0; sub _start { pass($_[STATE]); $poe_kernel->post( $testd->session_id(), 'register', 'all' ); return; } sub _stop { $testd->shutdown; pass($_[STATE]); return; } sub testd_registered { my ($sender,$object) = @_[SENDER,ARG0]; pass($_[STATE]); isa_ok( $object, 'Test::POE::Server::TCP' ); $poe_kernel->post( $sender, 'unregister', 'all' ); return; } 05_subclass.t100644001750001750 463312706430410 17450 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20/tuse strict; { package TPSTSubclass; use base qw(Test::POE::Server::TCP); my $VERSION = '0.01'; } use Socket; use Test::More; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); my %data = ( tests => [ [ '+OK' => 'cock' ], [ '-ERR' => 'quit' ], ], ); plan tests => 12; POE::Session->create( package_states => [ 'main' => [qw( _start _sock_up _sock_fail _sock_in _sock_err testd_registered testd_connected testd_disconnected testd_client_input )], ], heap => \%data, ); $poe_kernel->run(); exit 0; sub _start { $_[HEAP]->{testd} = TPSTSubclass->spawn( address => '127.0.0.1', port => 0, options => { trace => 0 }, ); isa_ok( $_[HEAP]->{testd}, 'Test::POE::Server::TCP' ); isa_ok( $_[HEAP]->{testd}, 'TPSTSubclass' ); return; } sub testd_registered { my ($heap,$object) = @_[HEAP,ARG0]; isa_ok( $object, 'Test::POE::Server::TCP' ); isa_ok( $object, 'TPSTSubclass' ); $heap->{port} = $object->port(); $heap->{factory} = POE::Wheel::SocketFactory->new( RemoteAddress => '127.0.0.1', RemotePort => $heap->{port}, SuccessEvent => '_sock_up', FailureEvent => '_sock_fail', ); return; } sub _sock_up { my ($heap,$socket) = @_[HEAP,ARG0]; delete $heap->{factory}; $heap->{socket} = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); return; } sub _sock_fail { my $heap = $_[HEAP]; delete $heap->{factory}; $heap->{testd}->shutdown(); return; } sub _sock_in { my ($heap,$input) = @_[HEAP,ARG0]; my @parms = split /\s+/, $input; my $test = shift @{ $heap->{tests} }; if ( $test and $test->[0] eq $parms[0] ) { pass($input); $heap->{socket}->put( $test->[1] ); return; } pass($input); return; } sub _sock_err { delete $_[HEAP]->{socket}; pass("Disconnected"); $_[HEAP]->{testd}->shutdown(); return; } sub testd_connected { my ($heap,$state,$id) = @_[HEAP,STATE,ARG0]; $heap->{testd}->send_to_client( $id, '+OK POP3 Fakepop 6.9 server ready' ); pass($state); return; } sub testd_disconnected { pass($_[STATE]); return; } sub testd_client_input { my ($sender,$id,$input) = @_[SENDER,ARG0,ARG1]; my $testd = $_[SENDER]->get_heap(); pass($_[STATE]); if ( $input eq 'quit' ) { $testd->disconnect( $id ); $testd->send_to_client( $id, '+OK POP3 server signing off' ); return; } $testd->send_to_client( $id, '-ERR' ); return; } 07_synopsis.t100644001750001750 411012706430410 17510 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20/tuse strict; use Test::More; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use Test::POE::Server::TCP; plan tests => 5; my @data = ( 'This is a test', 'This is another test', 'This is the last test', ); POE::Session->create( package_states => [ 'main' => [qw( _start _sock_up _sock_fail _sock_in _sock_err testd_registered testd_connected testd_disconnected testd_client_input )], ], heap => { data => \@data, }, ); $poe_kernel->run(); exit 0; sub _start { $_[HEAP]->{testd} = Test::POE::Server::TCP->spawn( address => '127.0.0.1', port => 0, ); return; } sub testd_registered { my ($heap,$object) = @_[HEAP,ARG0]; $heap->{port} = $object->port(); $heap->{factory} = POE::Wheel::SocketFactory->new( RemoteAddress => '127.0.0.1', RemotePort => $heap->{port}, SuccessEvent => '_sock_up', FailureEvent => '_sock_fail', ); return; } sub _sock_up { my ($heap,$socket) = @_[HEAP,ARG0]; delete $heap->{factory}; $heap->{socket} = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{socket}->put( $heap->{data}->[0] ); return; } sub _sock_fail { my $heap = $_[HEAP]; delete $heap->{factory}; $heap->{testd}->shutdown(); return; } sub _sock_in { my ($heap,$input) = @_[HEAP,ARG0]; my $data = shift @{ $heap->{data} }; ok( $input eq $data, 'Data matched' ); unless ( scalar @{ $heap->{data} } ) { if ( $^O =~ /(cygwin|MSWin)/ ) { $heap->{socket}->shutdown_input(); $heap->{socket}->shutdown_output(); } delete $heap->{socket}; return; } $heap->{socket}->put( $heap->{data}->[0] ); return; } sub _sock_err { delete $_[HEAP]->{socket}; return; } sub testd_connected { my ($heap,$state,$id) = @_[HEAP,STATE,ARG0]; pass($state); return; } sub testd_disconnected { pass($_[STATE]); $poe_kernel->post( $_[SENDER], 'shutdown' ); return; } sub testd_client_input { my ($sender,$id,$input) = @_[SENDER,ARG0,ARG1]; my $testd = $_[SENDER]->get_heap(); $testd->send_to_client( $id, $input ); return; } 12_all_clients.t100644001750001750 522512706430410 20116 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20/tuse strict; use Socket; use Test::More; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use Test::POE::Server::TCP; my %data = ( tests => [ [ '+OK' => 'cock' ], [ '-ERR' => 'quit' ], ], clients => 2, ); plan tests => 14; POE::Session->create( package_states => [ 'main' => [qw( _start _sock_up _sock_fail _sock_in _sock_err testd_registered testd_connected testd_disconnected testd_client_input testd_client_flushed )], ], heap => \%data, ); $poe_kernel->run(); exit 0; sub _start { $_[HEAP]->{testd} = Test::POE::Server::TCP->spawn( address => '127.0.0.1', port => 0, options => { trace => 0 }, ); isa_ok( $_[HEAP]->{testd}, 'Test::POE::Server::TCP' ); return; } sub testd_registered { my ($heap,$object) = @_[HEAP,ARG0]; isa_ok( $object, 'Test::POE::Server::TCP' ); $heap->{port} = $object->port(); for ( 1 .. $heap->{clients} ) { my $factory = POE::Wheel::SocketFactory->new( RemoteAddress => '127.0.0.1', RemotePort => $heap->{port}, SuccessEvent => '_sock_up', FailureEvent => '_sock_fail', ); $heap->{factories}->{ $factory->ID } = $factory; } return; } sub _sock_up { my ($heap,$socket,$fact_id) = @_[HEAP,ARG0,ARG3]; delete $heap->{factories}->{ $fact_id }; my $wheel = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{wheels}->{ $wheel->ID } = $wheel; $heap->{numbers}->{ $wheel->ID } = 0; return; } sub _sock_fail { my ($heap,$fact_id) = @_[HEAP,ARG3]; delete $heap->{factory}->{ $fact_id }; $heap->{clients}--; $heap->{testd}->shutdown() if $heap->{clients} <= 0; return; } sub _sock_in { my ($heap,$input,$wheel_id) = @_[HEAP,ARG0,ARG1]; is($input,'HELLO ALL','Input from server to all'); $heap->{wheels}->{ $wheel_id }->put('quit'); return; } sub _sock_err { my ($heap,$wheel_id) = @_[HEAP,ARG3]; delete $heap->{wheels}->{ $wheel_id }; pass("Disconnected"); $heap->{clients}--; $heap->{testd}->shutdown() if $heap->{clients} <= 0; return; } sub testd_connected { my ($heap,$state,$id) = @_[HEAP,STATE,ARG0]; pass($state); $heap->{connected}++; return unless $heap->{connected} == $heap->{clients}; $poe_kernel->post( $_[SENDER], 'send_to_all_clients', 'HELLO ALL' ); return; } sub testd_disconnected { pass($_[STATE]); return; } sub testd_client_flushed { pass($_[STATE]); return; } sub testd_client_input { my ($sender,$id,$input) = @_[SENDER,ARG0,ARG1]; my $testd = $_[SENDER]->get_heap(); pass($_[STATE]); if ( $input eq 'quit' ) { $testd->terminate( $id ); return; } $testd->send_to_client( $id, '-ERR' ); return; } 09_client_info.t100644001750001750 446512706430410 20131 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20/tuse strict; use Test::More; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use Test::POE::Server::TCP; plan tests => 16; my @data = ( 'This is a test', 'This is another test', 'This is the last test', ); POE::Session->create( package_states => [ 'main' => [qw( _start _sock_up _sock_fail _sock_in _sock_err testd_registered testd_connected testd_disconnected testd_client_flushed )], ], heap => { data => \@data, }, ); $poe_kernel->run(); exit 0; sub _start { my $heap = $_[HEAP]; $heap->{testd} = Test::POE::Server::TCP->spawn( address => '127.0.0.1', port => 0, ); $heap->{count} = @{ $heap->{data} }; return; } sub testd_registered { my ($heap,$object) = @_[HEAP,ARG0]; $heap->{port} = $object->port(); $heap->{factory} = POE::Wheel::SocketFactory->new( RemoteAddress => '127.0.0.1', RemotePort => $heap->{port}, SuccessEvent => '_sock_up', FailureEvent => '_sock_fail', ); return; } sub _sock_up { my ($heap,$socket) = @_[HEAP,ARG0]; delete $heap->{factory}; $heap->{socket} = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); return; } sub _sock_fail { my $heap = $_[HEAP]; pass($_[STATE]); delete $heap->{factory}; $heap->{testd}->shutdown(); return; } sub _sock_in { my ($heap,$input) = @_[HEAP,ARG0]; pass($input); return; } sub _sock_err { delete $_[HEAP]->{socket}; pass($_[STATE]); $_[HEAP]->{testd}->shutdown(); return; } sub testd_connected { my ($heap,$state,$id) = @_[HEAP,STATE,ARG0]; pass($state); my @orig = @_[ARG1..$#_]; my @test = $heap->{testd}->client_info( $id ); my $test = $heap->{testd}->client_info( $id ); isa_ok( $heap->{testd}->client_wheel( $id ), 'POE::Wheel::ReadWrite' ); ok( $test[$_] eq $orig[$_], "Client Address: " . $orig[$_] ) for 0 .. 3; my @test2 = map { $test->{$_} } qw(peeraddr peerport sockaddr sockport); ok( $test2[$_] eq $orig[$_], "Client Address: " . $orig[$_] ) for 0 .. 3; $heap->{testd}->send_to_client( $id, [ @{ $heap->{data} } ] ); return; } sub testd_disconnected { pass($_[STATE]); $poe_kernel->post( $_[SENDER], 'shutdown' ); return; } sub testd_client_flushed { my ($heap,$state,$id) = @_[HEAP,STATE,ARG0]; pass($state); $heap->{testd}->terminate($id); return; } examples000755001750001750 012706430410 16345 5ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20synopsis.pl100644001750001750 372412706430410 20737 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20/examplesuse strict; use Test::More; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use Test::POE::Server::TCP; plan tests => 5; my @data = ( 'This is a test', 'This is another test', 'This is the last test', ); POE::Session->create( package_states => [ 'main' => [qw( _start _sock_up _sock_fail _sock_in _sock_err testd_registered testd_connected testd_disconnected testd_client_input )], ], heap => { data => \@data, }, ); $poe_kernel->run(); exit 0; sub _start { $_[HEAP]->{testd} = Test::POE::Server::TCP->spawn( address => '127.0.0.1', port => 0, ); return; } sub testd_registered { my ($heap,$object) = @_[HEAP,ARG0]; $heap->{port} = $object->port(); $heap->{factory} = POE::Wheel::SocketFactory->new( RemoteAddress => '127.0.0.1', RemotePort => $heap->{port}, SuccessEvent => '_sock_up', FailureEvent => '_sock_fail', ); return; } sub _sock_up { my ($heap,$socket) = @_[HEAP,ARG0]; delete $heap->{factory}; $heap->{socket} = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{socket}->put( $heap->{data}->[0] ); return; } sub _sock_fail { my $heap = $_[HEAP]; delete $heap->{factory}; $heap->{testd}->shutdown(); return; } sub _sock_in { my ($heap,$input) = @_[HEAP,ARG0]; my $data = shift @{ $heap->{data} }; ok( $input eq $data, 'Data matched' ); unless ( scalar @{ $heap->{data} } ) { delete $heap->{socket}; return; } $heap->{socket}->put( $heap->{data}->[0] ); return; } sub _sock_err { delete $_[HEAP]->{socket}; return; } sub testd_connected { my ($heap,$state,$id) = @_[HEAP,STATE,ARG0]; pass($state); return; } sub testd_disconnected { pass($_[STATE]); $poe_kernel->post( $_[SENDER], 'shutdown' ); return; } sub testd_client_input { my ($sender,$id,$input) = @_[SENDER,ARG0,ARG1]; my $testd = $_[SENDER]->get_heap(); $testd->send_to_client( $id, $input ); return; } author-pod-syntax.t100644001750001750 50312706430410 20703 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20/t#!perl BEGIN { unless ($ENV{AUTHOR_TESTING}) { require Test::More; Test::More::plan(skip_all => 'these tests are for testing by the author'); } } # This file was automatically generated by Dist::Zilla::Plugin::PodSyntaxTests. use strict; use warnings; use Test::More; use Test::Pod 1.41; all_pod_files_ok(); author-pod-coverage.t100644001750001750 56512706430410 21160 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20/t#!perl BEGIN { unless ($ENV{AUTHOR_TESTING}) { require Test::More; Test::More::plan(skip_all => 'these tests are for testing by the author'); } } # This file was automatically generated by Dist::Zilla::Plugin::PodCoverageTests. use Test::Pod::Coverage 1.08; use Pod::Coverage::TrustPod; all_pod_coverage_ok({ coverage_class => 'Pod::Coverage::TrustPod' }); Server000755001750001750 012706430410 20105 5ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20/lib/Test/POETCP.pm100644001750001750 6351512706430410 21263 0ustar00bingosbingos000000000000Test-POE-Server-TCP-1.20/lib/Test/POE/Serverpackage Test::POE::Server::TCP; $Test::POE::Server::TCP::VERSION = '1.20'; # ABSTRACT: A POE Component providing TCP server services for test cases use strict; use warnings; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use Socket; use Carp qw(carp croak); our $GOT_SOCKET6; BEGIN { eval { Socket->import(qw(AF_INET6 IN6ADDR_ANY NI_NUMERICHOST NI_NUMERICSERV getnameinfo)); $GOT_SOCKET6 = 1; }; if (!$GOT_SOCKET6) { # provide a dummy subs so code compiles *AF_INET6 = sub { ~0 }; *IN6ADDR_ANY = sub { ~0 }; } } sub spawn { my $package = shift; my %opts = @_; $opts{lc $_} = delete $opts{$_} for keys %opts; my $options = delete $opts{options}; my $self = bless \%opts, $package; $self->{_prefix} = delete $self->{prefix}; $self->{_prefix} = 'testd_' unless defined $self->{_prefix}; $self->{_prefix} .= '_' unless $self->{_prefix} =~ /\_$/; $self->{session_id} = POE::Session->create( object_states => [ $self => { shutdown => '_shutdown', send_event => '__send_event', send_to_client => '_send_to_client', send_to_all_clients => '_send_to_all_clients', disconnect => '_disconnect', terminate => '_terminate', start_listener => '_start_listener', }, $self => [ qw(_start register unregister _accept_client _conn_input _conn_error _conn_flushed _conn_alarm _send_to_client __send_event _disconnect _send_to_all_clients _accept_failed4 _accept_failed6) ], ], heap => $self, ( ref($options) eq 'HASH' ? ( options => $options ) : () ), )->ID(); return $self; } sub session_id { return $_[0]->{session_id}; } sub pause_listening { $_[0]->{listener}->pause_accept() if $_[0]->{listener}; $_[0]->{listener6}->pause_accept() if $_[0]->{listener6}; } sub resume_listening { $_[0]->{listener}->resume_accept() if $_[0]->{listener}; $_[0]->{listener6}->resume_accept() if $_[0]->{listener6}; } sub getsockname { return unless $_[0]->{listener}; return $_[0]->{listener}->getsockname(); } sub port { my $self = shift; return ( sockaddr_in( $self->getsockname() ) )[0]; } sub getsockname6 { return unless $_[0]->{listener6}; return $_[0]->{listener6}->getsockname(); } sub port6 { my $self = shift; return ( sockaddr_in6( $self->getsockname6() ) )[0]; } sub _conn_exists { my ($self,$wheel_id) = @_; return 0 unless $wheel_id and defined $self->{clients}->{ $wheel_id }; return 1; } sub shutdown { my $self = shift; $poe_kernel->call( $self->{session_id}, 'shutdown' ); } sub _start { my ($kernel,$self,$sender) = @_[KERNEL,OBJECT,SENDER]; $self->{session_id} = $_[SESSION]->ID(); if ( $self->{alias} ) { $kernel->alias_set( $self->{alias} ); } else { $kernel->refcount_increment( $self->{session_id} => __PACKAGE__ ); } if ( $kernel != $sender ) { my $sender_id = $sender->ID; $self->{events}->{$self->{_prefix} . 'all'}->{$sender_id} = $sender_id; $self->{sessions}->{$sender_id}->{'ref'} = $sender_id; $self->{sessions}->{$sender_id}->{'refcnt'}++; $kernel->refcount_increment($sender_id, __PACKAGE__); $kernel->post( $sender, $self->{_prefix} . 'registered', $self ); $kernel->detach_myself(); } $kernel->call( $self->{session_id}, 'start_listener' ); return; } sub start_listener { my $self = shift; $poe_kernel->call( $self->{session_id}, 'start_listener', @_ ); } sub _start_listener { my ($kernel,$self) = @_[KERNEL,OBJECT]; return if $self->{listener}; $self->{listener} = POE::Wheel::SocketFactory->new( ( defined $self->{address} ? ( BindAddress => $self->{address} ) : () ), ( defined $self->{port} ? ( BindPort => $self->{port} ) : ( BindPort => 0 ) ), SuccessEvent => '_accept_client', FailureEvent => '_accept_failed4', SocketDomain => AF_INET, # Sets the socket() domain SocketType => SOCK_STREAM, # Sets the socket() type SocketProtocol => 'tcp', # Sets the socket() protocol Reuse => 'on', # Lets the port be reused ); return unless $GOT_SOCKET6; $self->{listener6} = POE::Wheel::SocketFactory->new( #BindAddress => IN6ADDR_ANY, ( defined $self->{port} ? ( BindPort => $self->{port} ) : ( BindPort => 0 ) ), SuccessEvent => '_accept_client', FailureEvent => '_accept_failed6', SocketDomain => AF_INET6, # Sets the socket() domain SocketType => SOCK_STREAM, # Sets the socket() type SocketProtocol => 'tcp', # Sets the socket() protocol Reuse => 'on', # Lets the port be reused ); return; } sub _accept_client { my ($kernel,$self,$socket,$listener_id) = @_[KERNEL,OBJECT,ARG0,ARG3]; my (undef,$peeraddr,$peerport) = getnameinfo( CORE::getpeername( $socket ), NI_NUMERICHOST | NI_NUMERICSERV ); my (undef,$sockaddr,$sockport) = getnameinfo( CORE::getsockname( $socket ), NI_NUMERICHOST | NI_NUMERICSERV ); s!^::ffff:!! for ( $sockaddr, $peeraddr ); my $wheel = POE::Wheel::ReadWrite->new( Handle => $socket, _get_filters( $self->{filter}, $self->{inputfilter}, $self->{outputfilter} ), InputEvent => '_conn_input', ErrorEvent => '_conn_error', FlushedEvent => '_conn_flushed', ); return unless $wheel; my $id = $wheel->ID(); $self->{clients}->{ $id } = { wheel => $wheel, peeraddr => $peeraddr, peerport => $peerport, sockaddr => $sockaddr, sockport => $sockport, }; $self->_send_event( $self->{_prefix} . 'connected', $id, $peeraddr, $peerport, $sockaddr, $sockport ); #$self->{clients}->{ $id }->{alarm} = $kernel->delay_set( '_conn_alarm', $self->{time_out} || 300, $id ); return; } sub client_info { my $self = shift; my $id = shift || return; return unless $self->_conn_exists( $id ); my %hash = %{ $self->{clients}->{ $id } }; delete $hash{wheel}; return map { $hash{$_} } qw(peeraddr peerport sockaddr sockport) if wantarray; return \%hash; } sub client_wheel { my $self = shift; my $id = shift || return; return unless $self->_conn_exists( $id ); return $self->{clients}->{ $id }->{wheel}; } sub _get_filters { my ($client_filter, $client_infilter, $client_outfilter) = @_; if (defined $client_infilter or defined $client_outfilter) { return ( "InputFilter" => _load_filter($client_infilter), "OutputFilter" => _load_filter($client_outfilter) ); if (defined $client_filter) { carp( "Filter ignored with InputFilter or OutputFilter" ); } } elsif (defined $client_filter) { return ( "Filter" => _load_filter($client_filter) ); } else { return ( Filter => POE::Filter::Line->new(), ); } } # Get something: either arrayref, ref, or string # Return filter sub _load_filter { my $filter = shift; if (ref ($filter) eq 'ARRAY') { my @args = @$filter; $filter = shift @args; if ( _test_filter($filter) ){ return $filter->new(@args); } else { return POE::Filter::Line->new(@args); } } elsif (ref $filter) { return $filter->clone(); } else { if ( _test_filter($filter) ) { return $filter->new(); } else { return POE::Filter::Line->new(); } } } # Test if a Filter can be loaded, return sucess or failure sub _test_filter { my $filter = shift; my $eval = eval { (my $mod = $filter) =~ s!::!/!g; require "$mod.pm"; 1; }; if (!$eval and $@) { carp( "Failed to load [$filter]\n" . "Reason $@\nUsing defualt POE::Filter::Line " ); return 0; } return 1; } sub _accept_failed4 { my ($kernel,$self,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,OBJECT,ARG0..ARG3]; warn "Wheel $wheel_id generated $operation error $errnum: $errstr\n"; delete $self->{listener} if $operation eq 'listen'; $self->_send_event( $self->{_prefix} . 'listener_failed', $operation, $errnum, $errstr ); return; } sub _accept_failed6 { my ($kernel,$self,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,OBJECT,ARG0..ARG3]; warn "Wheel $wheel_id generated $operation error $errnum: $errstr\n"; delete $self->{listener6} if $operation eq 'listen'; $self->_send_event( $self->{_prefix} . 'listener_failed', $operation, $errnum, $errstr ); return; } sub disconnect { my $self = shift; $poe_kernel->call( $self->{session_id}, 'disconnect', @_ ); } sub _disconnect { my ($kernel,$self,$id) = @_[KERNEL,OBJECT,ARG0]; return unless $self->_conn_exists( $id ); $self->{clients}->{ $id }->{quit} = 1; return 1; } sub terminate { my $self = shift; $poe_kernel->call( $self->{session_id}, 'terminate', @_ ); } sub _terminate { my ($kernel,$self,$id) = @_[KERNEL,OBJECT,ARG0]; return unless $self->_conn_exists( $id ); delete $self->{clients}->{ $id }; $self->_send_event( $self->{_prefix} . 'disconnected', $id ); return 1; } sub _conn_input { my ($kernel,$self,$input,$id) = @_[KERNEL,OBJECT,ARG0,ARG1]; return unless $self->_conn_exists( $id ); #$kernel->delay_adjust( $self->{clients}->{ $id }->{alarm}, $self->{time_out} || 300 ); $self->_send_event( $self->{_prefix} . 'client_input', $id, $input ); return; } sub _conn_error { my ($self,$errstr,$id) = @_[OBJECT,ARG2,ARG3]; return unless $self->_conn_exists( $id ); my $href = delete $self->{clients}->{ $id }; delete $href->{wheel}; $self->_send_event( $self->{_prefix} . 'disconnected', $id, map { $href->{$_} } qw(peeraddr peerport sockaddr sockport) ); return; } sub _conn_flushed { my ($self,$id) = @_[OBJECT,ARG0]; return unless $self->_conn_exists( $id ); if ( $self->{clients}->{ $id }->{BUFFER} ) { my $item = shift @{ $self->{clients}->{ $id }->{BUFFER} }; unless ( $item ) { delete $self->{clients}->{ $id }->{BUFFER}; $self->_send_event( $self->{_prefix} . 'client_flushed', $id ); return; } $self->{clients}->{ $id }->{wheel}->put($item); return; } unless ( $self->{clients}->{ $id }->{quit} ) { $self->_send_event( $self->{_prefix} . 'client_flushed', $id ); return; } delete $self->{clients}->{ $id }; $self->_send_event( $self->{_prefix} . 'disconnected', $id ); return; } sub _conn_alarm { my ($kernel,$self,$id) = @_[KERNEL,OBJECT,ARG0]; return unless $self->_conn_exists( $id ); delete $self->{clients}->{ $id }; $self->_send_event( $self->{_prefix} . 'disconnected', $id ); return; } sub _shutdown { my ($kernel,$self) = @_[KERNEL,OBJECT]; delete $self->{listener}; delete $self->{listener6}; delete $self->{clients}; $kernel->alarm_remove_all(); $kernel->alias_remove( $_ ) for $kernel->alias_list(); $kernel->refcount_decrement( $self->{session_id} => __PACKAGE__ ) unless $self->{alias}; # $self->_pluggable_destroy(); $self->_unregister_sessions(); return; } sub register { my ($kernel, $self, $session, $sender, @events) = @_[KERNEL, OBJECT, SESSION, SENDER, ARG0 .. $#_]; unless (@events) { warn "register: Not enough arguments"; return; } my $sender_id = $sender->ID(); foreach (@events) { $_ = $self->{_prefix} . $_ unless /^_/; $self->{events}->{$_}->{$sender_id} = $sender_id; $self->{sessions}->{$sender_id}->{'ref'} = $sender_id; unless ($self->{sessions}->{$sender_id}->{refcnt}++ or $session == $sender) { $kernel->refcount_increment($sender_id, __PACKAGE__); } } $kernel->post( $sender, $self->{_prefix} . 'registered', $self ); return; } sub unregister { my ($kernel, $self, $session, $sender, @events) = @_[KERNEL, OBJECT, SESSION, SENDER, ARG0 .. $#_]; unless (@events) { warn "unregister: Not enough arguments"; return; } $self->_unregister($session,$sender,@events); undef; } sub _unregister { my ($self,$session,$sender) = splice @_,0,3; my $sender_id = $sender->ID(); foreach (@_) { $_ = $self->{_prefix} . $_ unless /^_/; my $blah = delete $self->{events}->{$_}->{$sender_id}; unless ( $blah ) { warn "$sender_id hasn't registered for '$_' events\n"; next; } if (--$self->{sessions}->{$sender_id}->{refcnt} <= 0) { delete $self->{sessions}->{$sender_id}; unless ($session == $sender) { $poe_kernel->refcount_decrement($sender_id, __PACKAGE__); } } } undef; } sub _unregister_sessions { my $self = shift; my $testd_id = $self->session_id(); foreach my $session_id ( keys %{ $self->{sessions} } ) { if (--$self->{sessions}->{$session_id}->{refcnt} <= 0) { delete $self->{sessions}->{$session_id}; $poe_kernel->refcount_decrement($session_id, __PACKAGE__) unless ( $session_id eq $testd_id ); } } } sub __send_event { my( $self, $event, @args ) = @_[ OBJECT, ARG0, ARG1 .. $#_ ]; $self->_send_event( $event, @args ); return; } sub _send_event { my $self = shift; my ($event, @args) = @_; my $kernel = $POE::Kernel::poe_kernel; my %sessions; $sessions{$_} = $_ for (values %{$self->{events}->{$self->{_prefix} . 'all'}}, values %{$self->{events}->{$event}}); $kernel->post( $_ => $event => @args ) for values %sessions; undef; } sub send_to_client { my $self = shift; $poe_kernel->call( $self->{session_id}, '_send_to_client', @_ ); } sub _send_to_client { my ($kernel,$self,$id,$output) = @_[KERNEL,OBJECT,ARG0..ARG1]; return unless $self->_conn_exists( $id ); return unless defined $output; if ( ref $output eq 'ARRAY' ) { my $temp = [ @{ $output } ]; my $first = shift @{ $temp }; $self->{clients}->{ $id }->{BUFFER} = $temp if scalar @{ $temp }; $self->{clients}->{ $id }->{wheel}->put($first) if defined $first; return 1; } $self->{clients}->{ $id }->{wheel}->put($output); return 1; } sub send_to_all_clients { my $self = shift; $poe_kernel->call( $self->{session_id}, '_send_to_all_clients', @_ ); } sub _send_to_all_clients { my ($kernel,$self,$output) = @_[KERNEL,OBJECT,ARG0]; return unless defined $output; $self->send_to_client( $_, $output ) for keys %{ $self->{clients} }; return 1; } q{Putting the test into POE}; __END__ =pod =encoding UTF-8 =head1 NAME Test::POE::Server::TCP - A POE Component providing TCP server services for test cases =head1 VERSION version 1.20 =head1 SYNOPSIS A very simple echo server with logging of requests by each client: use strict; use POE; use Test::POE::Server::TCP; POE::Session->create( package_states => [ 'main' => [qw( _start testd_connected testd_disconnected testd_client_input )], ], ); $poe_kernel->run(); exit 0; sub _start { # Spawn the Test::POE::Server::TCP server. $_[HEAP]->{testd} = Test::POE::Server::TCP->spawn( address => '127.0.0.1', port => 0, ); return; } sub testd_connected { my ($heap,$id) = @_[HEAP,ARG0]; # A client connected the unique ID is in ARG0 # Create a blank arrayref for this client on *our* heap $heap->{clients}->{ $id } = [ ]; return; } sub testd_client_input { my ($kernel,$heap,$sender,$id,$input) = @_[KERNEL,HEAP,SENDER,ARG0,ARG1]; # The client sent us a line of input # lets store it push @{ $heap->{clients}->{ $id } }, $input; # Okay, we are an echo server so lets send it back to the client # We know the SENDER so can always obtain the server object. my $testd = $sender->get_heap(); $testd->send_to_client( $id, $input ); # Or even # $sender->get_heap()->send_to_client( $id, $input ); # Alternatively we could just post back to the SENDER # $kernel->post( $sender, 'send_to_client', $id, $input ); return; } sub testd_disconnected { my ($heap,$id) = @_[HEAP,ARG0]; # Client disconnected for whatever reason # We need to free up our storage delete $heap->{clients}->{ $id }; return; } Using the module in a testcase: use strict; use Test::More; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use Test::POE::Server::TCP; plan tests => 5; my @data = ( 'This is a test', 'This is another test', 'This is the last test', ); POE::Session->create( package_states => [ 'main' => [qw( _start _sock_up _sock_fail _sock_in _sock_err testd_connected testd_disconnected testd_client_input )], ], heap => { data => \@data, }, ); $poe_kernel->run(); exit 0; sub _start { $_[HEAP]->{testd} = Test::POE::Server::TCP->spawn( address => '127.0.0.1', port => 0, ); return; } sub testd_registered { my ($heap,$object) = @_[HEAP,ARG0]; $heap->{port} = $object->port(); $heap->{factory} = POE::Wheel::SocketFactory->new( RemoteAddress => '127.0.0.1', RemotePort => $heap->{port}, SuccessEvent => '_sock_up', FailureEvent => '_sock_fail', ); return; } sub _sock_up { my ($heap,$socket) = @_[HEAP,ARG0]; delete $heap->{factory}; $heap->{socket} = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{socket}->put( $heap->{data}->[0] ); return; } sub _sock_fail { my $heap = $_[HEAP]; delete $heap->{factory}; $heap->{testd}->shutdown(); return; } sub _sock_in { my ($heap,$input) = @_[HEAP,ARG0]; my $data = shift @{ $heap->{data} }; ok( $input eq $data, 'Data matched' ); unless ( scalar @{ $heap->{data} } ) { delete $heap->{socket}; return; } $heap->{socket}->put( $heap->{data}->[0] ); return; } sub _sock_err { delete $_[HEAP]->{socket}; return; } sub testd_connected { my ($heap,$state,$id) = @_[HEAP,STATE,ARG0]; pass($state); return; } sub testd_disconnected { pass($_[STATE]); $poe_kernel->post( $_[SENDER], 'shutdown' ); return; } sub testd_client_input { my ($sender,$id,$input) = @_[SENDER,ARG0,ARG1]; my $testd = $_[SENDER]->get_heap(); $testd->send_to_client( $id, $input ); return; } =head1 DESCRIPTION Test::POE::Server::TCP is a L component that provides a TCP server framework for inclusion in client component test cases, instead of having to roll your own. Once registered with the component, a session will receive events related to client connects, disconnects, input and flushed output. Each of these events will refer to a unique client ID which may be used in communication with the component when sending data to the client or disconnecting a client connection. If AF_INET6 sockets are supported the component with create an AF_INET and an AF_INET6 socket. =head1 CONSTRUCTOR =over =item C Takes a number of optional arguments: 'alias', set an alias on the component; 'address', bind the listening socket to a particular address; 'port', listen on a particular port, default is 0, assign a random port; 'options', a hashref of POE::Session options; 'filter', specify a POE::Filter to use for client connections, default is POE::Filter::Line; 'inputfilter', specify a POE::Filter for client input; 'outputfilter', specify a POE::Filter for output to clients; 'prefix', specify a different prefix than 'testd' for events; The semantics for C, C and C are the same as for L in that one may provide either a C, C or an C. If the component is Ced within another session it will automatically C the parent session to receive C events. =back =head1 METHODS =over =item C Returns the POE::Session ID of the component. =item C Terminates the component. Shuts down the listener and disconnects connected clients. =item C Send some output to a connected client. First parameter must be a valid client id. Second parameter is a string of text to send. The second parameter may also be an arrayref of items to send to the client. If the filter you have used requires an arrayref as input, nest that arrayref within another arrayref. =item C Send some output to all connected clients. The parameter is a string of text to send. The parameter may also be an arrayref of items to send to the clients. If the filter you have used requires an arrayref as input, nest that arrayref within another arrayref. =item C Retrieve socket information of a given client. Requires a valid client ID as a parameter. If called in a list context it returns a list consisting of, in order, the client address, the client TCP port, our address and our TCP port. In a scalar context it returns a HASHREF with the following keys: 'peeraddr', the client address; 'peerport', the client TCP port; 'sockaddr', our address; 'sockport', our TCP port; =item C Retrieve the L object of a given client. Requires a valid client ID as a parameter. This enables one to manipulate the given L object, say to switch L. =item C Places a client connection in pending disconnect state. Requires a valid client ID as a parameter. Set this, then send an applicable message to the client using send_to_client() and the client connection will be terminated. =item C Immediately disconnects a client conenction. Requires a valid client ID as a parameter. =item C Stops the underlying listening socket from accepting new connections. This lets you test whether you handle the connection timing out gracefully. =item C The companion of C =item C Access to the L method of the underlying listening AF_INET socket. =item C Returns the port that the component is listening on. =item C Access to the L method of the underlying listening AF_INET6 socket. =item C Returns the port that the component is listening on for AF_INET6. =item C If the listener fails on C you can attempt to restart it with this. =back =head1 INPUT EVENTS These are events that the component will accept: =over =item C Takes N arguments: a list of event names that your session wants to listen for, minus the 'testd_' prefix. Registering for 'all' will cause it to send all TESTD-related events to you; this is the easiest way to handle it. =item C Takes N arguments: a list of event names which you don't want to receive. If you've previously done a 'register' for a particular event which you no longer care about, this event will tell the POP3D to stop sending them to you. (If you haven't, it just ignores you. No big deal). =item C Terminates the component. Shuts down the listener and disconnects connected clients. =item C Send some output to a connected client. First parameter must be a valid client id. Second parameter is a string of text to send. The second parameter may also be an arrayref of items to send to the client. If the filter you have used requires an arrayref as input, nest that arrayref within another arrayref. =item C Send some output to all connected clients. The parameter is a string of text to send. The parameter may also be an arrayref of items to send to the clients. If the filter you have used requires an arrayref as input, nest that arrayref within another arrayref. =item C Places a client connection in pending disconnect state. Requires a valid client ID as a parameter. Set this, then send an applicable message to the client using send_to_client() and the client connection will be terminated. =item C Immediately disconnects a client conenction. Requires a valid client ID as a parameter. =item C If the listener fails on C you can attempt to restart it with this. =back =head1 OUTPUT EVENTS The component sends the following events to registered sessions. If you have changed the C option in C then substitute C with the event prefix that you specified. =over =item C This event is sent to a registering session. ARG0 is the Test::POE::Server::TCP object. =item C Generated if the component cannot either start a listener or there is a problem accepting client connections. ARG0 contains the name of the operation that failed. ARG1 and ARG2 hold numeric and string values for $!, respectively. If the operation was C, the component will remove the listener. You may attempt to start it again using C. =item C Generated whenever a client connects to the component. ARG0 is the client ID, ARG1 is the client's IP address, ARG2 is the client's TCP port. ARG3 is our IP address and ARG4 is our socket port. =item C Generated whenever a client disconnects. ARG0 was the client ID, ARG1 was the client's IP address, ARG2 was the client's TCP port. ARG3 was our IP address and ARG4 was our socket port. =item C Generated whenever a client sends us some traffic. ARG0 is the client ID, ARG1 is the data sent ( tokenised by whatever POE::Filter you specified ). =item C Generated whenever anything we send to the client is actually flushed down the 'line'. ARG0 is the client ID. =back =head1 CREDITS This module uses code borrowed from L by Rocco Caputo, Ann Barcomb and Jos Boumans. =head1 SEE ALSO L L =head1 AUTHOR Chris Williams =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2016 by Chris Williams, Rocco Caputo, Ann Barcomb and Jos Boumans. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut