1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
#!/usr/bin/perl
use v5.14;
use warnings;
use IO::Async::Test;
use Test2::V0 0.000149;
use IO::Async::Loop;
use IO::Socket::INET;
use IO::Async::Listener;
# Some odd locations like BSD jails might not like INADDR_ANY. We'll establish
# a baseline first to test against
my $INADDR_ANY = do {
my $anysock = IO::Socket::INET->new( LocalPort => 0, Listen => 1 );
$anysock->sockaddr;
};
my $INADDR_ANY_HOST = inet_ntoa( $INADDR_ANY );
if( $INADDR_ANY ne INADDR_ANY ) {
diag( "Testing with INADDR_ANY=$INADDR_ANY_HOST; this may be because of odd networking" );
}
my $loop = IO::Async::Loop->new_builtin;
testing_loop( $loop );
my $listensock;
$listensock = IO::Socket::INET->new(
LocalAddr => "localhost",
Type => SOCK_STREAM,
Listen => 1,
Blocking => 0,
) or die "Cannot socket() - $!";
{
my $newclient;
my $listener = IO::Async::Listener->new(
handle => $listensock,
on_accept => sub { ( undef, $newclient ) = @_ },
);
ok( defined $listener, 'defined $listener' );
isa_ok( $listener, [ "IO::Async::Listener" ], '$listener isa IO::Async::Listener' );
isa_ok( $listener, [ "IO::Async::Notifier" ], '$listener isa IO::Async::Notifier' );
is_oneref( $listener, '$listener has refcount 1 initially' );
ok( $listener->is_listening, '$listener is_listening' );
is( [ unpack_sockaddr_in $listener->sockname ],
[ unpack_sockaddr_in $listensock->sockname ], '$listener->sockname' );
is( $listener->family, AF_INET, '$listener->family' );
is( $listener->socktype, SOCK_STREAM, '$listener->sockname' );
$loop->add( $listener );
is_refcount( $listener, 2, '$listener has refcount 2 after adding to Loop' );
my $clientsock = IO::Socket::INET->new( Type => SOCK_STREAM )
or die "Cannot socket() - $!";
$clientsock->connect( $listensock->sockname ) or die "Cannot connect() - $!";
ok( defined $clientsock->peername, '$clientsock is connected' );
wait_for { defined $newclient };
is( [ unpack_sockaddr_in $newclient->peername ],
[ unpack_sockaddr_in $clientsock->sockname ], '$newclient peer is correct' );
is_refcount( $listener, 2, '$listener has refcount 2 before removing from Loop' );
$loop->remove( $listener );
is_oneref( $listener, '$listener has refcount 1 after removing from Loop' );
}
# on_accept handle constructors
{
my $accepted;
my $listener = IO::Async::Listener->new(
handle => $listensock,
on_accept => sub { ( undef, $accepted ) = @_ },
);
$loop->add( $listener );
require IO::Async::Stream;
# handle_constructor
{
$listener->configure( handle_constructor => sub {
return IO::Async::Stream->new;
} );
my $clientsock = IO::Socket::INET->new( Type => SOCK_STREAM )
or die "Cannot socket() - $!";
$clientsock->connect( $listensock->sockname ) or die "Cannot connect() - $!";
wait_for { defined $accepted };
isa_ok( $accepted, [ "IO::Async::Stream" ], '$accepted with handle_constructor' );
undef $accepted;
}
# handle_class
{
$listener->configure( handle_class => "IO::Async::Stream" );
my $clientsock = IO::Socket::INET->new( Type => SOCK_STREAM )
or die "Cannot socket() - $!";
$clientsock->connect( $listensock->sockname ) or die "Cannot connect() - $!";
wait_for { defined $accepted };
isa_ok( $accepted, [ "IO::Async::Stream" ], '$accepted with handle_constructor' );
undef $accepted;
}
$loop->remove( $listener );
}
# on_stream
{
my $newstream;
my $listener = IO::Async::Listener->new(
handle => $listensock,
on_stream => sub { ( undef, $newstream ) = @_ },
);
$loop->add( $listener );
my $clientsock = IO::Socket::INET->new( Type => SOCK_STREAM )
or die "Cannot socket() - $!";
$clientsock->connect( $listensock->sockname ) or die "Cannot connect() - $!";
wait_for { defined $newstream };
isa_ok( $newstream, [ "IO::Async::Stream" ], 'on_stream $newstream isa IO::Async::Stream' );
is( [ unpack_sockaddr_in $newstream->read_handle->peername ],
[ unpack_sockaddr_in $clientsock->sockname ], '$newstream sock peer is correct' );
$loop->remove( $listener );
}
# on_socket
{
my $newsocket;
my $listener = IO::Async::Listener->new(
handle => $listensock,
on_socket => sub { ( undef, $newsocket ) = @_ },
);
$loop->add( $listener );
my $clientsock = IO::Socket::INET->new( Type => SOCK_STREAM )
or die "Cannot socket() - $!";
$clientsock->connect( $listensock->sockname ) or die "Cannot connect() - $!";
wait_for { defined $newsocket };
isa_ok( $newsocket, [ "IO::Async::Socket" ], 'on_socket $newsocket isa IO::Async::Socket' );
is( [ unpack_sockaddr_in $newsocket->read_handle->peername ],
[ unpack_sockaddr_in $clientsock->sockname ], '$newsocket sock peer is correct' );
$loop->remove( $listener );
}
# stop listening
{
my $listener = IO::Async::Listener->new(
handle => $listensock,
on_accept => sub {},
);
$loop->add( $listener );
$listener->configure( handle => undef );
is( $listener->read_handle, undef, '$listener has no read handle any more' );
$loop->remove( $listener );
}
# Subclass
{
my $sub_newclient;
{
package TestListener;
use base qw( IO::Async::Listener );
sub on_accept { ( undef, $sub_newclient ) = @_ }
}
my $listener = TestListener->new(
handle => $listensock,
);
ok( defined $listener, 'subclass defined $listener' );
isa_ok( $listener, [ "IO::Async::Listener" ], 'subclass $listener isa IO::Async::Listener' );
is_oneref( $listener, 'subclass $listener has refcount 1 initially' );
$loop->add( $listener );
is_refcount( $listener, 2, 'subclass $listener has refcount 2 after adding to Loop' );
my $clientsock = IO::Socket::INET->new( LocalAddr => "127.0.0.1", Type => SOCK_STREAM )
or die "Cannot socket() - $!";
$clientsock->connect( $listensock->sockname ) or die "Cannot connect() - $!";
ok( defined $clientsock->peername, 'subclass $clientsock is connected' );
wait_for { defined $sub_newclient };
is( [ unpack_sockaddr_in $sub_newclient->peername ],
[ unpack_sockaddr_in $clientsock->sockname ], '$sub_newclient peer is correct' );
is_refcount( $listener, 2, 'subclass $listener has refcount 2 before removing from Loop' );
$loop->remove( $listener );
is_oneref( $listener, 'subclass $listener has refcount 1 after removing from Loop' );
}
# Subclass with handle_constructor
{
{
package TestListener::WithConstructor;
use base qw( IO::Async::Listener );
sub handle_constructor { return IO::Async::Stream->new }
}
my $accepted;
my $listener = TestListener::WithConstructor->new(
handle => $listensock,
on_accept => sub { ( undef, $accepted ) = @_; },
);
$loop->add( $listener );
my $clientsock = IO::Socket::INET->new( Type => SOCK_STREAM )
or die "Cannot socket() - $!";
$clientsock->connect( $listensock->sockname ) or die "Cannot connect() - $!";
wait_for { defined $accepted };
isa_ok( $accepted, [ "IO::Async::Stream" ], '$accepted with handle_constructor method' );
$loop->remove( $listener );
}
{
my $newclient;
my $listener = IO::Async::Listener->new(
on_accept => sub { ( undef, $newclient ) = @_ },
);
ok( !$listener->is_listening, '$listener is_listening not yet' );
$loop->add( $listener );
my $listen_self;
$listener->listen(
addr => { family => "inet", socktype => "stream", addr => pack_sockaddr_in( 0, $INADDR_ANY ) },
on_listen => sub { $listen_self = shift },
on_listen_error => sub { die "Test died early - $_[0] - $_[-1]\n"; },
);
ok( $listener->is_listening, '$listener is_listening' );
my $sockname = $listener->sockname;
ok( defined $sockname, 'defined $sockname' );
my ( $port, $sinaddr ) = unpack_sockaddr_in( $sockname );
ok( $port > 0, 'socket listens on some defined port number' );
is( inet_ntoa( $sinaddr ), $INADDR_ANY_HOST, 'socket listens on INADDR_ANY' );
is( $listener->family, AF_INET, '$listener->family' );
is( $listener->socktype, SOCK_STREAM, '$listener->sockname' );
ref_is( $listen_self, $listener, '$listen_self is $listener' );
undef $listen_self; # for refcount
my $clientsock = IO::Socket::INET->new( Type => SOCK_STREAM )
or die "Cannot socket() - $!";
$clientsock->connect( pack_sockaddr_in( $port, INADDR_LOOPBACK ) ) or die "Cannot connect() - $!";
ok( defined $clientsock->peername, '$clientsock is connected' );
wait_for { defined $newclient };
is( [ unpack_sockaddr_in $newclient->peername ],
[ unpack_sockaddr_in $clientsock->sockname ], '$newclient peer is correct' );
$loop->remove( $listener );
}
done_testing;
|