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
|
#!/usr/bin/perl
use strict;
use warnings;
use IO::Async::Test;
use Test::More;
use IO::Async::Loop;
use IO::Async::Handle;
use IO::Async::OS;
use IO::Socket::INET;
use Socket qw( SOCK_STREAM );
my $loop = IO::Async::Loop->new_builtin;
testing_loop( $loop );
# Try connect(2)ing to a socket we've just created
my $listensock = IO::Socket::INET->new(
Type => SOCK_STREAM,
LocalAddr => 'localhost',
LocalPort => 0,
Listen => 1
) or die "Cannot create listensock - $!";
my $addr = $listensock->sockname;
# ->connect to plain addr
{
my $handle = IO::Async::Handle->new(
on_read_ready => sub {},
on_write_ready => sub {},
);
$loop->add( $handle );
my $f = $handle->connect( addr => [ 'inet', 'stream', 0, $addr ] );
ok( defined $f, '$handle->connect Future defined' );
wait_for { $f->is_ready };
$f->failure and $f->get;
ok( defined $handle->read_handle, '$handle->read_handle defined after ->connect addr' );
is( $handle->read_handle->peerport, $listensock->sockport, '$handle->read_handle->peerport after ->connect addr' );
$listensock->accept; # drop it
$loop->remove( $handle );
}
# ->connect to host/service
{
my $handle = IO::Async::Handle->new(
on_read_ready => sub {},
on_write_ready => sub {},
);
$loop->add( $handle );
my $f = $handle->connect(
family => "inet",
socktype => "stream",
host => $listensock->sockhost,
service => $listensock->sockport,
);
wait_for { $f->is_ready };
$f->failure and $f->get;
ok( defined $handle->read_handle, '$handle->read_handle defined after ->connect host/service' );
is( $handle->read_handle->peerport, $listensock->sockport, '$handle->read_handle->peerport after ->connect host/service' );
$listensock->accept; # drop it
$loop->remove( $handle );
}
done_testing;
|