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 \s-1POE\s0 component that provides a \s-1TCP\s0 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

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`filter\*(C', \*(C`inputfilter\*(C' and \*(C`outputfilter\*(C' are the same as for POE::Component::Server::TCP in that one may provide either a \*(C`SCALAR\*(C', \*(C`ARRAYREF\*(C' or an \*(C`OBJECT\*(C'. If the component is \*(C`spawn\*(C'ed within another session it will automatically \*(C`register\*(C' the parent session to receive \*(C`all\*(C' events. \*(C`address\*(C' and \*(C`port\*(C' are optional within \*(C`spawn\*(C', but if they aren't specified they must be provided to subsequent \*(C`connect\*(C's. If \*(C`autoconnect\*(C' is specified, \*(C`address\*(C' and \*(C`port\*(C' must also be defined.

METHODS

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`address\*(C' and \*(C`port\*(C' are optional if they have been already specified during \*(C`spawn\*(C'. Returns the POE::Session \s-1ID\s0 of the component. Terminates the component. It will terminate any pending connects or connections. Retrieves socket information about the current connection. In a list context it returns a list consisting of, in order, the server address, the server \s-1TCP\s0 port, our address and our \s-1TCP\s0 port. In a scalar context it returns a \s-1HASHREF\s0 with the following keys: 'peeraddr', the server address; 'peerport', the server TCP port; 'sockaddr', our address; 'sockport', our TCP port; 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. Places the server connection into pending disconnect state. Set this, then send an applicable message to the server using \*(C`send_to_server()\*(C' and the server connection will be terminated. Immediately disconnects a server conenction. Returns the underlying POE::Wheel::ReadWrite object if we are currently connected to a server, \*(C`undef\*(C' otherwise. You can use this method to call methods on the wheel object to switch filters, etc. Exercise caution. Returns the currently configured alias.

INPUT EVENTS

These are events that the component will accept: 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. 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). 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`address\*(C' and \*(C`port\*(C' are optional if they have been already specified during \*(C`spawn\*(C'. Terminates the component. It will terminate any pending connects or connections. 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. Places the server connection into pending disconnect state. Set this, then send an applicable message to the server using \*(C`send_to_server()\*(C' and the server connection will be terminated. Immediately disconnects a server conenction.

OUTPUT EVENTS

The component sends the following events to registered sessions. If you have changed the \*(C`prefix\*(C' option in \*(C`spawn\*(C' then substitute \*(C`testc\*(C' with the event prefix that you specified. This event is sent to a registering session. \s-1ARG0\s0 is the Test::POE::Client::TCP object. Generated if the component cannot make a socket connection. \s-1ARG0\s0 contains the name of the operation that failed. \s-1ARG1\s0 and \s-1ARG2\s0 hold numeric and string values for $!, respectively. Generated whenever a connection is established. \s-1ARG0\s0 is the server's \s-1IP\s0 address, \s-1ARG1\s0 is the server's \s-1TCP\s0 port. \s-1ARG3\s0 is our \s-1IP\s0 address and \s-1ARG4\s0 is our socket port. Generated whenever we disconnect from the server. Generated whenever the server sends us some traffic. \s-1ARG0\s0 is the data sent ( tokenised by whatever POE::Filter you specified ). 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.

RELATED TO Test::POE::Client::TCP…

\s-1POE\s0

POE::Component::Server::TCP

AUTHOR

Chris Williams <[email protected]>

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.