--- AnyEvent/t/handle/01_readline.t 2008/04/27 16:56:17 1.1 +++ AnyEvent/t/handle/01_readline.t 2008/05/21 14:37:55 1.5 @@ -1,31 +1,65 @@ #!perl + use strict; + +use AnyEvent::Impl::Perl; use AnyEvent::Handle; -use Test::More tests => 1; +use Test::More tests => 3; use Socket; -my $cv = AnyEvent->condvar; - -socketpair my $rd, my $wr, AF_UNIX, SOCK_STREAM, PF_UNSPEC; - -my $rd_ae = AnyEvent::Handle->new (fh => $rd); - -my $line_cnt = 3; -my $concat; +{ + my $cv = AnyEvent->condvar; -$rd_ae->readlines (sub { - my ($rd_ae, @lines) = @_; - for (@lines) { - chomp; - $line_cnt--; - $concat .= $_; - } - if ($line_cnt <= 0) { $cv->broadcast } -}); + socketpair my $rd, my $wr, AF_UNIX, SOCK_STREAM, PF_UNSPEC; -$wr->syswrite ("A\nBC\nDEF\nG\n"); -$wr->syswrite (("X" x 113) . "\n"); + my $rd_ae = AnyEvent::Handle->new ( + fh => $rd, + on_eof => sub { $cv->broadcast }, + ); + + my $concat; + + $rd_ae->push_read_line (sub { + is ($_[1], "A", 'A line was read correctly'); + my $cb; $cb = sub { + $concat .= $_[1]; + $_[0]->push_read_line ($cb); + }; + $_[0]->push_read_line ($cb); + }); + + syswrite $wr, "A\nBC\nDEF\nG\n" . ("X" x 113) . "\n"; + close $wr; + + $cv->wait; + is ($concat, "BCDEFG" . ("X" x 113), 'first lines were read correctly'); +} + +{ + my $cv = AnyEvent->condvar; + + socketpair my $rd, my $wr, AF_UNIX, SOCK_STREAM, PF_UNSPEC; + + my $concat; + + my $rd_ae = + AnyEvent::Handle->new ( + fh => $rd, + on_eof => sub { $cv->broadcast }, + on_read => sub { + $_[0]->push_read_line (sub { + $concat .= "$_[1]:"; + }); + } + ); + + my $wr_ae = new AnyEvent::Handle fh => $wr, on_eof => sub { die }; + + $wr_ae->push_write ("A\nBC\nDEF\nG\n" . ("X" x 113) . "\n"); + undef $wr; + undef $wr_ae; -$cv->wait; + $cv->wait; -is ($concat, "ABCDEFG".("X"x113), 'lines were read correctly'); + is ($concat, "A:BC:DEF:G:" . ("X" x 113) . ":", 'second lines were read correctly'); +}