| 1 | #!/usr/bin/perl -w | ||
| 2 | # vim: ts=2 sw=2 expandtab | ||
| 3 | |||
| 4 | # Rocco Caputo noticed this bug in POE::Loop::Glib v0.037. | ||
| 5 | # LotR wanted this converted into a test in PTL so we can | ||
| 6 | # verify the bug is gone and help other loop authors :) | ||
| 7 | |||
| 8 | # TODO do we need a way to timeout the test? | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | |||
| 12 | BEGIN { | ||
| 13 | unless (-f "run_network_tests") { | ||
| 14 | print "1..0 # Skip Network access (and permission) required to run this test\n"; | ||
| 15 | CORE::exit(); | ||
| 16 | } | ||
| 17 | } | ||
| 18 | |||
| 19 | sub POE::Kernel::ASSERT_DEFAULT () { 1 } | ||
| 20 | #sub POE::Kernel::TRACE_FILES () { 1 } | ||
| 21 | |||
| 22 | BEGIN { | ||
| 23 | eval "sub POE::Kernel::TRACE_DEFAULT () { 1 }" if ( | ||
| 24 | exists $INC{'Devel/Cover.pm'} | ||
| 25 | ); | ||
| 26 | } | ||
| 27 | |||
| 28 | use Test::More; | ||
| 29 | plan tests => 8; | ||
| 30 | |||
| 31 | use POE qw( Component::Server::TCP Component::Client::TCP ); | ||
| 32 | use Socket qw( sockaddr_in ); | ||
| 33 | |||
| 34 | my $num_clients = 5; | ||
| 35 | |||
| 36 | # testing variables | ||
| 37 | my $num_server_connects = 0; | ||
| 38 | my $num_server_disconnects = 0; | ||
| 39 | my $num_server_inputs = 0; | ||
| 40 | my $num_server_flushes = 0; | ||
| 41 | my $acceptor_port; | ||
| 42 | |||
| 43 | my $num_client_connects = 0; | ||
| 44 | my $num_client_disconnects = 0; | ||
| 45 | my $num_client_inputs = 0; | ||
| 46 | my $num_client_flushes = 0; | ||
| 47 | |||
| 48 | # Spawn the TCP server. | ||
| 49 | POE::Component::Server::TCP->new( | ||
| 50 | Alias => 'server', | ||
| 51 | Address => 'localhost', | ||
| 52 | Port => 0, | ||
| 53 | Started => sub { | ||
| 54 | $acceptor_port = ( | ||
| 55 | sockaddr_in($_[HEAP]->{listener}->getsockname()) | ||
| 56 | )[0]; | ||
| 57 | }, | ||
| 58 | |||
| 59 | ClientConnected => sub { $num_server_connects++ }, | ||
| 60 | |||
| 61 | ClientInput => sub { | ||
| 62 | $num_server_inputs++; | ||
| 63 | |||
| 64 | $_[HEAP]->{client}->put( 'from server' ); | ||
| 65 | $_[KERNEL]->yield( 'shutdown' ); | ||
| 66 | }, | ||
| 67 | |||
| 68 | ClientFlushed => sub { $num_server_flushes++ }, | ||
| 69 | |||
| 70 | ClientDisconnected => sub { | ||
| 71 | $num_server_disconnects++; | ||
| 72 | |||
| 73 | # end the test after N clients is done | ||
| 74 | if ( $num_server_disconnects >= $num_clients ) { | ||
| 75 | $_[KERNEL]->call( 'server', 'shutdown' ); | ||
| 76 | } | ||
| 77 | }, | ||
| 78 | ); | ||
| 79 | |||
| 80 | # spawn the client | ||
| 81 | for ( 1 .. $num_clients ) { | ||
| 82 | POE::Component::Client::TCP->new( | ||
| 83 | RemoteAddress => 'localhost', | ||
| 84 | RemotePort => $acceptor_port, | ||
| 85 | ConnectTimeout => 2, | ||
| 86 | |||
| 87 | Connected => sub { | ||
| 88 | $num_client_connects++; | ||
| 89 | |||
| 90 | $_[HEAP]->{server}->put( 'from client' ); | ||
| 91 | }, | ||
| 92 | Disconnected => sub { $num_client_disconnects++ }, | ||
| 93 | |||
| 94 | ServerInput => sub { | ||
| 95 | $num_client_inputs++; | ||
| 96 | |||
| 97 | $_[KERNEL]->delay( 'shutdown' => 1 ); | ||
| 98 | }, | ||
| 99 | ServerError => sub {}, | ||
| 100 | ServerFlushed => sub { $num_client_flushes++ }, | ||
| 101 | ); | ||
| 102 | } | ||
| 103 | |||
| 104 | $poe_kernel->run(); | ||
| 105 | |||
| 106 | # Okay, make sure we processed N connections | ||
| 107 | is( $num_server_connects, $num_clients, "Server got $num_clients client connections" ); | ||
| 108 | is( $num_server_disconnects, $num_clients, "Server got $num_clients client disconnections" ); | ||
| 109 | is( $num_server_inputs, $num_clients, "Client sent input $num_clients times" ); | ||
| 110 | is( $num_server_flushes, $num_clients, "Server flushed $num_clients lines of data" ); | ||
| 111 | |||
| 112 | is( $num_client_connects, $num_clients, "Client connected $num_clients times" ); | ||
| 113 | is( $num_client_disconnects, $num_clients, "Client disconnected $num_clients times" ); | ||
| 114 | is( $num_client_inputs, $num_clients, "Server sent input $num_clients times" ); | ||
| 115 | is( $num_client_flushes, $num_clients, "Client flushed $num_clients lines of data" ); | ||
| 116 | |||
| 117 | 1; | ||
| 118 |