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.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 => 1; 11use Test::More tests => 8;
5use Socket; 12use Socket;
13use Errno;
6 14
15{
7my $cv = AnyEvent->condvar; 16 my $cv = AnyEvent->condvar;
8 17
9socketpair my $rd, my $wr, AF_UNIX, SOCK_STREAM, PF_UNSPEC; 18 my ($rd, $wr) = portable_socketpair;
10 19
11my $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 );
12 30
13my $line_cnt = 3;
14my $concat; 31 my $concat;
15 32
16$rd_ae->readlines (sub { 33 $rd_ae->push_read (line => sub {
17 my ($rd_ae, @lines) = @_; 34 is ($_[1], "A", 'A line was read correctly');
18 for (@lines) { 35 my $cb; $cb = sub {
19 chomp;
20 $line_cnt--;
21 $concat .= $_; 36 $concat .= $_[1];
37 $_[0]->push_read (line => $cb);
38 };
39 $_[0]->push_read (line => $cb);
22 } 40 });
23 if ($line_cnt <= 0) { $cv->broadcast }
24});
25 41
26$wr->syswrite ("A\nBC\nDEF\nG\n"); 42 syswrite $wr, "A\012BC\012DEF\012G\012" . ("X" x 113) . "\012";
27$wr->syswrite (("X" x 113) . "\n"); 43 close $wr;
28 44
29$cv->wait; 45 $cv->wait;
46 is ($concat, "BCDEFG" . ("X" x 113), 'initial lines were read correctly');
47}
30 48
49{
50 my $cv = AnyEvent->condvar;
51
52 my ($rd, $wr) = portable_socketpair;
53
54 my $concat;
55
56 my $rd_ae =
57 AnyEvent::Handle->new (
58 fh => $rd,
59 on_eof => sub { $cv->broadcast },
60 on_read => sub {
61 $_[0]->push_read (line => sub {
62 $concat .= "$_[1]:";
63 });
64 }
65 );
66
67 my $wr_ae = new AnyEvent::Handle fh => $wr, on_eof => sub { die };
68
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) });
83
84 $cv->wait;
85
31is ($concat, "ABCDEFG".("X"x113), 'lines were read correctly'); 86 is ($concat, "A:BC:DEF:G:" . ("X" x 113) . ":", 'second set of lines were read correctly');
87}
88

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines