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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines