| 1 | #!/usr/bin/perl -w | ||
| 2 | # vim: ts=2 sw=2 expandtab | ||
| 3 | |||
| 4 | # Exercise Server::TCP and later, when it's available, Client::TCP. | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use lib qw(./mylib ../mylib); | ||
| 8 | |||
| 9 | BEGIN { | ||
| 10 | unless (-f "run_network_tests") { | ||
| 11 | print "1..0 # Skip Network access (and permission) required to run this test\n"; | ||
| 12 | CORE::exit(); | ||
| 13 | } | ||
| 14 | } | ||
| 15 | |||
| 16 | use Test::More tests => 34; | ||
| 17 | |||
| 18 | sub POE::Kernel::ASSERT_DEFAULT () { 1 } | ||
| 19 | |||
| 20 | BEGIN { | ||
| 21 | package | ||
| 22 | POE::Kernel; | ||
| 23 | use constant TRACE_DEFAULT => exists($INC{'Devel/Cover.pm'}); | ||
| 24 | } | ||
| 25 | |||
| 26 | use POE qw( Component::Server::TCP Wheel::ReadWrite Component::Client::TCP ); | ||
| 27 | |||
| 28 | my ($acceptor_port, $callback_port); | ||
| 29 | |||
| 30 | # Create a server. This one uses Acceptor to create a session of the | ||
| 31 | # program's devising. | ||
| 32 | |||
| 33 | POE::Component::Server::TCP->new( | ||
| 34 | Port => 0, | ||
| 35 | Address => '127.0.0.1', | ||
| 36 | Alias => 'acceptor_server', | ||
| 37 | Started => sub { | ||
| 38 | use Socket qw(sockaddr_in); | ||
| 39 | $acceptor_port = ( | ||
| 40 | sockaddr_in($_[HEAP]->{listener}->getsockname()) | ||
| 41 | )[0]; | ||
| 42 | }, | ||
| 43 | Acceptor => sub { | ||
| 44 | my ($socket, $peer_addr, $peer_port) = @_[ARG0..ARG2]; | ||
| 45 | POE::Session->create( | ||
| 46 | inline_states => { | ||
| 47 | _start => sub { | ||
| 48 | my $heap = $_[HEAP]; | ||
| 49 | $heap->{wheel} = POE::Wheel::ReadWrite->new( | ||
| 50 | Handle => $socket, | ||
| 51 | InputEvent => 'got_input', | ||
| 52 | ErrorEvent => 'got_error', | ||
| 53 | FlushedEvent => 'got_flush', | ||
| 54 | ); | ||
| 55 | pass("acceptor server got client connection"); | ||
| 56 | }, | ||
| 57 | _stop => sub { | ||
| 58 | pass("acceptor server stopped the client session"); | ||
| 59 | }, | ||
| 60 | got_input => sub { | ||
| 61 | my ($heap, $input) = @_[HEAP, ARG0]; | ||
| 62 | pass("acceptor server received input"); | ||
| 63 | $heap->{wheel}->put("echo: $input"); | ||
| 64 | $heap->{shutdown} = 1 if $input eq "quit"; | ||
| 65 | }, | ||
| 66 | got_error => sub { | ||
| 67 | my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0..ARG2]; | ||
| 68 | print "acceptor server got $operation error $errnum: $errstr\n"; | ||
| 69 | }, | ||
| 70 | got_flush => sub { | ||
| 71 | my $heap = $_[HEAP]; | ||
| 72 | pass("acceptor server flushed output"); | ||
| 73 | delete $heap->{wheel} if $heap->{shutdown}; | ||
| 74 | }, | ||
| 75 | }, | ||
| 76 | ); | ||
| 77 | }, | ||
| 78 | ); | ||
| 79 | |||
| 80 | # Create a server. This one uses ClientXyz to process clients instead | ||
| 81 | # of a user-defined session. | ||
| 82 | |||
| 83 | POE::Component::Server::TCP->new( | ||
| 84 | Port => 0, | ||
| 85 | Address => '127.0.0.1', | ||
| 86 | Alias => 'input_server', | ||
| 87 | ClientFilter => [ "POE::Filter::Line", Literal => "\n" ], | ||
| 88 | Started => sub { | ||
| 89 | use Socket qw(sockaddr_in); | ||
| 90 | $callback_port = ( | ||
| 91 | sockaddr_in($_[HEAP]->{listener}->getsockname()) | ||
| 92 | )[0]; | ||
| 93 | }, | ||
| 94 | ClientInput => sub { | ||
| 95 | my ($heap, $input) = @_[HEAP, ARG0]; | ||
| 96 | pass("callback server got input"); | ||
| 97 | $heap->{client}->put("echo: $input"); | ||
| 98 | $heap->{shutdown} = 1 if $input eq "quit"; | ||
| 99 | }, | ||
| 100 | ClientError => sub { | ||
| 101 | my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0..ARG2]; | ||
| 102 | print "callback server got $operation error $errnum: $errstr\n"; | ||
| 103 | }, | ||
| 104 | ClientFlushed => sub { | ||
| 105 | pass("callback server flushed output"); | ||
| 106 | }, | ||
| 107 | ClientConnected => sub { | ||
| 108 | pass("callback server got client connection"); | ||
| 109 | }, | ||
| 110 | ClientPreConnect => sub { | ||
| 111 | pass("server got pre-connect callback"); | ||
| 112 | ok(fileno($_[ARG0]), "ARG0 is a socket"); | ||
| 113 | return $_[ARG0]; | ||
| 114 | }, | ||
| 115 | ClientDisconnected => sub { | ||
| 116 | pass("callback server got client disconnected"); | ||
| 117 | }, | ||
| 118 | ); | ||
| 119 | |||
| 120 | # Test that the constructor of PoCo::Client::TCP is strict in what it | ||
| 121 | # accepts as valid arguments | ||
| 122 | { | ||
| 123 | eval { POE::Component::Client::TCP->new( | ||
| 124 | RemoteAddress => "1.2.3.4", Odd => "Elephant", "Of Arguments") }; | ||
| 125 | ok($@ =~ /odd|even/, | ||
| 126 | "Client::TCP constructor requires even number of parameters"); | ||
| 127 | |||
| 128 | my %base_args = ( | ||
| 129 | RemoteAddress => '127.0.0.1', | ||
| 130 | RemotePort => 31401, | ||
| 131 | Connected => sub { }, | ||
| 132 | ConnectError => sub { }, | ||
| 133 | Disconnected => sub { }, | ||
| 134 | ServerInput => sub { }, | ||
| 135 | ServerError => sub { }, | ||
| 136 | ServerFlushed => sub { }, | ||
| 137 | ); | ||
| 138 | my $test_missing = sub { | ||
| 139 | my ($args, $remove) = @_; | ||
| 140 | delete $$args{$_} for @$remove; | ||
| 141 | eval { POE::Component::Client::TCP->new( %$args ) }; | ||
| 142 | ok($@ ne '', "Client::TCP constructor requires " . join(", ", @$remove)); | ||
| 143 | }; | ||
| 144 | $test_missing->({%base_args}, ["RemoteAddress"]); | ||
| 145 | $test_missing->({%base_args}, ["RemotePort"]); | ||
| 146 | $test_missing->({%base_args}, ["ServerInput"]); | ||
| 147 | my %mark_args = (%base_args, | ||
| 148 | HighMark => 256, LowMark => 64, | ||
| 149 | ServerHigh => sub { }, ServerLow => sub { } | ||
| 150 | ); | ||
| 151 | $test_missing->({%mark_args}, ["LowMark", "ServerHigh", "ServerLow"]); | ||
| 152 | $test_missing->({%mark_args}, ["HighMark", "ServerHigh", "ServerLow"]); | ||
| 153 | $test_missing->({%mark_args}, ["HighMark", "LowMark", "ServerLow"]); | ||
| 154 | $test_missing->({%mark_args}, ["HighMark", "LowMark", "ServerHigh"]); | ||
| 155 | |||
| 156 | my $test_notref = sub { | ||
| 157 | my $which = shift; | ||
| 158 | eval { POE::Component::Client::TCP->new( %base_args, | ||
| 159 | $which => "Not a reference" ) }; | ||
| 160 | ok($@ =~ /$which.*reference/i, | ||
| 161 | "Client::TCP constructor requires $which to be a reference"); | ||
| 162 | }; | ||
| 163 | $test_notref->("InlineStates"); | ||
| 164 | $test_notref->("PackageStates"); | ||
| 165 | $test_notref->("ObjectStates"); | ||
| 166 | |||
| 167 | eval { POE::Component::Client::TCP->new( %base_args, | ||
| 168 | SessionParams => sub { "Not an array reference" }) }; | ||
| 169 | ok($@ =~ /SessionParams/, | ||
| 170 | "Client::TCP constructor requires SessionParams to be an array reference"); | ||
| 171 | } | ||
| 172 | |||
| 173 | # A client to connect to acceptor_server. | ||
| 174 | |||
| 175 | POE::Component::Client::TCP->new( | ||
| 176 | RemoteAddress => '127.0.0.1', | ||
| 177 | RemotePort => $acceptor_port, | ||
| 178 | |||
| 179 | PreConnect => sub { | ||
| 180 | pass("acceptor pre connect"); | ||
| 181 | ok(fileno($_[ARG0]), "acceptor has a socket"); | ||
| 182 | return $_[ARG0]; | ||
| 183 | }, | ||
| 184 | |||
| 185 | Connected => sub { | ||
| 186 | pass("acceptor client connected"); | ||
| 187 | $_[HEAP]->{server}->put( "quit" ); | ||
| 188 | }, | ||
| 189 | |||
| 190 | ConnectError => sub { | ||
| 191 | my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0..ARG2]; | ||
| 192 | print "acceptor client got $operation error $errnum: $errstr\n"; | ||
| 193 | }, | ||
| 194 | |||
| 195 | Disconnected => sub { | ||
| 196 | pass("acceptor client disconnected"); | ||
| 197 | $_[KERNEL]->post( acceptor_server => 'shutdown' ); | ||
| 198 | }, | ||
| 199 | |||
| 200 | ServerInput => sub { | ||
| 201 | my ($heap, $input) = @_[HEAP, ARG0]; | ||
| 202 | pass("acceptor client got input"); | ||
| 203 | }, | ||
| 204 | |||
| 205 | ServerError => sub { | ||
| 206 | my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0..ARG2]; | ||
| 207 | ok( | ||
| 208 | ($operation eq "read") && ($errnum == 0), | ||
| 209 | "acceptor client got read error 0 (EOF)" | ||
| 210 | ); | ||
| 211 | }, | ||
| 212 | |||
| 213 | ServerFlushed => sub { | ||
| 214 | pass("acceptor client flushed output"); | ||
| 215 | }, | ||
| 216 | ); | ||
| 217 | |||
| 218 | # A client to connect to input_server. | ||
| 219 | |||
| 220 | POE::Component::Client::TCP->new( | ||
| 221 | RemoteAddress => '127.0.0.1', | ||
| 222 | RemotePort => $callback_port, | ||
| 223 | Filter => [ "POE::Filter::Line", Literal => "\n" ], | ||
| 224 | |||
| 225 | Connected => sub { | ||
| 226 | pass("callback client connected"); | ||
| 227 | $_[HEAP]->{server}->put( "quit" ); | ||
| 228 | }, | ||
| 229 | |||
| 230 | ConnectError => sub { | ||
| 231 | my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0..ARG2]; | ||
| 232 | print "callback client got $operation error $errnum: $errstr\n"; | ||
| 233 | }, | ||
| 234 | |||
| 235 | Disconnected => sub { | ||
| 236 | pass("callback client disconnected"); | ||
| 237 | $_[KERNEL]->post( input_server => 'shutdown' ); | ||
| 238 | }, | ||
| 239 | |||
| 240 | ServerInput => sub { | ||
| 241 | my ($heap, $input) = @_[HEAP, ARG0]; | ||
| 242 | pass("callback client got input"); | ||
| 243 | }, | ||
| 244 | |||
| 245 | ServerError => sub { | ||
| 246 | my ($heap, $operation, $errnum, $errstr) = @_[HEAP, ARG0..ARG2]; | ||
| 247 | ok( | ||
| 248 | ($operation eq "read") && ($errnum == 0), | ||
| 249 | "callback client got read error 0 (EOF)" | ||
| 250 | ); | ||
| 251 | }, | ||
| 252 | |||
| 253 | ServerFlushed => sub { | ||
| 254 | pass("callback client flushed output"); | ||
| 255 | }, | ||
| 256 | ); | ||
| 257 | |||
| 258 | # Run the tests. | ||
| 259 | |||
| 260 | POE::Kernel->run(); | ||
| 261 | |||
| 262 | 1; |