Test-POE-Client-TCP-1.12000755001751000144 012073554717 14301 5ustar00chrisusers000000000000README100644001751000144 2623312073554717 15270 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12NAME Test::POE::Client::TCP - A POE Component providing TCP client services for test cases VERSION version 1.12 SYNOPSIS use strict; use Socket; use Test::More tests => 15; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use Test::POE::Client::TCP; my @data = ( 'This is a test', 'This is another test', 'This is the last test', ); POE::Session->create( package_states => [ 'main' => [qw( _start _accept _failed _sock_in _sock_err testc_registered testc_connected testc_disconnected testc_input testc_flushed )], ], heap => { data => \@data, }, ); $poe_kernel->run(); exit 0; sub _start { my ($kernel,$heap) = @_[KERNEL,HEAP]; $heap->{listener} = POE::Wheel::SocketFactory->new( BindAddress => '127.0.0.1', SuccessEvent => '_accept', FailureEvent => '_failed', 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 ); $heap->{testc} = Test::POE::Client::TCP->spawn(); return; } sub _accept { my ($kernel,$heap,$socket) = @_[KERNEL,HEAP,ARG0]; my $wheel = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{wheels}->{ $wheel->ID } = $wheel; return; } sub _failed { my ($kernel,$heap,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,HEAP,ARG0..ARG3]; die "Wheel $wheel_id generated $operation error $errnum: $errstr\n"; return; } sub _sock_in { my ($heap,$input,$wheel_id) = @_[HEAP,ARG0,ARG1]; pass('Got input from client'); $heap->{wheels}->{ $wheel_id }->put( $input ) if $heap->{wheels}->{ $wheel_id }; return; } sub _sock_err { my ($heap,$wheel_id) = @_[HEAP,ARG3]; pass('Client disconnected'); delete $heap->{wheels}->{ $wheel_id }; return; } sub testc_registered { my ($kernel,$sender,$object) = @_[KERNEL,SENDER,ARG0]; pass($_[STATE]); my $port = ( sockaddr_in( $_[HEAP]->{listener}->getsockname() ) )[0]; $kernel->post( $sender, 'connect', { address => '127.0.0.1', port => $port } ); return; } sub testc_connected { my ($kernel,$heap,$sender) = @_[KERNEL,HEAP,SENDER]; pass($_[STATE]); $kernel->post( $sender, 'send_to_server', $heap->{data}->[0] ); return; } sub testc_flushed { pass($_[STATE]); return; } sub testc_input { my ($heap,$input) = @_[HEAP,ARG0]; pass('Got something back from the server'); my $data = shift @{ $heap->{data} }; ok( $input eq $data, "Data matched: '$input'" ); unless ( scalar @{ $heap->{data} } ) { $heap->{testc}->terminate(); return; } $poe_kernel->post( $_[SENDER], 'send_to_server', $heap->{data}->[0] ); return; } sub testc_disconnected { my ($heap,$state) = @_[HEAP,STATE]; pass($state); delete $heap->{wheels}; delete $heap->{listener}; $heap->{testc}->shutdown(); return; } DESCRIPTION Test::POE::Client::TCP is a POE component that provides a TCP client 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 connections made, disconnects, flushed output and input from the specified server. CONSTRUCTOR "spawn" Takes a number of optional arguments: 'alias', set an alias on the component; 'address', the remote address to connect to; 'port', the remote port to connect to; '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; 'localaddr', specify that connections be made from a particular local address; 'localport', specify that connections be made from a particular port; 'autoconnect', set to a true value to make the poco connect immediately; 'prefix', specify an event prefix other than the default of 'testc'; 'timeout', specify number of seconds to wait for socket timeouts; 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 "spawn"ed within another session it will automatically "register" the parent session to receive "all" events. "address" and "port" are optional within "spawn", but if they aren't specified they must be provided to subsequent "connect"s. If "autoconnect" is specified, "address" and "port" must also be defined. METHODS "connect" Initiates a connection to the given server. Takes a number of parameters: 'address', the remote address to connect to; 'port', the remote port to connect to; 'localaddr', specify that connections be made from a particular local address, optional; 'localport', specify that connections be made from a particular port, optional; "address" and "port" are optional if they have been already specified during "spawn". "session_id" Returns the POE::Session ID of the component. "shutdown" Terminates the component. It will terminate any pending connects or connections. "server_info" Retrieves socket information about the current connection. In a list context it returns a list consisting of, in order, the server address, the server TCP port, our address and our TCP port. In a scalar context it returns a HASHREF with the following keys: 'peeraddr', the server address; 'peerport', the server TCP port; 'sockaddr', our address; 'sockport', our TCP port; "send_to_server" Send some output to the connected server. The first parameter is a string of text to send. This 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. "disconnect" Places the server connection into pending disconnect state. Set this, then send an applicable message to the server using "send_to_server()" and the server connection will be terminated. "terminate" Immediately disconnects a server conenction. "wheel" Returns the underlying POE::Wheel::ReadWrite object if we are currently connected to a server, "undef" otherwise. You can use this method to call methods on the wheel object to switch filters, etc. Exercise caution. "alias" Returns the currently configured alias. 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 'testc_' prefix. Registering for 'all' will cause it to send all TESTC-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 poco to stop sending them to you. (If you haven't, it just ignores you. No big deal). "connect" Initiates a connection to the given server. Takes a number of parameters: 'address', the remote address to connect to; 'port', the remote port to connect to; 'localaddr', specify that connections be made from a particular local address, optional; 'localport', specify that connections be made from a particular port, optional; "address" and "port" are optional if they have been already specified during "spawn". "shutdown" Terminates the component. It will terminate any pending connects or connections. "send_to_server" Send some output to the connected server. The first parameter is a string of text to send. This 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. "disconnect" Places the server connection into pending disconnect state. Set this, then send an applicable message to the server using "send_to_server()" and the server connection will be terminated. "terminate" Immediately disconnects a server conenction. OUTPUT EVENTS The component sends the following events to registered sessions. If you have changed the "prefix" option in "spawn" then substitute "testc" with the event prefix that you specified. "testc_registered" This event is sent to a registering session. ARG0 is the Test::POE::Client::TCP object. "testc_socket_failed" Generated if the component cannot make a socket connection. ARG0 contains the name of the operation that failed. ARG1 and ARG2 hold numeric and string values for $!, respectively. "testc_connected" Generated whenever a connection is established. ARG0 is the server's IP address, ARG1 is the server's TCP port. ARG3 is our IP address and ARG4 is our socket port. "testc_disconnected" Generated whenever we disconnect from the server. "testc_input" Generated whenever the server sends us some traffic. ARG0 is the data sent ( tokenised by whatever POE::Filter you specified ). "testc_flushed" Generated whenever anything we send to the server is actually flushed down the 'line'. KUDOS Contains 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) 2013 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. LICENSE100644001751000144 4406212073554717 15415 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12This software is copyright (c) 2013 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) 2013 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, Suite 500, Boston, MA 02110-1335 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) 2013 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 Changes100644001751000144 654212073554717 15664 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12================================================== Changes from 2008-01-12 00:00:00 +0000 to present. ================================================== ----------------------------------------- version 1.12 at 2013-01-10 14:55:52 +0000 ----------------------------------------- Change: 34a8e3a668f6c4ba1f5725971598fc8cd4eaef1b Author: Chris 'BinGOs' Williams Date : 2013-01-10 14:55:52 +0000 Bump version to 1.12 Change: 37b60d1ae2460076f82f07300b2c1f0d78412200 Author: Chris 'BinGOs' Williams Date : 2013-01-10 14:55:28 +0000 Implement a timeout option Change: 4a636064c8cceeddc33fac4095ff546d5e62e3a1 Author: Chris 'BinGOs' Williams Date : 2013-01-10 13:19:52 +0000 Update distribution to Dist::Zilla ----------------------------------------- version 1.10 at 2011-06-29 09:39:16 +0000 ----------------------------------------- Change: da4c89e75890aea9213ad2cf890fce310290d7fc Author: Chris 'BinGOs' Williams Date : 2011-06-29 10:39:16 +0000 Explicitly shutdown socket in/out on destruction under Cygwin/MSWin32 ----------------------------------------- version 1.08 at 2010-04-28 14:30:50 +0000 ----------------------------------------- Change: e01e068cabdaa0afc907422655732ec0a49831ae Author: Chris Williams Date : 2010-04-28 15:30:50 +0000 Mod changes Change: 89cf253e9a3ab2175184e5d242b12c3f3eb3e3f5 Author: Chris Williams Date : 2010-04-28 15:29:55 +0000 Bump version ----------------------------------------- version 1.06 at 2009-10-26 23:23:08 +0000 ----------------------------------------- Change: adcbdc62c056aa5849a78fcb5fd8c34ddca58600 Author: Chris Williams Date : 2009-10-26 23:23:08 +0000 Amended tests and bumped required POE version Change: 61bce98e0e218b96faa360e6bef98a9ae3f34033 Author: Chris Williams Date : 2009-06-17 23:43:35 +0000 Updated MANIFEST ----------------------------------------- version 1.04 at 2009-06-17 22:41:06 +0000 ----------------------------------------- Change: 7c94933cad841febeadbd78860a93dddc45b0477 Author: Chris Williams Date : 2009-06-17 23:41:06 +0000 Added accessor for alias ----------------------------------------- version 1.02 at 2009-04-07 20:06:59 +0000 ----------------------------------------- Change: f95144656b122e51b1a5e315886a7e87e0c7c691 Author: Chris Williams Date : 2009-04-07 21:06:59 +0000 Make auto_set_repository() only run at author-time Change: 20e5748aee88d043b72227046d78ab6891759710 Author: Chris Williams Date : 2009-04-07 21:06:40 +0000 Make auto_set_repository() only run at author-time ----------------------------------------- version 1.00 at 2009-04-06 21:54:24 +0000 ----------------------------------------- Change: fd17e74af7af11027eba7fa28ac08cbad7fa90bc Author: Chris Williams Date : 2009-04-06 22:54:24 +0000 Bump to a 'stable' version. Add auto_set_repository to Makefile.PL Change: a069cbd3fc6d67413a592512a74c1653e8e8df82 Author: Chris Williams Date : 2009-03-12 11:52:11 +0000 Initial commit ================ End of releases. ================ dist.ini100644001751000144 64112073554717 16007 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12name = Test-POE-Client-TCP version = 1.12 author = Chris Williams license = Perl_5 copyright_holder = Chris Williams, Rocco Caputo, Ann Barcomb and Jos Boumans [@BINGOS] [Prereqs / BuildRequires] ExtUtils::MakeMaker = 6.59 Test::More = 0.47 Text::ParseWords = 0 [Prereqs] POE = 1.28 POE::Filter = 0 POE::Filter::Line = 0 POE::Wheel::ReadWrite = 0 POE::Wheel::SocketFactory = 0 perl = 5.006 META.yml100644001751000144 141012073554717 15627 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12--- abstract: 'A POE Component providing TCP client services for test cases' author: - 'Chris Williams ' build_requires: ExtUtils::MakeMaker: 6.59 Test::More: 0.47 Text::ParseWords: 0 configure_requires: ExtUtils::MakeMaker: 6.30 dynamic_config: 0 generated_by: 'Dist::Zilla version 4.300028, CPAN::Meta::Converter version 2.120921' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: Test-POE-Client-TCP requires: POE: 1.28 POE::Filter: 0 POE::Filter::Line: 0 POE::Wheel::ReadWrite: 0 POE::Wheel::SocketFactory: 0 perl: 5.006 resources: homepage: https://github.com/bingos/test-poe-client-tcp repository: https://github.com/bingos/test-poe-client-tcp.git version: 1.12 MANIFEST100644001751000144 57312073554717 15500 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12Changes Changes.old LICENSE MANIFEST META.json META.yml Makefile.PL README dist.ini examples/synopsis.pl lib/Test/POE/Client/TCP.pm t/00-compile.t t/00_use.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_autoconnect.t t/09_server_info.t t/10_prefix.t t/release-pod-coverage.t t/release-pod-syntax.t META.json100644001751000144 304412073554717 16004 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12{ "abstract" : "A POE Component providing TCP client services for test cases", "author" : [ "Chris Williams " ], "dynamic_config" : 0, "generated_by" : "Dist::Zilla version 4.300028, CPAN::Meta::Converter version 2.120921", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Test-POE-Client-TCP", "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "6.59", "Test::More" : "0.47", "Text::ParseWords" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "6.30" } }, "develop" : { "requires" : { "Pod::Coverage::TrustPod" : "0", "Test::Pod" : "1.41", "Test::Pod::Coverage" : "1.08" } }, "runtime" : { "requires" : { "POE" : "1.28", "POE::Filter" : "0", "POE::Filter::Line" : "0", "POE::Wheel::ReadWrite" : "0", "POE::Wheel::SocketFactory" : "0", "perl" : "5.006" } } }, "release_status" : "stable", "resources" : { "homepage" : "https://github.com/bingos/test-poe-client-tcp", "repository" : { "type" : "git", "url" : "https://github.com/bingos/test-poe-client-tcp.git", "web" : "https://github.com/bingos/test-poe-client-tcp" } }, "version" : "1.12" } t000755001751000144 012073554720 14457 5ustar00chrisusers000000000000Test-POE-Client-TCP-1.1200_use.t100644001751000144 7512073554720 16041 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12/tuse Test::More tests => 1; use_ok('Test::POE::Client::TCP'); Changes.old100644001751000144 165712073554720 16435 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12Test::POE::Client::TCP ====================== 1.10 Wed Jun 29 10:37:32 BST 2011 - Explicitly shutdown socket in/out on destruction under Cygwin/MSWin32 1.08 Wed Apr 28 15:30:13 BST 2010 - The 'oh fuck' I deleted it from CPAN release 1.06 Mon Oct 26 23:22:03 GMT 2009 - Amended tests and bumped required POE version 1.04 Wed Jun 17 23:39:16 BST 2009 - Added accessor for alias. 1.02 Tue Apr 7 21:06:02 BST 2009 - Make auto_set_repository() only run at author-time 1.00 Mon Apr 6 22:52:02 BST 2009 - Bump to 'stable' version - add auto_set_repository to Makefile.PL 0.10 Tue Jan 20 12:27:34 GMT 2009 - Added 'wheel' method to get the POE::Wheel::ReadWrite object. 0.08 Thu Jan 15 10:33:50 GMT 2009 - Enabled event 'prefix' to be specified. 0.06 Tue Jul 1 10:28:19 BST 2008 - Managed to delete distro from CPAN, restoring. 0.04 Tue May 20 18:02:45 BST 2008 - added server_info() method. 0.02 - Initial CPAN release Makefile.PL100644001751000144 236212073554720 16331 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12 use strict; use warnings; use 5.006; use ExtUtils::MakeMaker 6.30; my %WriteMakefileArgs = ( "ABSTRACT" => "A POE Component providing TCP client services for test cases", "AUTHOR" => "Chris Williams ", "BUILD_REQUIRES" => { "ExtUtils::MakeMaker" => "6.59", "Test::More" => "0.47", "Text::ParseWords" => 0 }, "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => "6.30" }, "DISTNAME" => "Test-POE-Client-TCP", "EXE_FILES" => [], "LICENSE" => "perl", "NAME" => "Test::POE::Client::TCP", "PREREQ_PM" => { "POE" => "1.28", "POE::Filter" => 0, "POE::Filter::Line" => 0, "POE::Wheel::ReadWrite" => 0, "POE::Wheel::SocketFactory" => 0 }, "VERSION" => "1.12", "test" => { "TESTS" => "t/*.t" } ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.56) } ) { my $br = delete $WriteMakefileArgs{BUILD_REQUIRES}; my $pp = $WriteMakefileArgs{PREREQ_PM}; for my $mod ( keys %$br ) { if ( exists $pp->{$mod} ) { $pp->{$mod} = $br->{$mod} if $br->{$mod} > $pp->{$mod}; } else { $pp->{$mod} = $br->{$mod}; } } } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); 01_spawn.t100644001751000144 540512073554720 16440 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12/tuse strict; use Socket; use Test::More tests => 12; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use_ok('Test::POE::Client::TCP'); POE::Session->create( package_states => [ 'main' => [qw( _start _accept _failed _sock_in _sock_err testc_registered testc_connected testc_disconnected testc_input testc_flushed )], ], ); $poe_kernel->run(); exit 0; sub _start { my ($kernel,$heap) = @_[KERNEL,HEAP]; $heap->{listener} = POE::Wheel::SocketFactory->new( BindAddress => '127.0.0.1', SuccessEvent => '_accept', FailureEvent => '_failed', 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 ); $heap->{testc} = Test::POE::Client::TCP->spawn(); isa_ok( $heap->{testc}, 'Test::POE::Client::TCP' ); return; } sub _accept { my ($kernel,$heap,$socket) = @_[KERNEL,HEAP,ARG0]; my $wheel = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{wheels}->{ $wheel->ID } = $wheel; return; } sub _failed { my ($kernel,$heap,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,HEAP,ARG0..ARG3]; die "Wheel $wheel_id generated $operation error $errnum: $errstr\n"; return; } sub _sock_in { my ($heap,$input,$wheel_id) = @_[HEAP,ARG0,ARG1]; pass('Got input from client'); $heap->{wheels}->{ $wheel_id }->put( $input ) if $heap->{wheels}->{ $wheel_id }; return; } sub _sock_err { my ($heap,$wheel_id) = @_[HEAP,ARG3]; pass('Client disconnected'); delete $heap->{wheels}->{ $wheel_id }; return; } sub testc_registered { my ($kernel,$sender,$object) = @_[KERNEL,SENDER,ARG0]; pass($_[STATE]); isa_ok( $object, 'Test::POE::Client::TCP' ); my $port = ( sockaddr_in( $_[HEAP]->{listener}->getsockname() ) )[0]; $kernel->post( $sender, 'connect', { address => '127.0.0.1', port => $port } ); return; } sub testc_connected { my ($kernel,$sender) = @_[KERNEL,SENDER]; pass($_[STATE]); isa_ok( $_[HEAP]->{testc}->wheel, 'POE::Wheel::ReadWrite' ); $kernel->post( $sender, 'send_to_server', 'Hello, is it me you are looking for?' ); return; } sub testc_flushed { pass($_[STATE]); return; } sub testc_input { my ($heap,$input) = @_[HEAP,ARG0]; pass('Got something back from the server'); ok( $input eq 'Hello, is it me you are looking for?', $input ); $heap->{testc}->terminate(); return; } sub testc_disconnected { my ($heap,$state) = @_[HEAP,STATE]; pass($state); delete $heap->{wheels}; delete $heap->{listener}; $heap->{testc}->shutdown(); return; } 06_filter.t100644001751000144 1013712073554720 16620 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12/tuse strict; { package TPCTParseWords; 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 tests => 10; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use_ok('Test::POE::Client::TCP'); POE::Session->create( package_states => [ 'main' => [qw( _start _accept _failed _sock_in _sock_err testc_registered testc_connected testc_disconnected testc_input testc_flushed )], ], ); $poe_kernel->run(); exit 0; sub _start { my ($kernel,$heap) = @_[KERNEL,HEAP]; $heap->{listener} = POE::Wheel::SocketFactory->new( BindAddress => '127.0.0.1', SuccessEvent => '_accept', FailureEvent => '_failed', 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 ); $heap->{testc} = Test::POE::Client::TCP->spawn( inputfilter => TPCTParseWords->new(), outputfilter => POE::Filter::Line->new(), ); isa_ok( $heap->{testc}, 'Test::POE::Client::TCP' ); return; } sub _accept { my ($kernel,$heap,$socket) = @_[KERNEL,HEAP,ARG0]; my $wheel = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{wheels}->{ $wheel->ID } = $wheel; $wheel->put('"This is just a test" "line" "so there"'); return; } sub _failed { my ($kernel,$heap,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,HEAP,ARG0..ARG3]; die "Wheel $wheel_id generated $operation error $errnum: $errstr\n"; return; } sub _sock_in { my ($heap,$input,$wheel_id) = @_[HEAP,ARG0,ARG1]; pass('Got input from client'); # $heap->{wheels}->{ $wheel_id }->put( $input ) if $heap->{wheels}->{ $wheel_id }; delete $heap->{wheels}->{ $wheel_id } if $input eq 'quit'; return; } sub _sock_err { my ($heap,$wheel_id) = @_[HEAP,ARG3]; pass('Client disconnected'); delete $heap->{wheels}->{ $wheel_id }; return; } sub testc_registered { my ($kernel,$sender,$object) = @_[KERNEL,SENDER,ARG0]; pass($_[STATE]); isa_ok( $object, 'Test::POE::Client::TCP' ); my $port = ( sockaddr_in( $_[HEAP]->{listener}->getsockname() ) )[0]; $kernel->post( $sender, 'connect', { address => '127.0.0.1', port => $port } ); return; } sub testc_connected { my ($kernel,$sender) = @_[KERNEL,SENDER]; pass($_[STATE]); # $kernel->post( $sender, 'send_to_server', 'Hello, is it me you are looking for?' ); return; } sub testc_flushed { pass($_[STATE]); return; } sub testc_input { my ($heap,$input) = @_[HEAP,ARG0]; pass('Got something back from the server'); # ok( $input eq 'Hello, is it me you are looking for?', $input ); ok( ( $input->[0] eq 'This is just a test' and $input->[1] eq 'line' and $input->[2] eq 'so there' ) , 'Test Get' ); $heap->{testc}->send_to_server('quit'); return; } sub testc_disconnected { my ($heap,$state) = @_[HEAP,STATE]; pass($state); delete $heap->{wheels}; delete $heap->{listener}; $heap->{testc}->shutdown(); return; } 08_buffer.t100644001751000144 550212073554720 16566 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12/tuse strict; use Socket; use Test::More tests => 14; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use_ok('Test::POE::Client::TCP'); my @data = ( 'This is a test', 'This is another test', 'This is the last test', ); POE::Session->create( package_states => [ 'main' => [qw( _start _accept _failed _sock_in _sock_err testc_registered testc_connected testc_disconnected testc_input testc_flushed )], ], heap => { data => \@data, }, ); $poe_kernel->run(); exit 0; sub _start { my ($kernel,$heap) = @_[KERNEL,HEAP]; $heap->{listener} = POE::Wheel::SocketFactory->new( BindAddress => '127.0.0.1', SuccessEvent => '_accept', FailureEvent => '_failed', 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 ); $heap->{count} = @{ $heap->{data} }; $heap->{testc} = Test::POE::Client::TCP->spawn(); isa_ok( $heap->{testc}, 'Test::POE::Client::TCP' ); return; } sub _accept { my ($kernel,$heap,$socket) = @_[KERNEL,HEAP,ARG0]; my $wheel = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{wheels}->{ $wheel->ID } = $wheel; return; } sub _failed { my ($kernel,$heap,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,HEAP,ARG0..ARG3]; die "Wheel $wheel_id generated $operation error $errnum: $errstr\n"; return; } sub _sock_in { my ($heap,$input,$wheel_id) = @_[HEAP,ARG0,ARG1]; pass('Got input from client'); $heap->{wheels}->{ $wheel_id }->put( $input ) if $heap->{wheels}->{ $wheel_id }; return; } sub _sock_err { my ($heap,$wheel_id) = @_[HEAP,ARG3]; pass('Client disconnected'); delete $heap->{wheels}->{ $wheel_id }; return; } sub testc_registered { my ($kernel,$sender,$object) = @_[KERNEL,SENDER,ARG0]; pass($_[STATE]); isa_ok( $object, 'Test::POE::Client::TCP' ); my $port = ( sockaddr_in( $_[HEAP]->{listener}->getsockname() ) )[0]; $kernel->post( $sender, 'connect', { address => '127.0.0.1', port => $port } ); return; } sub testc_connected { my ($kernel,$sender) = @_[KERNEL,SENDER]; pass($_[STATE]); $kernel->post( $sender, 'send_to_server', [ @{ $_[HEAP]->{data} } ] ); return; } sub testc_flushed { pass($_[STATE]); return; } sub testc_input { my ($heap,$input) = @_[HEAP,ARG0]; pass('Got something back from the server'); $heap->{count}--; $heap->{testc}->terminate() if $heap->{count} <= 0; return; } sub testc_disconnected { my ($heap,$state) = @_[HEAP,STATE]; pass($state); delete $heap->{wheels}; delete $heap->{listener}; $heap->{testc}->shutdown(); return; } 10_prefix.t100644001751000144 562612073554720 16612 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12/tuse strict; use Socket; use Test::More tests => 16; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use Test::POE::Client::TCP; my @data = ( 'This is a test', 'This is another test', 'This is the last test', ); POE::Session->create( package_states => [ 'main' => [qw( _start _accept _failed _sock_in _sock_err bingosc_registered bingosc_connected bingosc_disconnected bingosc_input bingosc_flushed )], ], heap => { data => \@data, }, ); $poe_kernel->run(); exit 0; sub _start { my ($kernel,$heap) = @_[KERNEL,HEAP]; $heap->{listener} = POE::Wheel::SocketFactory->new( BindAddress => '127.0.0.1', SuccessEvent => '_accept', FailureEvent => '_failed', 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 ); $heap->{bingosc} = Test::POE::Client::TCP->spawn( prefix => 'bingosc' ); return; } sub _accept { my ($kernel,$heap,$socket) = @_[KERNEL,HEAP,ARG0]; my $wheel = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{wheels}->{ $wheel->ID } = $wheel; return; } sub _failed { my ($kernel,$heap,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,HEAP,ARG0..ARG3]; die "Wheel $wheel_id generated $operation error $errnum: $errstr\n"; return; } sub _sock_in { my ($heap,$input,$wheel_id) = @_[HEAP,ARG0,ARG1]; pass('Got input from client'); $heap->{wheels}->{ $wheel_id }->put( $input ) if $heap->{wheels}->{ $wheel_id }; return; } sub _sock_err { my ($heap,$wheel_id) = @_[HEAP,ARG3]; pass('Client disconnected'); delete $heap->{wheels}->{ $wheel_id }; return; } sub bingosc_registered { my ($kernel,$sender,$object) = @_[KERNEL,SENDER,ARG0]; pass($_[STATE]); my $port = ( sockaddr_in( $_[HEAP]->{listener}->getsockname() ) )[0]; $kernel->post( $sender, 'connect', { address => '127.0.0.1', port => $port } ); return; } sub bingosc_connected { my ($kernel,$heap,$sender) = @_[KERNEL,HEAP,SENDER]; pass($_[STATE]); $kernel->post( $sender, 'send_to_server', $heap->{data}->[0] ); return; } sub bingosc_flushed { pass($_[STATE]); return; } sub bingosc_input { my ($heap,$input) = @_[HEAP,ARG0]; pass('Got something back from the server'); my $data = shift @{ $heap->{data} }; ok( $input eq $data, "Data matched: '$input'" ); unless ( scalar @{ $heap->{data} } ) { $heap->{bingosc}->terminate(); return; } $poe_kernel->post( $_[SENDER], 'send_to_server', $heap->{data}->[0] ); return; } sub bingosc_disconnected { my ($heap,$state) = @_[HEAP,STATE]; pass($state); delete $heap->{wheels}; delete $heap->{listener}; $heap->{bingosc}->shutdown(); return; } 00-compile.t100644001751000144 312512073554720 16652 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12/t#!perl use strict; use warnings; use Test::More; use File::Find; use File::Temp qw{ tempdir }; my @modules; find( sub { return if $File::Find::name !~ /\.pm\z/; my $found = $File::Find::name; $found =~ s{^lib/}{}; $found =~ s{[/\\]}{::}g; $found =~ s/\.pm$//; # nothing to skip push @modules, $found; }, 'lib', ); sub _find_scripts { my $dir = shift @_; my @found_scripts = (); find( sub { return unless -f; my $found = $File::Find::name; # nothing to skip open my $FH, '<', $_ or do { note( "Unable to open $found in ( $! ), skipping" ); return; }; my $shebang = <$FH>; return unless $shebang =~ /^#!.*?\bperl\b\s*$/; push @found_scripts, $found; }, $dir, ); return @found_scripts; } my @scripts; do { push @scripts, _find_scripts($_) if -d $_ } for qw{ bin script scripts }; my $plan = scalar(@modules) + scalar(@scripts); $plan ? (plan tests => $plan) : (plan skip_all => "no tests to run"); { # fake home for cpan-testers # no fake requested ## local $ENV{HOME} = tempdir( CLEANUP => 1 ); like( qx{ $^X -Ilib -e "require $_; print '$_ ok'" }, qr/^\s*$_ ok/s, "$_ loaded ok" ) for sort @modules; SKIP: { eval "use Test::Script 1.05; 1;"; skip "Test::Script needed to test script compilation", scalar(@scripts) if $@; foreach my $file ( @scripts ) { my $script = $file; $script =~ s!.*/!!; script_compiles( $file, "$script script compiles" ); } } } 04_multiple.t100644001751000144 562212073554720 17147 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12/tuse strict; use Socket; use Test::More tests => 21; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use_ok('Test::POE::Client::TCP'); POE::Session->create( package_states => [ 'main' => [qw( _start _accept _failed _sock_in _sock_err testc_registered testc_connected testc_disconnected testc_input testc_flushed )], ], heap => { clients => 2, }, ); $poe_kernel->run(); exit 0; sub _start { my ($kernel,$heap) = @_[KERNEL,HEAP]; $heap->{listener} = POE::Wheel::SocketFactory->new( BindAddress => '127.0.0.1', SuccessEvent => '_accept', FailureEvent => '_failed', 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 ); for ( 1 .. $heap->{clients} ) { my $testc = Test::POE::Client::TCP->spawn(); isa_ok( $testc, 'Test::POE::Client::TCP' ); push @{ $heap->{testc} }, $testc; } return; } sub _accept { my ($kernel,$heap,$socket) = @_[KERNEL,HEAP,ARG0]; my $wheel = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{wheels}->{ $wheel->ID } = $wheel; return; } sub _failed { my ($kernel,$heap,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,HEAP,ARG0..ARG3]; die "Wheel $wheel_id generated $operation error $errnum: $errstr\n"; return; } sub _sock_in { my ($heap,$input,$wheel_id) = @_[HEAP,ARG0,ARG1]; pass('Got input from client'); $heap->{wheels}->{ $wheel_id }->put( $input ) if $heap->{wheels}->{ $wheel_id }; return; } sub _sock_err { my ($heap,$wheel_id) = @_[HEAP,ARG3]; pass('Client disconnected'); delete $heap->{wheels}->{ $wheel_id }; return; } sub testc_registered { my ($kernel,$sender,$object) = @_[KERNEL,SENDER,ARG0]; pass($_[STATE]); isa_ok( $object, 'Test::POE::Client::TCP' ); my $port = ( sockaddr_in( $_[HEAP]->{listener}->getsockname() ) )[0]; $kernel->post( $sender, 'connect', { address => '127.0.0.1', port => $port } ); return; } sub testc_connected { my ($kernel,$sender) = @_[KERNEL,SENDER]; pass($_[STATE]); $kernel->post( $sender, 'send_to_server', 'Hello, is it me you are looking for?' ); return; } sub testc_flushed { pass($_[STATE]); return; } sub testc_input { my ($heap,$input) = @_[HEAP,ARG0]; pass('Got something back from the server'); ok( $input eq 'Hello, is it me you are looking for?', $input ); $poe_kernel->post( $_[SENDER], 'terminate' ); return; } sub testc_disconnected { my ($heap,$state) = @_[HEAP,STATE]; pass($state); $heap->{count}++; if ( $heap->{count} >= $heap->{clients} ) { $_->shutdown() for @{ $heap->{testc} }; delete $heap->{wheels}; delete $heap->{listener}; } return; } 03_register.t100644001751000144 121712073554720 17133 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12/tuse strict; use Test::More tests => 6; use POE; use_ok( 'Test::POE::Client::TCP' ); POE::Session->create( package_states => [ 'main' => [qw( _start _stop testc_registered )], ], ); $poe_kernel->run(); exit 0; sub _start { $_[HEAP]->{testc} = Test::POE::Client::TCP->spawn(); isa_ok( $_[HEAP]->{testc}, 'Test::POE::Client::TCP' ); pass($_[STATE]); return; } sub _stop { $_[HEAP]->{testc}->shutdown; pass($_[STATE]); return; } sub testc_registered { my ($sender,$object) = @_[SENDER,ARG0]; pass($_[STATE]); isa_ok( $object, 'Test::POE::Client::TCP' ); $poe_kernel->post( $sender, 'unregister', 'all' ); return; } 05_subclass.t100644001751000144 551012073554720 17130 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12/tuse strict; { package TPCTSubclass; use base qw(Test::POE::Client::TCP); my $VERSION = '0.01'; } use Socket; use Test::More tests => 12; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); POE::Session->create( package_states => [ 'main' => [qw( _start _accept _failed _sock_in _sock_err testc_registered testc_connected testc_disconnected testc_input testc_flushed )], ], ); $poe_kernel->run(); exit 0; sub _start { my ($kernel,$heap) = @_[KERNEL,HEAP]; $heap->{listener} = POE::Wheel::SocketFactory->new( BindAddress => '127.0.0.1', SuccessEvent => '_accept', FailureEvent => '_failed', 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 ); $heap->{testc} = TPCTSubclass->spawn(); isa_ok( $heap->{testc}, 'Test::POE::Client::TCP' ); isa_ok( $heap->{testc}, 'TPCTSubclass' ); return; } sub _accept { my ($kernel,$heap,$socket) = @_[KERNEL,HEAP,ARG0]; my $wheel = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{wheels}->{ $wheel->ID } = $wheel; return; } sub _failed { my ($kernel,$heap,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,HEAP,ARG0..ARG3]; die "Wheel $wheel_id generated $operation error $errnum: $errstr\n"; return; } sub _sock_in { my ($heap,$input,$wheel_id) = @_[HEAP,ARG0,ARG1]; pass('Got input from client'); $heap->{wheels}->{ $wheel_id }->put( $input ) if $heap->{wheels}->{ $wheel_id }; return; } sub _sock_err { my ($heap,$wheel_id) = @_[HEAP,ARG3]; pass('Client disconnected'); delete $heap->{wheels}->{ $wheel_id }; return; } sub testc_registered { my ($kernel,$sender,$object) = @_[KERNEL,SENDER,ARG0]; pass($_[STATE]); isa_ok( $object, 'Test::POE::Client::TCP' ); isa_ok( $object, 'TPCTSubclass' ); my $port = ( sockaddr_in( $_[HEAP]->{listener}->getsockname() ) )[0]; $kernel->post( $sender, 'connect', { address => '127.0.0.1', port => $port } ); return; } sub testc_connected { my ($kernel,$sender) = @_[KERNEL,SENDER]; pass($_[STATE]); $kernel->post( $sender, 'send_to_server', 'Hello, is it me you are looking for?' ); return; } sub testc_flushed { pass($_[STATE]); return; } sub testc_input { my ($heap,$input) = @_[HEAP,ARG0]; pass('Got something back from the server'); ok( $input eq 'Hello, is it me you are looking for?', $input ); $heap->{testc}->terminate(); return; } sub testc_disconnected { my ($heap,$state) = @_[HEAP,STATE]; pass($state); delete $heap->{wheels}; delete $heap->{listener}; $heap->{testc}->shutdown(); return; } 07_synopsis.t100644001751000144 554712073554720 17214 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12/tuse strict; use Socket; use Test::More tests => 16; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use Test::POE::Client::TCP; my @data = ( 'This is a test', 'This is another test', 'This is the last test', ); POE::Session->create( package_states => [ 'main' => [qw( _start _accept _failed _sock_in _sock_err testc_registered testc_connected testc_disconnected testc_input testc_flushed )], ], heap => { data => \@data, }, ); $poe_kernel->run(); exit 0; sub _start { my ($kernel,$heap) = @_[KERNEL,HEAP]; $heap->{listener} = POE::Wheel::SocketFactory->new( BindAddress => '127.0.0.1', SuccessEvent => '_accept', FailureEvent => '_failed', 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 ); $heap->{testc} = Test::POE::Client::TCP->spawn(); return; } sub _accept { my ($kernel,$heap,$socket) = @_[KERNEL,HEAP,ARG0]; my $wheel = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{wheels}->{ $wheel->ID } = $wheel; return; } sub _failed { my ($kernel,$heap,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,HEAP,ARG0..ARG3]; die "Wheel $wheel_id generated $operation error $errnum: $errstr\n"; return; } sub _sock_in { my ($heap,$input,$wheel_id) = @_[HEAP,ARG0,ARG1]; pass('Got input from client'); $heap->{wheels}->{ $wheel_id }->put( $input ) if $heap->{wheels}->{ $wheel_id }; return; } sub _sock_err { my ($heap,$wheel_id) = @_[HEAP,ARG3]; pass('Client disconnected'); delete $heap->{wheels}->{ $wheel_id }; return; } sub testc_registered { my ($kernel,$sender,$object) = @_[KERNEL,SENDER,ARG0]; pass($_[STATE]); my $port = ( sockaddr_in( $_[HEAP]->{listener}->getsockname() ) )[0]; $kernel->post( $sender, 'connect', { address => '127.0.0.1', port => $port } ); return; } sub testc_connected { my ($kernel,$heap,$sender) = @_[KERNEL,HEAP,SENDER]; pass($_[STATE]); $kernel->post( $sender, 'send_to_server', $heap->{data}->[0] ); return; } sub testc_flushed { pass($_[STATE]); return; } sub testc_input { my ($heap,$input) = @_[HEAP,ARG0]; pass('Got something back from the server'); my $data = shift @{ $heap->{data} }; ok( $input eq $data, "Data matched: '$input'" ); unless ( scalar @{ $heap->{data} } ) { $heap->{testc}->terminate(); return; } $poe_kernel->post( $_[SENDER], 'send_to_server', $heap->{data}->[0] ); return; } sub testc_disconnected { my ($heap,$state) = @_[HEAP,STATE]; pass($state); delete $heap->{wheels}; delete $heap->{listener}; $heap->{testc}->shutdown(); return; } 02_register.t100644001751000144 125412073554720 17133 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12/tuse strict; use Test::More tests => 6; use POE; use_ok('Test::POE::Client::TCP'); my $testc = Test::POE::Client::TCP->spawn(); isa_ok( $testc, 'Test::POE::Client::TCP' ); POE::Session->create( package_states => [ 'main' => [qw( _start _stop testc_registered )], ], ); $poe_kernel->run(); exit 0; sub _start { pass($_[STATE]); $poe_kernel->post( $testc->session_id(), 'register', 'all' ); return; } sub _stop { $testc->shutdown; pass($_[STATE]); return; } sub testc_registered { my ($sender,$object) = @_[SENDER,ARG0]; pass($_[STATE]); isa_ok( $object, 'Test::POE::Client::TCP' ); $poe_kernel->post( $sender, 'unregister', 'all' ); return; } 09_autoconnect.t100644001751000144 537512073554720 17650 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12/tuse strict; use Socket; use Test::More tests => 11; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use_ok('Test::POE::Client::TCP'); POE::Session->create( package_states => [ 'main' => [qw( _start _accept _failed _sock_in _sock_err testc_registered testc_connected testc_disconnected testc_input testc_flushed )], ], ); $poe_kernel->run(); exit 0; sub _start { my ($kernel,$heap) = @_[KERNEL,HEAP]; $heap->{listener} = POE::Wheel::SocketFactory->new( BindAddress => '127.0.0.1', SuccessEvent => '_accept', FailureEvent => '_failed', 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 ); my $port = ( sockaddr_in( $heap->{listener}->getsockname() ) )[0]; $heap->{testc} = Test::POE::Client::TCP->spawn( address => '127.0.0.1', port => $port, autoconnect => 1 ); isa_ok( $heap->{testc}, 'Test::POE::Client::TCP' ); return; } sub _accept { my ($kernel,$heap,$socket) = @_[KERNEL,HEAP,ARG0]; my $wheel = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{wheels}->{ $wheel->ID } = $wheel; return; } sub _failed { my ($kernel,$heap,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,HEAP,ARG0..ARG3]; die "Wheel $wheel_id generated $operation error $errnum: $errstr\n"; return; } sub _sock_in { my ($heap,$input,$wheel_id) = @_[HEAP,ARG0,ARG1]; pass('Got input from client'); $heap->{wheels}->{ $wheel_id }->put( $input ) if $heap->{wheels}->{ $wheel_id }; return; } sub _sock_err { my ($heap,$wheel_id) = @_[HEAP,ARG3]; pass('Client disconnected'); delete $heap->{wheels}->{ $wheel_id }; return; } sub testc_registered { my ($kernel,$sender,$object) = @_[KERNEL,SENDER,ARG0]; pass($_[STATE]); isa_ok( $object, 'Test::POE::Client::TCP' ); # $kernel->post( $sender, 'connect', { address => '127.0.0.1', port => $port } ); return; } sub testc_connected { my ($kernel,$sender) = @_[KERNEL,SENDER]; pass($_[STATE]); $kernel->post( $sender, 'send_to_server', 'Hello, is it me you are looking for?' ); return; } sub testc_flushed { pass($_[STATE]); return; } sub testc_input { my ($heap,$input) = @_[HEAP,ARG0]; pass('Got something back from the server'); ok( $input eq 'Hello, is it me you are looking for?', $input ); $heap->{testc}->terminate(); return; } sub testc_disconnected { my ($heap,$state) = @_[HEAP,STATE]; pass($state); delete $heap->{wheels}; delete $heap->{listener}; $heap->{testc}->shutdown(); return; } 09_server_info.t100644001751000144 630412073554720 17640 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12/tuse strict; use Socket; use Test::More tests => 22; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use_ok('Test::POE::Client::TCP'); my @data = ( 'This is a test', 'This is another test', 'This is the last test', ); POE::Session->create( package_states => [ 'main' => [qw( _start _accept _failed _sock_in _sock_err testc_registered testc_connected testc_disconnected testc_input testc_flushed )], ], heap => { data => \@data, }, ); $poe_kernel->run(); exit 0; sub _start { my ($kernel,$heap) = @_[KERNEL,HEAP]; $heap->{listener} = POE::Wheel::SocketFactory->new( BindAddress => '127.0.0.1', SuccessEvent => '_accept', FailureEvent => '_failed', 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 ); $heap->{count} = @{ $heap->{data} }; $heap->{testc} = Test::POE::Client::TCP->spawn(); isa_ok( $heap->{testc}, 'Test::POE::Client::TCP' ); return; } sub _accept { my ($kernel,$heap,$socket) = @_[KERNEL,HEAP,ARG0]; my $wheel = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{wheels}->{ $wheel->ID } = $wheel; return; } sub _failed { my ($kernel,$heap,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,HEAP,ARG0..ARG3]; die "Wheel $wheel_id generated $operation error $errnum: $errstr\n"; return; } sub _sock_in { my ($heap,$input,$wheel_id) = @_[HEAP,ARG0,ARG1]; pass('Got input from client'); $heap->{wheels}->{ $wheel_id }->put( $input ) if $heap->{wheels}->{ $wheel_id }; return; } sub _sock_err { my ($heap,$wheel_id) = @_[HEAP,ARG3]; pass('Client disconnected'); delete $heap->{wheels}->{ $wheel_id }; return; } sub testc_registered { my ($kernel,$sender,$object) = @_[KERNEL,SENDER,ARG0]; pass($_[STATE]); isa_ok( $object, 'Test::POE::Client::TCP' ); my $port = ( sockaddr_in( $_[HEAP]->{listener}->getsockname() ) )[0]; diag("Connecting to port: $port\n"); $kernel->post( $sender, 'connect', { address => '127.0.0.1', port => $port } ); return; } sub testc_connected { my ($kernel,$heap,$sender) = @_[KERNEL,HEAP,SENDER]; pass($_[STATE]); my @orig = @_[ARG0..$#_]; my @test = $heap->{testc}->server_info(); my $test = $heap->{testc}->server_info(); ok( $test[$_] eq $orig[$_], "Server Info: " . $orig[$_] ) for 0 .. 3; my @test2 = map { $test->{$_} } qw(peeraddr peerport sockaddr sockport); ok( $test2[$_] eq $orig[$_], "Server Info: " . $orig[$_] ) for 0 .. 3; $kernel->post( $sender, 'send_to_server', [ @{ $_[HEAP]->{data} } ] ); return; } sub testc_flushed { pass($_[STATE]); return; } sub testc_input { my ($heap,$input) = @_[HEAP,ARG0]; pass('Got something back from the server'); $heap->{count}--; $heap->{testc}->terminate() if $heap->{count} <= 0; return; } sub testc_disconnected { my ($heap,$state) = @_[HEAP,STATE]; pass($state); delete $heap->{wheels}; delete $heap->{listener}; $heap->{testc}->shutdown(); return; } examples000755001751000144 012073554720 16032 5ustar00chrisusers000000000000Test-POE-Client-TCP-1.12synopsis.pl100644001751000144 554712073554720 20431 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12/examplesuse strict; use Socket; use Test::More tests => 15; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use Test::POE::Client::TCP; my @data = ( 'This is a test', 'This is another test', 'This is the last test', ); POE::Session->create( package_states => [ 'main' => [qw( _start _accept _failed _sock_in _sock_err testc_registered testc_connected testc_disconnected testc_input testc_flushed )], ], heap => { data => \@data, }, ); $poe_kernel->run(); exit 0; sub _start { my ($kernel,$heap) = @_[KERNEL,HEAP]; $heap->{listener} = POE::Wheel::SocketFactory->new( BindAddress => '127.0.0.1', SuccessEvent => '_accept', FailureEvent => '_failed', 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 ); $heap->{testc} = Test::POE::Client::TCP->spawn(); return; } sub _accept { my ($kernel,$heap,$socket) = @_[KERNEL,HEAP,ARG0]; my $wheel = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{wheels}->{ $wheel->ID } = $wheel; return; } sub _failed { my ($kernel,$heap,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,HEAP,ARG0..ARG3]; die "Wheel $wheel_id generated $operation error $errnum: $errstr\n"; return; } sub _sock_in { my ($heap,$input,$wheel_id) = @_[HEAP,ARG0,ARG1]; pass('Got input from client'); $heap->{wheels}->{ $wheel_id }->put( $input ) if $heap->{wheels}->{ $wheel_id }; return; } sub _sock_err { my ($heap,$wheel_id) = @_[HEAP,ARG3]; pass('Client disconnected'); delete $heap->{wheels}->{ $wheel_id }; return; } sub testc_registered { my ($kernel,$sender,$object) = @_[KERNEL,SENDER,ARG0]; pass($_[STATE]); my $port = ( sockaddr_in( $_[HEAP]->{listener}->getsockname() ) )[0]; $kernel->post( $sender, 'connect', { address => '127.0.0.1', port => $port } ); return; } sub testc_connected { my ($kernel,$heap,$sender) = @_[KERNEL,HEAP,SENDER]; pass($_[STATE]); $kernel->post( $sender, 'send_to_server', $heap->{data}->[0] ); return; } sub testc_flushed { pass($_[STATE]); return; } sub testc_input { my ($heap,$input) = @_[HEAP,ARG0]; pass('Got something back from the server'); my $data = shift @{ $heap->{data} }; ok( $input eq $data, "Data matched: '$input'" ); unless ( scalar @{ $heap->{data} } ) { $heap->{testc}->terminate(); return; } $poe_kernel->post( $_[SENDER], 'send_to_server', $heap->{data}->[0] ); return; } sub testc_disconnected { my ($heap,$state) = @_[HEAP,STATE]; pass($state); delete $heap->{wheels}; delete $heap->{listener}; $heap->{testc}->shutdown(); return; } release-pod-syntax.t100644001751000144 45012073554720 20507 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12/t#!perl BEGIN { unless ($ENV{RELEASE_TESTING}) { require Test::More; Test::More::plan(skip_all => 'these tests are for release candidate testing'); } } use Test::More; eval "use Test::Pod 1.41"; plan skip_all => "Test::Pod 1.41 required for testing POD" if $@; all_pod_files_ok(); release-pod-coverage.t100644001751000144 76512073554720 20765 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12/t#!perl BEGIN { unless ($ENV{RELEASE_TESTING}) { require Test::More; Test::More::plan(skip_all => 'these tests are for release candidate testing'); } } use Test::More; eval "use Test::Pod::Coverage 1.08"; plan skip_all => "Test::Pod::Coverage 1.08 required for testing POD coverage" if $@; eval "use Pod::Coverage::TrustPod"; plan skip_all => "Pod::Coverage::TrustPod required for testing POD coverage" if $@; all_pod_coverage_ok({ coverage_class => 'Pod::Coverage::TrustPod' }); Client000755001751000144 012073554720 17542 5ustar00chrisusers000000000000Test-POE-Client-TCP-1.12/lib/Test/POETCP.pm100644001751000144 5404112073554720 20712 0ustar00chrisusers000000000000Test-POE-Client-TCP-1.12/lib/Test/POE/Clientpackage Test::POE::Client::TCP; { $Test::POE::Client::TCP::VERSION = '1.12'; } #ABSTRACT: A POE Component providing TCP client services for test cases use strict; use warnings; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use POSIX qw[ETIMEDOUT]; use Socket; use Carp qw(carp croak); sub spawn { my $package = shift; my %opts = @_; $opts{lc $_} = delete $opts{$_} for keys %opts; my $options = delete $opts{options}; my $autoconnect = delete $opts{autoconnect}; if ( $autoconnect and !( $opts{address} and $opts{port} ) ) { carp "You must provide both 'address' and 'port' parameters when specifying 'autoconnect'\n"; return; } delete $opts{timeout} unless $opts{timeout} and $opts{timeout} =~ m!^\d+$!; my $self = bless \%opts, $package; $self->{_prefix} = delete $self->{prefix}; $self->{_prefix} = 'testc_' 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_server => '_send_to_server', disconnect => '_disconnect', terminate => '_terminate', connect => '_connect', _timeout => '_socket_fail', }, $self => [ qw(_start register unregister _socket_up _socket_fail _conn_input _conn_error _conn_flushed _send_to_server __send_event _disconnect) ], ], heap => $self, ( ref($options) eq 'HASH' ? ( options => $options ) : () ), args => [ $autoconnect ], )->ID(); return $self; } sub session_id { shift->{session_id}; } sub shutdown { $poe_kernel->call( shift->{session_id}, 'shutdown' ); } sub server_info { my $self = shift; return unless $self->{_server_info}; my @vals = @{ $self->{_server_info} }; return @vals if wantarray; return { map { $_ => shift @vals } qw(peeraddr peerport sockaddr sockport) }; } sub connect { $poe_kernel->call( shift->{session_id}, 'connect' ); } sub wheel { my $self = shift; return unless $self->{socket}; return $self->{socket}; } sub alias { shift->{alias}; } sub _start { my ($kernel,$self,$sender,$autoconnect) = @_[KERNEL,OBJECT,SENDER,ARG0]; $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->yield( 'connect' ) if $autoconnect and $self->{address} and $self->{port}; return; } sub _connect { my ($kernel,$self) = @_[KERNEL,OBJECT]; my $args; if ( ref( $_[ARG0] ) eq 'HASH' ) { $args = { %{ $_[ARG0] } }; } else { $args = { @_[ARG0..$#_] }; } $args->{lc $_} = delete $args->{$_} for keys %{ $args }; unless ( $self->{address} and $self->{port} ) { unless ( $args->{address} and $args->{port} ) { carp "You must provide both 'address' and 'port' parameters\n"; return; } $self->{address} = $args->{address}; $self->{port} = $args->{port}; } $self->{localaddr} = $args->{localaddr} if $args->{localaddr}; $self->{localport} = $args->{localaddr} if $args->{localport}; if ( $self->{socket} ) { carp "Already connected. Disconnect and call 'connect' again\n"; return; } if ( $self->{factory} ) { carp "Connection already in progress\n"; return; } $self->{factory} = POE::Wheel::SocketFactory->new( RemoteAddress => $self->{address}, RemotePort => $self->{port}, ( defined $self->{address} ? ( BindAddress => $self->{localaddr} ) : () ), ( defined $self->{port} ? ( BindPort => $self->{localport} ) : () ), SuccessEvent => '_socket_up', FailureEvent => '_socket_fail', SocketDomain => AF_INET, # Sets the socket() domain SocketType => SOCK_STREAM, # Sets the socket() type SocketProtocol => 'tcp', # Sets the socket() protocol Reuse => 'yes', # Lets the port be reused ); $kernel->delay( '_timeout', $self->{timeout}, 'connect', ETIMEDOUT, POSIX::strerror( ETIMEDOUT ) ) if $self->{timeout}; return; } sub _socket_up { my ($kernel,$self,$socket,$peeraddr,$peerport) = @_[KERNEL,OBJECT,ARG0..ARG2]; my $sockaddr = inet_ntoa( ( unpack_sockaddr_in ( CORE::getsockname $socket ) )[1] ); my $sockport = ( unpack_sockaddr_in ( CORE::getsockname $socket ) )[0]; $peeraddr = inet_ntoa( $peeraddr ); $kernel->delay( '_timeout' ); delete $self->{factory}; $self->{socket} = POE::Wheel::ReadWrite->new( Handle => $socket, _get_filters( $self->{filter}, $self->{inputfilter}, $self->{outputfilter} ), InputEvent => '_conn_input', ErrorEvent => '_conn_error', FlushedEvent => '_conn_flushed', ); $self->{_server_info} = [ $peeraddr, $peerport, $sockaddr, $sockport ]; $self->_send_event( $self->{_prefix} . 'connected', $peeraddr, $peerport, $sockaddr, $sockport ); return; } 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 _socket_fail { my ($kernel,$self,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,OBJECT,ARG0..ARG3]; carp "Wheel $wheel_id generated $operation error $errnum: $errstr\n" if $self->{debug}; $kernel->delay( '_timeout' ); delete $self->{factory}; $self->_send_event( $self->{_prefix} . 'socket_failed', $operation, $errnum, $errstr ); return; } sub disconnect { my $self = shift; $poe_kernel->call( $self->{session_id}, 'disconnect', @_ ); } sub _disconnect { my ($kernel,$self) = @_[KERNEL,OBJECT]; return unless $self->{socket}; $self->{_quit} = 1; return 1; } sub terminate { my $self = shift; $poe_kernel->call( $self->{session_id}, 'terminate', @_ ); } sub _terminate { my ($kernel,$self) = @_[KERNEL,OBJECT]; return unless $self->{socket}; if ( $^O =~ /(cygwin|MSWin)/ ) { $self->{socket}->shutdown_input(); $self->{socket}->shutdown_output(); } delete $self->{socket}; delete $self->{_server_info}; $self->_send_event( $self->{_prefix} . 'disconnected' ); return 1; } sub _conn_input { my ($kernel,$self,$input,$id) = @_[KERNEL,OBJECT,ARG0,ARG1]; $self->_send_event( $self->{_prefix} . 'input', $input ); return; } sub _conn_error { my ($self,$errstr,$id) = @_[OBJECT,ARG2,ARG3]; return unless $self->{socket}; delete $self->{socket}; delete $self->{_server_info}; $self->_send_event( $self->{_prefix} . 'disconnected' ); return; } sub _conn_flushed { my ($self,$id) = @_[OBJECT,ARG0]; return unless $self->{socket}; if ( $self->{BUFFER} ) { my $item = shift @{ $self->{BUFFER} }; unless ( $item ) { delete $self->{BUFFER}; $self->_send_event( $self->{_prefix} . 'flushed' ); return; } $self->{socket}->put($item); return; } unless ( $self->{_quit} ) { $self->_send_event( $self->{_prefix} . 'flushed' ); return; } delete $self->{socket}; delete $self->{_server_info}; $self->_send_event( $self->{_prefix} . 'disconnected' ); return; } sub _shutdown { my ($kernel,$self) = @_[KERNEL,OBJECT]; delete $self->{factory}; delete $self->{socket}; delete $self->{_server_info}; $kernel->alarm_remove_all(); $kernel->alias_remove( $_ ) for $kernel->alias_list(); $kernel->refcount_decrement( $self->{session_id} => __PACKAGE__ ) unless $self->{alias}; $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; # $poe_kernel->post( $self->{session_id}, '__send_event', @_ ); #} 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_server { my $self = shift; $poe_kernel->call( $self->{session_id}, '_send_to_server', @_ ); } sub _send_to_server { my ($kernel,$self,$output) = @_[KERNEL,OBJECT,ARG0]; return unless $self->{socket}; return unless $output; if ( ref $output eq 'ARRAY' ) { my $first = shift @{ $output }; $self->{BUFFER} = $output if scalar @{ $output }; $self->{socket}->put($first) if defined $first; return 1; } $self->{socket}->put($output); return 1; } q{Putting the test into POE}; __END__ =pod =head1 NAME Test::POE::Client::TCP - A POE Component providing TCP client services for test cases =head1 VERSION version 1.12 =head1 SYNOPSIS use strict; use Socket; use Test::More tests => 15; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use Test::POE::Client::TCP; my @data = ( 'This is a test', 'This is another test', 'This is the last test', ); POE::Session->create( package_states => [ 'main' => [qw( _start _accept _failed _sock_in _sock_err testc_registered testc_connected testc_disconnected testc_input testc_flushed )], ], heap => { data => \@data, }, ); $poe_kernel->run(); exit 0; sub _start { my ($kernel,$heap) = @_[KERNEL,HEAP]; $heap->{listener} = POE::Wheel::SocketFactory->new( BindAddress => '127.0.0.1', SuccessEvent => '_accept', FailureEvent => '_failed', 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 ); $heap->{testc} = Test::POE::Client::TCP->spawn(); return; } sub _accept { my ($kernel,$heap,$socket) = @_[KERNEL,HEAP,ARG0]; my $wheel = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{wheels}->{ $wheel->ID } = $wheel; return; } sub _failed { my ($kernel,$heap,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,HEAP,ARG0..ARG3]; die "Wheel $wheel_id generated $operation error $errnum: $errstr\n"; return; } sub _sock_in { my ($heap,$input,$wheel_id) = @_[HEAP,ARG0,ARG1]; pass('Got input from client'); $heap->{wheels}->{ $wheel_id }->put( $input ) if $heap->{wheels}->{ $wheel_id }; return; } sub _sock_err { my ($heap,$wheel_id) = @_[HEAP,ARG3]; pass('Client disconnected'); delete $heap->{wheels}->{ $wheel_id }; return; } sub testc_registered { my ($kernel,$sender,$object) = @_[KERNEL,SENDER,ARG0]; pass($_[STATE]); my $port = ( sockaddr_in( $_[HEAP]->{listener}->getsockname() ) )[0]; $kernel->post( $sender, 'connect', { address => '127.0.0.1', port => $port } ); return; } sub testc_connected { my ($kernel,$heap,$sender) = @_[KERNEL,HEAP,SENDER]; pass($_[STATE]); $kernel->post( $sender, 'send_to_server', $heap->{data}->[0] ); return; } sub testc_flushed { pass($_[STATE]); return; } sub testc_input { my ($heap,$input) = @_[HEAP,ARG0]; pass('Got something back from the server'); my $data = shift @{ $heap->{data} }; ok( $input eq $data, "Data matched: '$input'" ); unless ( scalar @{ $heap->{data} } ) { $heap->{testc}->terminate(); return; } $poe_kernel->post( $_[SENDER], 'send_to_server', $heap->{data}->[0] ); return; } sub testc_disconnected { my ($heap,$state) = @_[HEAP,STATE]; pass($state); delete $heap->{wheels}; delete $heap->{listener}; $heap->{testc}->shutdown(); return; } =head1 DESCRIPTION Test::POE::Client::TCP is a L component that provides a TCP client 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 connections made, disconnects, flushed output and input from the specified server. =head1 CONSTRUCTOR =over =item C Takes a number of optional arguments: 'alias', set an alias on the component; 'address', the remote address to connect to; 'port', the remote port to connect to; '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; 'localaddr', specify that connections be made from a particular local address; 'localport', specify that connections be made from a particular port; 'autoconnect', set to a true value to make the poco connect immediately; 'prefix', specify an event prefix other than the default of 'testc'; 'timeout', specify number of seconds to wait for socket timeouts; 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. C
and C are optional within C, but if they aren't specified they must be provided to subsequent Cs. If C is specified, C
and C must also be defined. =back =head1 METHODS =over =item C Initiates a connection to the given server. Takes a number of parameters: 'address', the remote address to connect to; 'port', the remote port to connect to; 'localaddr', specify that connections be made from a particular local address, optional; 'localport', specify that connections be made from a particular port, optional; C
and C are optional if they have been already specified during C. =item C Returns the POE::Session ID of the component. =item C Terminates the component. It will terminate any pending connects or connections. =item C Retrieves socket information about the current connection. In a list context it returns a list consisting of, in order, the server address, the server TCP port, our address and our TCP port. In a scalar context it returns a HASHREF with the following keys: 'peeraddr', the server address; 'peerport', the server TCP port; 'sockaddr', our address; 'sockport', our TCP port; =item C Send some output to the connected server. The first parameter is a string of text to send. This 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 Places the server connection into pending disconnect state. Set this, then send an applicable message to the server using C and the server connection will be terminated. =item C Immediately disconnects a server conenction. =item C Returns the underlying L object if we are currently connected to a server, C otherwise. You can use this method to call methods on the wheel object to switch filters, etc. Exercise caution. =item C Returns the currently configured alias. =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 'testc_' prefix. Registering for 'all' will cause it to send all TESTC-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 poco to stop sending them to you. (If you haven't, it just ignores you. No big deal). =item C Initiates a connection to the given server. Takes a number of parameters: 'address', the remote address to connect to; 'port', the remote port to connect to; 'localaddr', specify that connections be made from a particular local address, optional; 'localport', specify that connections be made from a particular port, optional; C
and C are optional if they have been already specified during C. =item C Terminates the component. It will terminate any pending connects or connections. =item C Send some output to the connected server. The first parameter is a string of text to send. This 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 Places the server connection into pending disconnect state. Set this, then send an applicable message to the server using C and the server connection will be terminated. =item C Immediately disconnects a server conenction. =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::Client::TCP object. =item C Generated if the component cannot make a socket connection. ARG0 contains the name of the operation that failed. ARG1 and ARG2 hold numeric and string values for $!, respectively. =item C Generated whenever a connection is established. ARG0 is the server's IP address, ARG1 is the server's TCP port. ARG3 is our IP address and ARG4 is our socket port. =item C Generated whenever we disconnect from the server. =item C Generated whenever the server sends us some traffic. ARG0 is the data sent ( tokenised by whatever POE::Filter you specified ). =item C Generated whenever anything we send to the server is actually flushed down the 'line'. =back =head1 KUDOS Contains 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) 2013 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