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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
#!/usr/bin/perl -w
use strict;
use IO::Async::Test;
use Test::More tests => 54;
use Test::Fatal;
use Test::Refcount;
use IO::File;
use POSIX qw( ECONNRESET );
use IO::Async::Loop;
use IO::Async::OS;
use IO::Async::Stream;
my $loop = IO::Async::Loop->new;
testing_loop( $loop );
sub mkhandles
{
my ( $rd, $wr ) = IO::Async::OS->pipepair or die "Cannot pipe() - $!";
# Need handles in nonblocking mode
$rd->blocking( 0 );
$wr->blocking( 0 );
return ( $rd, $wr );
}
{
my ( $rd, $wr ) = mkhandles;
my @lines;
my $stream = IO::Async::Stream->new(
read_handle => $rd,
on_read => sub {
my $self = shift;
my ( $buffref, $eof ) = @_;
push @lines, $1 while $$buffref =~ s/^(.*\n)//;
return 0;
},
);
ok( defined $stream, 'reading $stream defined' );
isa_ok( $stream, "IO::Async::Stream", 'reading $stream isa IO::Async::Stream' );
is_oneref( $stream, 'reading $stream has refcount 1 initially' );
$loop->add( $stream );
is_refcount( $stream, 2, 'reading $stream has refcount 2 after adding to Loop' );
$wr->syswrite( "message\n" );
is_deeply( \@lines, [], '@lines before wait' );
wait_for { scalar @lines };
is_deeply( \@lines, [ "message\n" ], '@lines after wait' );
undef @lines;
$wr->syswrite( "return" );
$loop->loop_once( 0.1 ); # nothing happens
is_deeply( \@lines, [], '@lines partial still empty' );
$wr->syswrite( "\n" );
wait_for { scalar @lines };
is_deeply( \@lines, [ "return\n" ], '@lines partial completed now received' );
undef @lines;
$wr->syswrite( "hello\nworld\n" );
wait_for { scalar @lines };
is_deeply( \@lines, [ "hello\n", "world\n" ], '@lines two at once' );
undef @lines;
my @new_lines;
$stream->configure(
on_read => sub {
my $self = shift;
my ( $buffref, $eof ) = @_;
push @new_lines, $1 while $$buffref =~ s/^(.*\n)//;
return 0;
},
);
$wr->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' );
is_refcount( $stream, 2, 'reading $stream has refcount 2 before removing from Loop' );
$loop->remove( $stream );
is_oneref( $stream, 'reading $stream refcount 1 finally' );
}
{
my ( $rd, $wr ) = mkhandles;
my @chunks;
my $stream = IO::Async::Stream->new(
read_handle => $rd,
read_len => 2,
on_read => sub {
my ( $self, $buffref, $eof ) = @_;
push @chunks, $$buffref;
$$buffref = "";
},
);
$loop->add( $stream );
$wr->syswrite( "partial" );
wait_for { scalar @chunks };
is_deeply( \@chunks, [ "pa" ], '@lines with read_len=2 without read_all' );
wait_for { @chunks == 4 };
is_deeply( \@chunks, [ "pa", "rt", "ia", "l" ], '@lines finally with read_len=2 without read_all' );
undef @chunks;
$stream->configure( read_all => 1 );
$wr->syswrite( "partial" );
wait_for { scalar @chunks };
is_deeply( \@chunks, [ "pa", "rt", "ia", "l" ], '@lines with read_len=2 with read_all' );
$loop->remove( $stream );
}
{
my ( $rd, $wr ) = mkhandles;
my $no_on_read_stream;
ok( !exception { $no_on_read_stream = IO::Async::Stream->new( read_handle => $rd ) },
'Allowed to construct a Stream without an on_read handler' );
ok( exception { $loop->add( $no_on_read_stream ) },
'Not allowed to add an on_read-less Stream to a Loop' );
}
# Subclass
my @sub_lines;
{
my ( $rd, $wr ) = mkhandles;
my $stream = TestStream->new(
read_handle => $rd,
);
ok( defined $stream, 'reading subclass $stream defined' );
isa_ok( $stream, "IO::Async::Stream", 'reading $stream isa IO::Async::Stream' );
is_oneref( $stream, 'subclass $stream has refcount 1 initially' );
$loop->add( $stream );
is_refcount( $stream, 2, 'subclass $stream has refcount 2 after adding to Loop' );
$wr->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( $stream );
}
# Dynamic on_read chaining
{
my ( $rd, $wr ) = mkhandles;
my $outer_count = 0;
my $inner_count = 0;
my $record;
my $stream = IO::Async::Stream->new(
read_handle => $rd,
on_read => sub {
my ( $self, $buffref, $eof ) = @_;
$outer_count++;
return 0 unless $$buffref =~ s/^(.*\n)//;
my $length = $1;
return sub {
my ( $self, $buffref, $eof ) = @_;
$inner_count++;
return 0 unless length $$buffref >= $length;
$record = substr( $$buffref, 0, $length, "" );
return undef;
}
},
);
is_oneref( $stream, 'dynamic reading $stream has refcount 1 initially' );
$loop->add( $stream );
$wr->syswrite( "11" ); # No linefeed yet
wait_for { $outer_count > 0 };
is( $outer_count, 1, '$outer_count after idle' );
is( $inner_count, 0, '$inner_count after idle' );
$wr->syswrite( "\n" );
wait_for { $inner_count > 0 };
is( $outer_count, 2, '$outer_count after received length' );
is( $inner_count, 1, '$inner_count after received length' );
$wr->syswrite( "Hello " );
wait_for { $inner_count > 1 };
is( $outer_count, 2, '$outer_count after partial body' );
is( $inner_count, 2, '$inner_count after partial body' );
$wr->syswrite( "world" );
wait_for { $inner_count > 2 };
is( $outer_count, 3, '$outer_count after complete body' );
is( $inner_count, 3, '$inner_count after complete body' );
is( $record, "Hello world", '$record after complete body' );
$loop->remove( $stream );
is_oneref( $stream, 'dynamic reading $stream has refcount 1 finally' );
}
# EOF
{
my ( $rd, $wr ) = mkhandles;
my $eof = 0;
my $partial;
my $stream = IO::Async::Stream->new( read_handle => $rd,
on_read => sub {
my ( undef, $buffref, $eof ) = @_;
$partial = $$buffref if $eof;
return 0;
},
on_read_eof => sub { $eof++ },
);
$loop->add( $stream );
$wr->syswrite( "Incomplete" );
$wr->close;
is( $eof, 0, 'EOF indication before wait' );
wait_for { $eof };
is( $eof, 1, 'EOF indication after wait' );
is( $partial, "Incomplete", 'EOF stream retains partial input' );
ok( !defined $stream->loop, 'EOF stream no longer member of Loop' );
ok( !defined $stream->read_handle, 'Stream no longer has a read_handle' );
}
# Disabled close_on_read_eof
{
my ( $rd, $wr ) = mkhandles;
my $eof = 0;
my $partial;
my $stream = IO::Async::Stream->new( read_handle => $rd,
on_read => sub {
my ( undef, $buffref, $eof ) = @_;
$partial = $$buffref if $eof;
return 0;
},
on_read_eof => sub { $eof++ },
close_on_read_eof => 0,
);
$loop->add( $stream );
$wr->syswrite( "Incomplete" );
$wr->close;
is( $eof, 0, 'EOF indication before wait' );
wait_for { $eof };
is( $eof, 1, 'EOF indication after wait' );
is( $partial, "Incomplete", 'EOF stream retains partial input' );
ok( defined $stream->loop, 'EOF stream still member of Loop' );
ok( defined $stream->read_handle, 'Stream still has a read_handle' );
}
# Close
{
my ( $rd, $wr ) = mkhandles;
my $closed = 0;
my $loop_during_closed;
my $stream = IO::Async::Stream->new( read_handle => $rd,
on_read => sub { },
on_closed => sub {
my ( $self ) = @_;
$closed = 1;
$loop_during_closed = $self->loop;
},
);
is_oneref( $stream, 'closing $stream has refcount 1 initially' );
$loop->add( $stream );
is_refcount( $stream, 2, 'closing $stream has refcount 2 after adding to Loop' );
is( $closed, 0, 'closed before close' );
$stream->close;
is( $closed, 1, 'closed after close' );
is( $loop_during_closed, $loop, 'loop during closed' );
ok( !defined $stream->loop, 'Stream no longer member of Loop' );
is_oneref( $stream, 'closing $stream refcount 1 finally' );
}
# Errors
{
my ( $rd, $wr ) = mkhandles;
$wr->syswrite( "X" ); # ensuring $rd is read-ready
no warnings 'redefine';
local *IO::Handle::sysread = sub {
$! = ECONNRESET;
return undef;
};
my $read_errno;
my $stream = IO::Async::Stream->new(
read_handle => $rd,
on_read => sub {},
on_read_error => sub { ( undef, $read_errno ) = @_ },
);
$loop->add( $stream );
wait_for { defined $read_errno };
cmp_ok( $read_errno, "==", ECONNRESET, 'errno after failed read' );
$loop->remove( $stream );
}
{
binmode STDIN; # Avoid harmless warning in case -CS is in effect
my $stream = IO::Async::Stream->new_for_stdin;
is( $stream->read_handle, \*STDIN, 'Stream->new_for_stdin->read_handle is STDIN' );
}
package TestStream;
use base qw( IO::Async::Stream );
sub on_read
{
my $self = shift;
my ( $buffref, $eof ) = @_;
return 0 unless $$buffref =~ s/^(.*\n)//;
push @sub_lines, $1;
return 1;
}
|