ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/t/handle/01_readline.t
(Generate patch)

Comparing AnyEvent/t/handle/01_readline.t (file contents):
Revision 1.2 by elmex, Sun Apr 27 17:27:34 2008 UTC vs.
Revision 1.14 by root, Fri Aug 26 03:34:01 2011 UTC

1#!perl 1#!perl
2
3# actually tests a few other read/write types as well
4
2use strict; 5use strict;
6
7use AnyEvent;
8BEGIN { require AnyEvent::Impl::Perl unless $ENV{PERL_ANYEVENT_MODEL} }
3use AnyEvent::Handle; 9use AnyEvent::Handle;
10use AnyEvent::Util;
4use Test::More tests => 2; 11use Test::More tests => 8;
5use Socket; 12use Socket;
6 13use Errno;
7 14
8{ 15{
9 my $cv = AnyEvent->condvar; 16 my $cv = AnyEvent->condvar;
10 17
11 socketpair my $rd, my $wr, AF_UNIX, SOCK_STREAM, PF_UNSPEC; 18 my ($rd, $wr) = portable_socketpair;
12 19
13 my $rd_ae = AnyEvent::Handle->new (fh => $rd); 20 my $rd_ae = AnyEvent::Handle->new (
21 fh => $rd,
22 on_error => sub {
23 ok ($! == &Errno::EPIPE);
24 $cv->broadcast;
25 },
26 on_eof => sub {
27 ok (0, "got eof");
28 },
29 );
30
14 my $concat; 31 my $concat;
15 32
16 $rd_ae->on_eof (sub { $cv->broadcast });
17 $rd_ae->readlines (sub { 33 $rd_ae->push_read (line => sub {
18 my ($rd_ae, @lines) = @_; 34 is ($_[1], "A", 'A line was read correctly');
19 for (@lines) { 35 my $cb; $cb = sub {
20 chomp;
21 $concat .= $_; 36 $concat .= $_[1];
37 $_[0]->push_read (line => $cb);
22 } 38 };
39 $_[0]->push_read (line => $cb);
23 }); 40 });
24 41
25 $wr->syswrite ("A\nBC\nDEF\nG\n"); 42 syswrite $wr, "A\012BC\012DEF\012G\012" . ("X" x 113) . "\012";
26 $wr->syswrite (("X" x 113) . "\n"); 43 close $wr;
27 $wr->close;
28 44
29 $cv->wait; 45 $cv->wait;
30
31 is ($concat, "ABCDEFG".("X"x113), 'lines were read correctly'); 46 is ($concat, "BCDEFG" . ("X" x 113), 'initial lines were read correctly');
32} 47}
33 48
34{ 49{
35 my $cv = AnyEvent->condvar; 50 my $cv = AnyEvent->condvar;
36 51
37 socketpair my $rd, my $wr, AF_UNIX, SOCK_STREAM, PF_UNSPEC; 52 my ($rd, $wr) = portable_socketpair;
38 53
39 my $concat; 54 my $concat;
40 55
41 my $rd_ae = 56 my $rd_ae =
42 AnyEvent::Handle->new ( 57 AnyEvent::Handle->new (
43 fh => $rd, 58 fh => $rd,
44 on_eof => sub { $cv->broadcast }, 59 on_eof => sub { $cv->broadcast },
45 on_readline => sub { 60 on_read => sub {
46 my ($rd_ae, @lines) = @_; 61 $_[0]->push_read (line => sub {
47 for (@lines) {
48 chomp;
49 $concat .= $_; 62 $concat .= "$_[1]:";
50 } 63 });
51 } 64 }
52 ); 65 );
53 66
54 $wr->syswrite ("A\nBC\nDEF\nG\n"); 67 my $wr_ae = new AnyEvent::Handle fh => $wr, on_eof => sub { die };
55 $wr->syswrite (("X" x 113) . "\n"); 68
56 $wr->close; 69 undef $wr;
70 undef $rd;
71
72 $wr_ae->push_write (netstring => "0:xx,,");
73 $wr_ae->push_write (netstring => "");
74 $wr_ae->push_write (storable => [4,3,2]);
75 $wr_ae->push_write (packstring => "w", "hallole" x 99999); # try to exhaust socket buffer here
76 $wr_ae->push_write ("A\012BC\012DEF\nG\012" . ("X" x 113) . "\012");
77 undef $wr_ae;
78
79 $rd_ae->push_read (netstring => sub { is ($_[1], "0:xx,,") });
80 $rd_ae->push_read (netstring => sub { is ($_[1], "") });
81 $rd_ae->push_read (storable => "w", sub { is ("@{$_[1]}", "4 3 2") });
82 $rd_ae->push_read (packstring => "w", sub { is ($_[1], "hallole" x 99999) });
57 83
58 $cv->wait; 84 $cv->wait;
59 85
60 is ($concat, "ABCDEFG".("X"x113), 'second lines were read correctly'); 86 is ($concat, "A:BC:DEF:G:" . ("X" x 113) . ":", 'second set of lines were read correctly');
61} 87}
88

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines