6.9/10 (33 أصوات )




  • مرات التنزيل: 180
  • متطلبات التشغيل:
  • الحجم:
  • الترخيص:
  • الاصدار : 1.16
  • اضيف في: 0000-00-00 00:00:00
  • اخر تحديث: 29/05/2012
  • الموقع علي الانترنت:






Description




The Test::POE::Server::TCP module is a POE component that provides a TCP server framework for inclusion in client component test cases, instead of having to roll your own.Once registered with the component, a session will receive events related to client connects, disconnects, input and flushed output. Each of these events will refer to a unique client ID which may be used in communication with the component when sending data to the client or disconnecting a client connection.SYNOPSISA very simple echo server with logging of requests by each client: use strict; use POE; use Test::POE::Server::TCP; POE::Session->create( package_states => [ 'main' => [qw( _start testd_connected testd_disconnected testd_client_input )], ], );  $poe_kernel->run(); exit 0;  sub _start { # Spawn the Test::POE::Server::TCP server. $_[HEAP]->{testd} = Test::POE::Server::TCP->spawn( address => '127.0.0.1', port => 0, ); return; }  sub testd_connected { my ($heap,$id) = @_[HEAP,ARG0]; # A client connected the unique ID is in ARG0 # Create a blank arrayref for this client on *our* heap $heap->{clients}->{ $id } = [ ]; return; }  sub testd_client_input { my ($kernel,$heap,$sender,$id,$input) = @_[KERNEL,HEAP,SENDER,ARG0,ARG1]; # The client sent us a line of input # lets store it push @{ $heap->{clients}->{ $id }, $input; # Okay, we are an echo server so lets send it back to the client # We know the SENDER so can always obtain the server object. my $testd = $sender->get_heap(); $testd->send_to_client( $id, $input ); # Or even # $sender->get_heap()->send_to_client( $id, $input ); # Alternatively we could just post back to the SENDER # $kernel->post( $sender, 'send_to_client', $id, $input ); return; } sub testd_disconnected { my ($heap,$id) = @_[HEAP,ARG0]; # Client disconnected for whatever reason # We need to free up our storage delete $heap->{clients}->{ $id }; return; }Using the module in a testcase: use strict; use Test::More; use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line); use Test::POE::Server::TCP;  plan tests => 5;  my @data = ( 'This is a test', 'This is another test', 'This is the last test', );  POE::Session->create( package_states => [ 'main' => [qw( _start _sock_up _sock_fail _sock_in _sock_err testd_connected testd_disconnected testd_client_input )], ], heap => { data => \@data, }, );  $poe_kernel->run(); exit 0;  sub _start { $_[HEAP]->{testd} = Test::POE::Server::TCP->spawn( address => '127.0.0.1', port => 0, ); return; }  sub testd_registered { my ($heap,$object) = @_[HEAP,ARG0]; $heap->{port} = $object->port(); $heap->{factory} = POE::Wheel::SocketFactory->new( RemoteAddress => '127.0.0.1', RemotePort => $heap->{port}, SuccessEvent => '_sock_up', FailureEvent => '_sock_fail', ); return; }  sub _sock_up { my ($heap,$socket) = @_[HEAP,ARG0]; delete $heap->{factory}; $heap->{socket} = POE::Wheel::ReadWrite->new( Handle => $socket, InputEvent => '_sock_in', ErrorEvent => '_sock_err', ); $heap->{socket}->put( $heap->{data}->[0] ); return; }  sub _sock_fail { my $heap = $_[HEAP]; delete $heap->{factory}; $heap->{testd}->shutdown(); return; }  sub _sock_in { my ($heap,$input) = @_[HEAP,ARG0]; my $data = shift @{ $heap->{data} }; ok( $input eq $data, 'Data matched' ); unless ( scalar @{ $heap->{data} } ) { delete $heap->{socket}; return; } $heap->{socket}->put( $heap->{data}->[0] ); return; }  sub _sock_err { delete $_[HEAP]->{socket}; return; }  sub testd_connected { my ($heap,$state,$id) = @_[HEAP,STATE,ARG0]; pass($state); return; }  sub testd_disconnected { pass($_[STATE]); $poe_kernel->post( $_[SENDER], 'shutdown' ); return; }  sub testd_client_input { my ($sender,$id,$input) = @_[SENDER,ARG0,ARG1]; my $testd = $_[SENDER]->get_heap(); $testd->send_to_client( $id, $input ); return; }Product's homepage Requirements:

· Perl









التعليقات علي Test::POE::Server::TCP 1.16
اضافة تعليق

تعليقات الفيسبوك

تعليقات الموقع