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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines