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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
|
#!/usr/bin/perl
use v5.14;
use warnings;
use IO::Async::Test;
use Test2::V0 0.000149;
use Test::Metrics::Any;
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_builtin;
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( \@lines, [], '@lines before wait' );
wait_for { scalar @lines };
is( \@lines, [ "message\n" ], '@lines after wait' );
undef @lines;
$wr->syswrite( "return" );
$loop->loop_once( 0.1 ); # nothing happens
is( \@lines, [], '@lines partial still empty' );
$wr->syswrite( "\n" );
wait_for { scalar @lines };
is( \@lines, [ "return\n" ], '@lines partial completed now received' );
undef @lines;
$wr->syswrite( "hello\nworld\n" );
wait_for { scalar @lines };
is( \@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( \@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' );
}
# Abstract reading with reader function
{
my ( $rd, $wr ) = mkhandles;
my $buffer = "Here is the contents\n";
my @lines;
my $stream = IO::Async::Stream->new(
read_handle => $rd,
reader => sub {
my $self = shift;
my $more = substr( $buffer, 0, $_[2], "" );
$_[1] .= $more;
return length $more;
},
on_read => sub {
my $self = shift;
my ( $buffref, $eof ) = @_;
push @lines, $1 while $$buffref =~ s/^(.*\n)//;
return 0;
},
);
$loop->add( $stream );
# make it readready
$wr->syswrite( "1" );
wait_for { scalar @lines };
is( \@lines, [ "Here is the contents\n" ], '@lines from stream with abstract reader' );
$loop->remove( $stream );
}
# ->want_readready_for_write
{
my ( $rd, $wr ) = mkhandles;
my $reader_called;
my $writer_called;
my $stream = IO::Async::Stream->new(
handle => $rd,
on_read => sub { return 0; }, # ignore reading
reader => sub { $reader_called++; sysread( $rd, $_[2], $_[3] ) },
writer => sub { $writer_called++; return 1 },
);
$loop->add( $stream );
# Hacky hack - make the stream want to write, but don't mark the stream write-ready
$stream->write( "A" );
$stream->want_writeready_for_write( 0 );
# End hack
# make it readready
$wr->syswrite( "1" );
wait_for { $reader_called };
ok( !$writer_called, 'writer not yet called before ->want_readready_for_write' );
$stream->want_readready_for_write( 1 );
undef $reader_called;
$wr->syswrite( "2" );
wait_for { $reader_called && $writer_called };
ok( $writer_called, 'writer now invoked with ->want_readready_for_write' );
$loop->remove( $stream );
}
{
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( \@chunks, [ "pa" ], '@lines with read_len=2 without read_all' );
wait_for { @chunks == 4 };
is( \@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( \@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( !dies { $no_on_read_stream = IO::Async::Stream->new( read_handle => $rd ) },
'Allowed to construct a Stream without an on_read handler' );
ok( dies { $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( \@sub_lines, [], '@sub_lines before wait' );
wait_for { scalar @sub_lines };
is( \@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' );
}
# ->push_on_read
{
my ( $rd, $wr ) = mkhandles;
my $base;
my $stream = IO::Async::Stream->new( read_handle => $rd,
on_read => sub {
my ( $self, $buffref ) = @_;
$base = $$buffref; $$buffref = "";
return 0;
},
);
$loop->add( $stream );
my $firstline;
$stream->push_on_read(
sub {
my ( $stream, $buffref, $eof ) = @_;
return 0 unless $$buffref =~ s/(.*)\n//;
$firstline = $1;
return undef;
}
);
my $eightbytes;
$stream->push_on_read(
sub {
my ( $stream, $buffref, $eof ) = @_;
return 0 unless length $$buffref >= 8;
$eightbytes = substr( $$buffref, 0, 8, "" );
return undef;
}
);
$wr->syswrite( "The first line\nABCDEFGHIJK" );
wait_for { defined $firstline and defined $eightbytes };
is( $firstline, "The first line", '$firstline from ->push_on_read CODE' );
is( $eightbytes, "ABCDEFGH", '$eightbytes from ->push_on_read CODE' );
is( $base, "IJK", '$base from ->push_on_read CODE' );
$loop->remove( $stream );
}
# 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;
ok( !$stream->is_read_eof, '$stream ->is_read_eof before wait' );
is( $eof, 0, 'EOF indication before wait' );
wait_for { $eof };
ok( $stream->is_read_eof, '$stream ->is_read_eof after wait' );
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' );
ref_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' );
}
# ->read Futures
{
my ( $rd, $wr ) = mkhandles;
my $stream = IO::Async::Stream->new( read_handle => $rd,
on_read => sub {
my ( $self, $buffref ) = @_;
die "Base on_read invoked with data in the buffer" if length $$buffref;
return 0;
},
);
$loop->add( $stream );
my $f_atmost = $stream->read_atmost( 256 );
$wr->syswrite( "Some data\n" );
wait_for { $f_atmost->is_ready };
is( scalar $f_atmost->get, "Some data\n", '->read_atmost' );
my $f_exactly = $stream->read_exactly( 4 );
my $f_until_qr = $stream->read_until( qr/[A-Z][a-z]*/ );
my $f_until_str = $stream->read_until( "\n" );
$wr->syswrite( "Here is the First line of input\n" );
wait_for { $f_exactly->is_ready and $f_until_qr->is_ready and $f_until_str->is_ready };
is( scalar $f_exactly->get, "Here", '->read_exactly' );
is( scalar $f_until_qr->get, " is the First", '->read_until regexp' );
is( scalar $f_until_str->get, " line of input\n", '->read_until str' );
my $f_first = $stream->read_until( "\n" );
my $f_second = $stream->read_until( "\n" );
$f_first->cancel;
$wr->syswrite( "For the second\n" );
wait_for { $f_second->is_ready };
is( scalar $f_second->get, "For the second\n", 'Second ->read_until recieves data after first is ->cancelled' );
my $f_until_eof = $stream->read_until_eof;
$wr->syswrite( "And the rest of it" );
$wr->close;
wait_for { $f_until_eof->is_ready };
is( scalar $f_until_eof->get, "And the rest of it", '->read_until_eof' );
# No need to remove as ->close did it
}
# RT101774
{
my ( $rd, $wr ) = mkhandles;
my $stream = IO::Async::Stream->new( read_handle => $rd,
on_read => sub { 0 },
);
$loop->add( $stream );
$wr->syswrite( "lalaLALA" );
my $f = wait_for_future $stream->read_exactly( 4 )->then( sub {
$stream->read_exactly( 4 );
});
is( scalar $f->get, "LALA", 'chained ->read_exactly' );
$loop->remove( $stream );
}
# watermarks
{
my ( $rd, $wr ) = mkhandles;
my $high_hit = 0;
my $low_hit = 0;
my $stream = IO::Async::Stream->new(
read_handle => $rd,
on_read => sub { 0 }, # we'll work by Futures
read_high_watermark => 8,
read_low_watermark => 4,
on_read_high_watermark => sub { $high_hit++ },
on_read_low_watermark => sub { $low_hit++ },
);
$loop->add( $stream );
$wr->syswrite( "1234567890" );
wait_for { $high_hit };
ok( 1, "Reading too much hits high watermark" );
is( $stream->read_exactly( 8 )->get, "12345678", 'Stream->read_exactly yields bytes' );
is( $low_hit, 1, 'Low watermark hit after ->read' );
}
# 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' );
my $f = wait_for_future $stream->read_atmost( 256 );
cmp_ok( ( $f->failure )[-1], "==", ECONNRESET, 'failure from ->read_atmost 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' );
}
# Metrics
SKIP: {
skip "Metrics are unavailable" unless $IO::Async::Metrics::METRICS;
my ( $rd, $wr ) = mkhandles;
my $done;
my $stream = IO::Async::Stream->new(
read_handle => $rd,
on_read => sub {
my ( $self, $bufref ) = @_;
$done = 1 if length $$bufref == 100;
return 0;
},
);
$loop->add( $stream );
$wr->syswrite( "X"x100 );
is_metrics_from(
sub { wait_for { $done } },
{ io_async_stream_read => 100 },
'Stream reading increments metric'
);
$loop->remove( $stream );
}
done_testing;
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;
}
|