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
|
#!/usr/bin/perl
use strict;
use warnings;
use IO::Async::Test;
use Test::More;
use Test::Refcount;
use IO::Async::Loop;
use IO::Async::OS;
use IO::Async::Stream;
use IO::Async::Protocol::Stream;
use IO::Socket::INET;
use Socket qw( SOCK_STREAM );
my $loop = IO::Async::Loop->new_builtin;
testing_loop( $loop );
{
my ( $S1, $S2 ) = IO::Async::OS->socketpair or die "Cannot create socket pair - $!";
# Need sockets in nonblocking mode
$S1->blocking( 0 );
$S2->blocking( 0 );
my @lines;
my $streamproto = IO::Async::Protocol::Stream->new(
transport => IO::Async::Stream->new( handle => $S1 ),
on_read => sub {
my $self = shift;
my ( $buffref, $eof ) = @_;
push @lines, $1 while $$buffref =~ s/^(.*\n)//;
return 0;
},
);
ok( defined $streamproto, '$streamproto defined' );
isa_ok( $streamproto, "IO::Async::Protocol::Stream", '$streamproto isa IO::Async::Protocol::Stream' );
is_oneref( $streamproto, '$streamproto has refcount 1 initially' );
$loop->add( $streamproto );
is_refcount( $streamproto, 2, '$streamproto has refcount 2 after adding to Loop' );
$S2->syswrite( "message\n" );
is_deeply( \@lines, [], '@lines before wait' );
wait_for { scalar @lines };
is_deeply( \@lines, [ "message\n" ], '@lines after wait' );
undef @lines;
my @new_lines;
$streamproto->configure(
on_read => sub {
my $self = shift;
my ( $buffref, $eof ) = @_;
push @new_lines, $1 while $$buffref =~ s/^(.*\n)//;
return 0;
},
);
$S2->syswrite( "new\nlines\n" );
wait_for { scalar @new_lines };
is( scalar @lines, 0, '@lines still empty after on_read replace' );
is_deeply( \@new_lines, [ "new\n", "lines\n" ], '@new_lines after on_read replace' );
$streamproto->write( "response\n" );
my $response = "";
wait_for_stream { $response =~ m/\n/ } $S2 => $response;
is( $response, "response\n", 'response written by protocol' );
my $done;
my $flushed;
$streamproto->write(
sub {
is( $_[0], $streamproto, 'writersub $_[0] is $streamproto' );
return $done++ ? undef : "a lazy message\n";
},
on_flush => sub {
is( $_[0], $streamproto, 'on_flush $_[0] is $streamproto' );
$flushed = 1;
},
);
wait_for { $flushed };
$response = "";
wait_for_stream { $response =~ m/\n/ } $S2 => $response;
is( $response, "a lazy message\n", 'response written by protocol writersub' );
my $closed = 0;
$streamproto->configure(
on_closed => sub { $closed++ },
);
$S2->close;
wait_for { $closed };
is( $closed, 1, '$closed after stream close' );
is_refcount( $streamproto, 2, '$streamproto has refcount 2 before removing from Loop' );
$loop->remove( $streamproto );
is_oneref( $streamproto, '$streamproto refcount 1 finally' );
}
my @sub_lines;
{
my ( $S1, $S2 ) = IO::Async::OS->socketpair or die "Cannot create socket pair - $!";
# Need sockets in nonblocking mode
$S1->blocking( 0 );
$S2->blocking( 0 );
my $streamproto = TestProtocol::Stream->new(
transport => IO::Async::Stream->new( handle => $S1 ),
);
ok( defined $streamproto, 'subclass $streamproto defined' );
isa_ok( $streamproto, "IO::Async::Protocol::Stream", '$streamproto isa IO::Async::Protocol::Stream' );
is_oneref( $streamproto, 'subclass $streamproto has refcount 1 initially' );
$loop->add( $streamproto );
is_refcount( $streamproto, 2, 'subclass $streamproto has refcount 2 after adding to Loop' );
$S2->syswrite( "message\n" );
is_deeply( \@sub_lines, [], '@sub_lines before wait' );
wait_for { scalar @sub_lines };
is_deeply( \@sub_lines, [ "message\n" ], '@sub_lines after wait' );
$loop->remove( $streamproto );
}
{
my ( $S1, $S2 ) = IO::Async::OS->socketpair or die "Cannot create socket pair - $!";
# Need sockets in nonblocking mode
$S1->blocking( 0 );
$S2->blocking( 0 );
my $serversock = IO::Socket::INET->new(
Type => SOCK_STREAM,
LocalHost => "localhost",
LocalPort => 0,
Listen => 1,
) or die "Cannot create server socket - $!";
my @lines;
my $streamproto = IO::Async::Protocol::Stream->new(
on_read => sub {
my $self = shift;
my ( $buffref, $eof ) = @_;
push @lines, $1 while $$buffref =~ s/^(.*\n)//;
return 0;
}
);
$loop->add( $streamproto );
my $connected = 0;
$streamproto->connect(
host => $serversock->sockhost,
service => $serversock->sockport,
family => $serversock->sockdomain,
on_connected => sub { $connected++ },
on_connect_error => sub { die "Test failed early - $_[-1]" },
on_resolve_error => sub { die "Test failed early - $_[-1]" },
);
wait_for { $connected };
my $clientsock = $serversock->accept;
is( $streamproto->transport->read_handle->peerport,
$serversock->sockport,
'Protocol is connected to server socket port' );
$clientsock->syswrite( "A message\n" );
undef @lines;
wait_for { @lines };
is( $lines[0], "A message\n", 'Protocol transport works' );
}
{
my $read_eof;
my $write_eof;
my $streamproto = IO::Async::Protocol::Stream->new(
on_read_eof => sub { $read_eof++ },
on_write_eof => sub { $write_eof++ },
);
$streamproto->configure( transport => my $stream = IO::Async::Stream->new );
$stream->invoke_event( on_read_eof => );
is( $read_eof, 1, '$read_eof after on_read_eof' );
$stream->invoke_event( on_write_eof => );
is( $write_eof, 1, '$write_eof after on_write_eof' );
}
done_testing;
package TestProtocol::Stream;
use base qw( IO::Async::Protocol::Stream );
sub on_read
{
my $self = shift;
my ( $buffref, $eof ) = @_;
push @sub_lines, $1 while $$buffref =~ s/^(.*\n)//;
return 0;
}
|