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.3 by root, Sun Apr 27 19:08:38 2008 UTC vs.
Revision 1.11 by root, Fri Jun 6 11:05:16 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines