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
|
#!/usr/bin/perl -w
use strict;
use Test::More tests => 81;
use Test::Exception;
use Test::Refcount;
use POSIX qw( EAGAIN ECONNRESET );
use IO::Async::Loop;
use IO::Async::Stream;
my $loop = IO::Async::Loop->new();
my ( $S1, $S2 ) = $loop->socketpair() or die "Cannot create socket pair - $!";
# Need sockets in nonblocking mode
$S1->blocking( 0 );
$S2->blocking( 0 );
dies_ok( sub { IO::Async::Stream->new( handle => $S1 ) },
'No on_read' );
lives_ok( sub { IO::Async::Stream->new( write_handle => \*STDOUT ) },
'Write-only Stream works' );
# useful test function
sub read_data($)
{
my ( $s ) = @_;
my $buffer;
my $ret = $s->sysread( $buffer, 8192 );
return $buffer if( defined $ret && $ret > 0 );
die "Socket closed" if( defined $ret && $ret == 0 );
return "" if( $! == EAGAIN );
die "Cannot sysread() - $!";
}
# Reading
my @received;
my $stream = IO::Async::Stream->new(
read_handle => $S1,
on_read => sub {
my $self = shift;
my ( $buffref, $buffclosed ) = @_;
return 0 unless( $$buffref =~ s/^(.*\n)// );
push @received, $1;
return 1;
},
);
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' );
$S2->syswrite( "message\n" );
is_deeply( \@received, [], '@received before loop_once' );
$loop->loop_once( 0.1 );
is_deeply( \@received, [ "message\n" ], '@received after loop_once' );
undef @received;
$S2->syswrite( "return" );
$loop->loop_once( 0.1 );
is_deeply( \@received, [], '@received partial still empty' );
$S2->syswrite( "\n" );
$loop->loop_once( 0.1 );
is_deeply( \@received, [ "return\n" ], '@received partial completed now received' );
undef @received;
$S2->syswrite( "hello\nworld\n" );
$loop->loop_once( 0.1 );
is_deeply( \@received, [ "hello\n", "world\n" ], '@received two at once' );
my @new_lines;
$stream->configure( on_read => sub {
my $self = shift;
my ( $buffref, $closed ) = @_;
return 0 unless( $$buffref =~ s/^(.*\n)// );
push @new_lines, $1;
return 1;
} );
$S2->syswrite( "new\nlines\n" );
$loop->loop_once( 0.1 );
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' );
undef $stream;
{
local $IO::Async::Stream::READLEN = 2;
my @chunks;
$stream = IO::Async::Stream->new(
read_handle => $S1,
on_read => sub {
my ( $self, $buffref, $closed ) = @_;
push @chunks, $$buffref;
$$buffref = "";
},
);
$loop->add( $stream );
$S2->syswrite( "partial" );
$loop->loop_once( 0.1 );
is_deeply( \@chunks, [ "pa" ], '@received with READLEN=2 without read_all' );
$loop->loop_once( 0.1 ) for 1 .. 3;
is_deeply( \@chunks, [ "pa", "rt", "ia", "l" ], '@received finally with READLEN=2 without read_all' );
undef @chunks;
$stream->configure( read_all => 1 );
$S2->syswrite( "partial" );
$loop->loop_once( 0.1 );
is_deeply( \@chunks, [ "pa", "rt", "ia", "l" ], '@received with READLEN=2 with read_all' );
}
# Subclass
my @sub_received;
$stream = TestStream->new(
read_handle => $S1,
);
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' );
$S2->syswrite( "message\n" );
is_deeply( \@sub_received, [], '@sub_received before loop_once' );
$loop->loop_once( 0.1 );
is_deeply( \@sub_received, [ "message\n" ], '@sub_received after loop_once' );
undef @received;
$loop->remove( $stream );
undef $stream;
# Dynamic on_read chaining
my $outer_count = 0;
my $inner_count = 0;
my $record;
$stream = IO::Async::Stream->new(
read_handle => $S1,
on_read => sub {
my ( $self, $buffref, $closed ) = @_;
$outer_count++;
return 0 unless $$buffref =~ s/^(.*\n)//;
my $length = $1;
return sub {
my ( $self, $buffref, $closed ) = @_;
$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 );
$S2->syswrite( "11" ); # No linefeed yet
$loop->loop_once( 0.1 );
is( $outer_count, 1, '$outer_count after idle' );
is( $inner_count, 0, '$inner_count after idle' );
$S2->syswrite( "\n" );
$loop->loop_once( 0.1 );
is( $outer_count, 2, '$outer_count after received length' );
is( $inner_count, 1, '$inner_count after received length' );
$S2->syswrite( "Hello " );
$loop->loop_once( 0.1 );
is( $outer_count, 2, '$outer_count after partial body' );
is( $inner_count, 2, '$inner_count after partial body' );
$S2->syswrite( "world" );
$loop->loop_once( 0.1 );
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' );
undef $stream;
# Writing
my $empty;
$stream = IO::Async::Stream->new(
write_handle => $S1,
on_outgoing_empty => sub { $empty = 1 },
);
ok( defined $stream, 'writing $stream defined' );
isa_ok( $stream, "IO::Async::Stream", 'writing $stream isa IO::Async::Stream' );
is_oneref( $stream, 'writing $stream has refcount 1 initially' );
$loop->add( $stream );
is_refcount( $stream, 2, 'writing $stream has refcount 2 after adding to Loop' );
ok( !$stream->want_writeready, 'want_writeready before write' );
$stream->write( "message\n" );
ok( $stream->want_writeready, 'want_writeready after write' );
$loop->loop_once( 0.1 );
ok( !$stream->want_writeready, 'want_writeready after loop_once' );
is( $empty, 1, '$empty after writing buffer' );
is( read_data( $S2 ), "message\n", 'data after writing buffer' );
$stream->configure( autoflush => 1 );
$stream->write( "immediate\n" );
ok( !$stream->want_writeready, 'not want_writeready after autoflush write' );
is( read_data( $S2 ), "immediate\n", 'data after autoflush write' );
$stream->configure( autoflush => 0 );
$stream->write( "partial " );
$stream->configure( autoflush => 1 );
$stream->write( "data\n" );
ok( !$stream->want_writeready, 'not want_writeready after split autoflush write' );
is( read_data( $S2 ), "partial data\n", 'data after split autoflush write' );
is_refcount( $stream, 2, 'writing $stream has refcount 2 before removing from Loop' );
$loop->remove( $stream );
is_oneref( $stream, 'writing $stream refcount 1 finally' );
undef $stream;
{
local $IO::Async::Stream::WRITELEN = 2;
$stream = IO::Async::Stream->new(
write_handle => $S1,
);
$loop->add( $stream );
$stream->write( "partial" );
$loop->loop_once( 0.1 );
is( read_data( $S2 ), "pa", 'data after writing buffer with WRITELEN=2 without write_all');
$loop->loop_once( 0.1 ) for 1 .. 3;
is( read_data( $S2 ), "rtial", 'data finally after writing buffer with WRITELEN=2 without write_all' );
$stream->configure( write_all => 1 );
$stream->write( "partial" );
$loop->loop_once( 0.1 );
is( read_data( $S2 ), "partial", 'data after writing buffer with WRITELEN=2 with write_all');
}
# Split reading/writing to different handles
my ( $S3, $S4 ) = $loop->socketpair() or die "Cannot socketpair - $!";
$S3->blocking( 0 );
$S4->blocking( 0 );
$stream = IO::Async::Stream->new(
read_handle => $S2,
write_handle => $S3,
on_read => sub {
my $self = shift;
my ( $buffref, $closed ) = @_;
return 0 unless( $$buffref =~ s/^(.*\n)// );
push @received, $1;
return 1;
},
);
is_oneref( $stream, 'split read/write $stream has refcount 1 initially' );
undef @received;
$loop->add( $stream );
is_refcount( $stream, 2, 'split read/write $stream has refcount 2 after adding to Loop' );
$stream->write( "message\n" );
$loop->loop_once( 0.1 );
is( read_data( $S4 ), "message\n", '$S4 receives data from split stream' );
is( read_data( $S1 ), "", '$S1 empty from split stream' );
$S1->syswrite( "reverse\n" );
$loop->loop_once( 0.1 );
is_deeply( \@received, [ "reverse\n" ], '@received on response to split stream' );
is_refcount( $stream, 2, 'split read/write $stream has refcount 2 before removing from Loop' );
$loop->remove( $stream );
is_oneref( $stream, 'split read/write $stream refcount 1 finally' );
undef $stream;
# Close
my $closed = 0;
my $loop_during_closed;
$stream = IO::Async::Stream->new( handle => $S1,
on_read => sub { },
on_closed => sub {
my ( $self ) = @_;
$closed = 1;
$loop_during_closed = $self->get_loop;
},
);
is_oneref( $stream, 'closing $stream has refcount 1 initially' );
$stream->write( "hello" );
$loop->add( $stream );
is_refcount( $stream, 2, 'closing $stream has refcount 2 after adding to Loop' );
is( $closed, 0, 'closed before close' );
$stream->close_when_empty;
is( $closed, 0, 'closed after close' );
$loop->loop_once( 1 ) or die "Nothing ready after 1 second";
is( $closed, 1, 'closed after wait' );
is( $loop_during_closed, $loop, 'loop during closed' );
ok( !defined $stream->get_loop, 'Stream no longer member of Loop' );
is_oneref( $stream, 'closing $stream refcount 1 finally' );
undef $stream;
# Make two sets of sockets now, so that we know they'll definitely have
# different FDs
( $S1, $S2 ) = $loop->socketpair() or die "Cannot create socket pair - $!";
( $S3, $S4 ) = $loop->socketpair() or die "Cannot create socket pair - $!";
# Need sockets in nonblocking mode
$_->blocking( 0 ) for $S1, $S2, $S3, $S4;
my $buffer = "";
$stream = IO::Async::Stream->new(
# No handle yet
on_read => sub {
my ( $self, $buffref, $closed ) = @_;
$buffer .= $$buffref;
$$buffref = "";
return 0;
},
on_closed => sub {
my ( $self ) = @_;
$closed = 1;
},
);
is_oneref( $stream, 'latehandle $stream has refcount 1 initially' );
$loop->add( $stream );
is_refcount( $stream, 2, 'latehandle $stream has refcount 2 after adding to Loop' );
dies_ok( sub { $stream->write( "some text" ) },
'->write on stream with no IO handle fails' );
$stream->set_handle( $S1 );
is_refcount( $stream, 2, 'latehandle $stream has refcount 2 after setting a handle' );
$stream->write( "some text" );
$loop->loop_once( 0.1 );
my $buffer2;
$S2->sysread( $buffer2, 8192 );
is( $buffer2, "some text", 'stream-written text appears' );
$S2->syswrite( "more text" );
$loop->loop_once( 0.1 );
is( $buffer, "more text", 'stream-read text appears' );
$stream->close_when_empty;
is( $closed, 1, 'closed after close' );
ok( !defined $stream->get_loop, 'Stream no longer member of Loop' );
is_oneref( $stream, 'latehandle $stream refcount 1 finally' );
# Now try re-opening the stream with a new handle, and check it continues to
# work
$loop->add( $stream );
$stream->set_handle( $S3 );
$stream->write( "more text" );
$loop->loop_once( 0.1 );
undef $buffer2;
$S4->sysread( $buffer2, 8192 );
is( $buffer2, "more text", 'stream-written text appears after reopen' );
$loop->remove( $stream );
undef $stream;
{
my ( $S1, $S2 ) = $loop->socketpair() or die "Cannot socketpair - $!";
my $stream = IO::Async::Stream->new(
handle => $S1,
on_read => sub { },
);
$stream->write( "hello" );
$loop->add( $stream );
is_refcount( $stream, 2, '$stream has two references' );
undef $stream; # Only ref is now in the Loop
$S2->close;
# $S1 should now be both read- and write-ready.
lives_ok sub { $loop->loop_once }, 'read+write-ready closed Stream doesn\'t die';
}
# Socket errors
my ( $ES1, $ES2 ) = $loop->socketpair() or die "Cannot socketpair - $!";
$ES2->syswrite( "X" ); # ensuring $ES1 is read- and write-ready
# cheating and hackery
bless $ES1, "ErrorSocket";
$ErrorSocket::errno = ECONNRESET;
my $read_errno;
my $write_errno;
$stream = IO::Async::Stream->new(
read_handle => $ES1,
on_read => sub {},
on_read_error => sub { ( undef, $read_errno ) = @_ },
);
$loop->add( $stream );
$loop->loop_once( 0.1 );
cmp_ok( $read_errno, "==", ECONNRESET, 'errno after failed read' );
$loop->remove( $stream );
$stream = IO::Async::Stream->new(
write_handle => $ES1,
on_write_error => sub { ( undef, $write_errno ) = @_ },
);
$loop->add( $stream );
$stream->write( "hello" );
$loop->loop_once( 0.1 );
cmp_ok( $write_errno, "==", ECONNRESET, 'errno after failed write' );
$loop->remove( $stream );
undef $stream;
package TestStream;
use base qw( IO::Async::Stream );
sub on_read
{
my $self = shift;
my ( $buffref, $buffclosed ) = @_;
return 0 unless $$buffref =~ s/^(.*\n)//;
push @sub_received, $1;
return 1;
}
package ErrorSocket;
use base qw( IO::Socket );
our $errno;
sub sysread { $! = $errno; undef; }
sub syswrite { $! = $errno; undef; }
sub close { }
|